faregate: github.com/matttproud/faregate Index | Examples | Files | Directories

package faregate

import "github.com/matttproud/faregate"

Package faregate provides a token bucket load shaper.


package main

import (

func Must(c <-chan struct{}, err error) <-chan struct{} {
    if err != nil {
    return c

func main() {
    rnd := rand.New(rand.NewSource(42))

    fg, err := New(RefreshInterval(time.Second), TokenCount(100), ConcurrencyLevel(1))
    if err != nil {
    defer fg.Close()

    for {
        q := rnd.Intn(10)
        fmt.Println("acquired", q)



Package Files


func ConcurrencyLevel Uses

func ConcurrencyLevel(l uint8) optionFn

ConcurrencyLevel sets how many concurrent token operations to support without blocking.

func RefreshInterval Uses

func RefreshInterval(i time.Duration) optionFn

RefreshInterval sets how frequently the bucket's tokens are refreshed.

func TokenCount Uses

func TokenCount(c uint64) optionFn

TokenCount sets how large the bucket is.

type Faregate Uses

type Faregate struct {
    // contains filtered or unexported fields

Faregate is a token bucket load shaper.

func New Uses

func New(opts ...optionFn) (*Faregate, error)

New creates a Faregate from the options. It must be closed when no longer needed to prevent leakage of goroutines.

func (*Faregate) Acquire Uses

func (f *Faregate) Acquire(n uint64) (chan struct{}, error)

Acquire acquires n tokens from the faregate. It returns a channel whose closure indicates when tokens were acquired. Token requests are typically answered fairly. If there is backlogging of tokens requests, the fairness guarantees break down.

func (*Faregate) Close Uses

func (f *Faregate) Close()

Close stops the Faregate. It should be called only once.


cmd/exampleExample is a HTTP server that respondes to requests on /limited according to the provided token bucket policy.

Package faregate imports 2 packages (graph) and is imported by 1 packages. Updated 2016-07-17. Refresh now. Tools for package owners.