ratelimiter

package
v1.20210104.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ratelimiter implements two common rate limiters; queue and token/leaky bucket.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LeakyBucket

type LeakyBucket struct {
	NumActions int
	Quantum    time.Duration
	Tokens     map[string]*Token
	Now        func() time.Time
}

LeakyBucket implements the token bucket rate limiting algorithm.

func NewLeakyBucket

func NewLeakyBucket(numActions int, quantum time.Duration) *LeakyBucket

NewLeakyBucket returns a new token bucket rate limiter. The rate is formed by `numActions` and `quantum`; the resulting rate is numActions/quantum.

func (*LeakyBucket) Check

func (lb *LeakyBucket) Check(id string) bool

Check returns true if an id has exceeded the rate limit, and false otherwise.

type Queue

type Queue struct {
	NumberOfActions int
	Quantum         time.Duration
	Limits          map[string]collections.Queue
	Now             func() time.Time
}

Queue is a simple implementation of a rate checker.

func NewQueue

func NewQueue(numberOfActions int, quantum time.Duration) *Queue

NewQueue returns a new queue based rate limiter.

func (*Queue) Check

func (q *Queue) Check(id string) bool

Check returns true if it has been called NumberOfActions times or more in Quantum or smaller duration.

type RateLimiter

type RateLimiter interface {
	// Check returns for a given id `true` if that id is _above_ the rate limit, and false otherwise.
	Check(string) bool
}

RateLimiter is a type that can be used as a rate limiter.

type Token

type Token struct {
	Count float64   // the rate adjusted count; initialize at max*rate, remove rate tokens per call
	Last  time.Time // last is used to calculate the elapsed, and subsequently the rate
}

Token is an individual id's work.

Jump to

Keyboard shortcuts

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