65aaa607ab
Change made by: - running "gvt fetch" on each of the packages mentioned in Godeps/Godeps.json - `rm -rf Godeps` - tweaking the build scripts to not mention Godeps - tweaking the build scripts to test `./lib/...`, `./cmd/...` explicitly (to avoid testing vendor) - tweaking the build scripts to not juggle GOPATH for Godeps and instead set GO15VENDOREXPERIMENT. This also results in some updated packages at the same time I bet. Building with Go 1.3 and 1.4 still *works* but won't use our vendored dependencies - the user needs to have the actual packages in their GOPATH then, which they'll get with a normal "go get". Building with Go 1.6+ will get our vendored dependencies by default even when not using our build script, which is nice. By doing this we gain some freedom in that we can pick and choose manually what to include in vendor, as it's not based on just dependency analysis of our own code. This is also a risk as we might pick up dependencies we are unaware of, as the build may work locally with those packages present in GOPATH. On the other hand the build server will detect this as it has no packages in it's GOPATH beyond what is included in the repo. Recommended tool to manage dependencies is github.com/FiloSottile/gvt. |
||
---|---|---|
.. | ||
LICENSE | ||
ratelimit_test.go | ||
ratelimit.go | ||
reader.go | ||
README.md |
ratelimit
-- import "github.com/juju/ratelimit"
The ratelimit package provides an efficient token bucket implementation. See http://en.wikipedia.org/wiki/Token_bucket.
Usage
func Reader
func Reader(r io.Reader, bucket *Bucket) io.Reader
Reader returns a reader that is rate limited by the given token bucket. Each token in the bucket represents one byte.
func Writer
func Writer(w io.Writer, bucket *Bucket) io.Writer
Writer returns a writer that is rate limited by the given token bucket. Each token in the bucket represents one byte.
type Bucket
type Bucket struct {
}
Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.
func NewBucket
func NewBucket(fillInterval time.Duration, capacity int64) *Bucket
NewBucket returns a new token bucket that fills at the rate of one token every fillInterval, up to the given maximum capacity. Both arguments must be positive. The bucket is initially full.
func NewBucketWithQuantum
func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket
NewBucketWithQuantum is similar to NewBucket, but allows the specification of the quantum size - quantum tokens are added every fillInterval.
func NewBucketWithRate
func NewBucketWithRate(rate float64, capacity int64) *Bucket
NewBucketWithRate returns a token bucket that fills the bucket at the rate of rate tokens per second up to the given maximum capacity. Because of limited clock resolution, at high rates, the actual rate may be up to 1% different from the specified rate.
func (*Bucket) Rate
func (tb *Bucket) Rate() float64
Rate returns the fill rate of the bucket, in tokens per second.
func (*Bucket) Take
func (tb *Bucket) Take(count int64) time.Duration
Take takes count tokens from the bucket without blocking. It returns the time that the caller should wait until the tokens are actually available.
Note that if the request is irrevocable - there is no way to return tokens to the bucket once this method commits us to taking them.
func (*Bucket) TakeAvailable
func (tb *Bucket) TakeAvailable(count int64) int64
TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.
func (*Bucket) TakeMaxDuration
func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)
TakeMaxDuration is like Take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait.
If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.
func (*Bucket) Wait
func (tb *Bucket) Wait(count int64)
Wait takes count tokens from the bucket, waiting until they are available.
func (*Bucket) WaitMaxDuration
func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool
WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.