add a blocking accept method to RateLimiter

This commit is contained in:
Ben Parees
2015-04-01 17:57:30 -04:00
parent 0cf3590c36
commit 70be667cf8
3 changed files with 22 additions and 0 deletions

View File

@@ -24,6 +24,8 @@ import (
type RateLimiter interface {
// CanAccept returns true if the rate is below the limit, false otherwise
CanAccept() bool
// Accept returns once a token becomes available.
Accept()
// Stop stops the rate limiter, subsequent calls to CanAccept will return false
Stop()
}
@@ -73,6 +75,11 @@ func (t *tickRateLimiter) CanAccept() bool {
}
}
// Accept will block until a token becomes available
func (t *tickRateLimiter) Accept() {
<-t.tokens
}
func (t *tickRateLimiter) Stop() {
close(t.stop)
}