faregate

package module
v0.0.0-...-a56b909 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2015 License: Apache-2.0 Imports: 2 Imported by: 0

README

General

Package faregate provides a token bucket load shaper.

This is a toy package for experimentation purposes, not meant for anything serious.

Documentation

GoDoc

Testing

Build Status

License

Apache License Version 2.0

Documentation

Overview

Package faregate provides a token bucket load shaper.

Example
package main

import (
	"fmt"
	"math/rand"
	"time"
)

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

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

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

	for {
		q := rnd.Intn(10)
		<-Must(fg.Acquire(uint64(q)))
		fmt.Println("acquired", q)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcurrencyLevel

func ConcurrencyLevel(l uint8) optionFn

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

func RefreshInterval

func RefreshInterval(i time.Duration) optionFn

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

func TokenCount

func TokenCount(c uint64) optionFn

TokenCount sets how large the bucket is.

Types

type Faregate

type Faregate struct {
	// contains filtered or unexported fields
}

Faregate is a token bucket load shaper.

func New

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

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

func (f *Faregate) Close()

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

Directories

Path Synopsis
cmd
example
Example is a HTTP server that respondes to requests on /limited according to the provided token bucket policy.
Example is a HTTP server that respondes to requests on /limited according to the provided token bucket policy.

Jump to

Keyboard shortcuts

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