bucket

package
v0.0.0-...-5a170ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2016 License: Apache-2.0 Imports: 2 Imported by: 8

Documentation

Overview

bucket implements a generic, embeddable token bucket algorithm.

Index

Constants

View Source
const (
	TPS       Rate = 1
	KTPS           = 1000 * TPS
	MTPS           = 1000 * KTPS
	GTPS           = 1000 * MTPS
	Unlimited      = 0
)

Common rates.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Represents a token bucket. It is not thread safe.

func New

func New(rate Rate, max uint64) (bucket *Bucket)

NewBucket creates a new bucket with the given rate and the maximum bucket size.

If rate is Unlimited, it returns a nil pointer and it is safe to call all Bucket methods on that nil pointer:

b := bucket.NewBucket(Unlimited)
b.Has(10)
b.Get(10)

func (*Bucket) Get

func (b *Bucket) Get(tokens uint64) (ok bool)

Get allocates t tockens from the buffer if available, or otherwise returns false.

func (*Bucket) Has

func (b *Bucket) Has(tokens uint64) (has bool)

Has returns whether the bucket has at least t tocken.

func (*Bucket) Max

func (b *Bucket) Max() uint64

Max returns the maximum number of tokens that this bucket can store.

func (*Bucket) Reset

func (b *Bucket) Reset()

Reset reset the bucket to an empty bucket and starts adding tokens from now.

func (*Bucket) Unlimited

func (b *Bucket) Unlimited() bool

Unlimited returns whether the bucket is unlimited.

func (*Bucket) When

func (b *Bucket) When(tokens uint64) (dur time.Duration)

When returns the minimum time to wait before enough tokens are available.

type Rate

type Rate uint64

Rate represents the rate of token generation per second.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL