Posted to tcl by rmax at Tue Mar 28 22:35:28 GMT 2017view pretty

coroutine ratelimit apply {{} {
    set limits ""
    while 1 {
	set args [yieldto return -level 0]
	while 1 {
	    lassign $args key ms body
	    if {$ms eq ""} {
		dict unset limits $key
		break
	    } else {
		set squelch [dict exists $limits $key]
		if {$squelch} {
		    after cancel [dict get $limits $key]
		}
		dict set limits $key [after $ms [list [info coroutine] $key]]
		if {$squelch} break
		set args [yieldto eval $body]
	    }
	}
    }
}}