diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index acff4d96b3a..8da863d2d0f 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -549,7 +549,7 @@ }, { "ImportPath": "github.com/juju/ratelimit", - "Rev": "772f5c38e468398c4511514f4f6aa9a4185bc0a0" + "Rev": "77ed1c8a01217656d2080ad51981f6e99adaa177" }, { "ImportPath": "github.com/kardianos/osext", diff --git a/Godeps/_workspace/src/github.com/juju/ratelimit/ratelimit.go b/Godeps/_workspace/src/github.com/juju/ratelimit/ratelimit.go index a36ffc74fec..3ef32fbcc09 100644 --- a/Godeps/_workspace/src/github.com/juju/ratelimit/ratelimit.go +++ b/Godeps/_workspace/src/github.com/juju/ratelimit/ratelimit.go @@ -8,10 +8,10 @@ package ratelimit import ( + "math" "strconv" "sync" "time" - "math" ) // Bucket represents a token bucket that fills at a predetermined rate. @@ -171,6 +171,30 @@ func (tb *Bucket) takeAvailable(now time.Time, count int64) int64 { return count } +// Available returns the number of available tokens. It will be negative +// when there are consumers waiting for tokens. Note that if this +// returns greater than zero, it does not guarantee that calls that take +// tokens from the buffer will succeed, as the number of available +// tokens could have changed in the meantime. This method is intended +// primarily for metrics reporting and debugging. +func (tb *Bucket) Available() int64 { + return tb.available(time.Now()) +} + +// available is the internal version of available - it takes the current time as +// an argument to enable easy testing. +func (tb *Bucket) available(now time.Time) int64 { + tb.mu.Lock() + defer tb.mu.Unlock() + tb.adjust(now) + return tb.avail +} + +// Capacity returns the capacity that the bucket was created with. +func (tb *Bucket) Capacity() int64 { + return tb.capacity +} + // Rate returns the fill rate of the bucket, in tokens per second. func (tb *Bucket) Rate() float64 { return 1e9 * float64(tb.quantum) / float64(tb.fillInterval)