rate

package
v0.0.0-...-d5b31fc Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 4 Imported by: 0

README

Rate Limiter

a generic event rate limiter by pure go

Interface

// Limiter controls how frequently events are allowed to happen
type Limiter interface {
    Take() error // Take take one token, if met error, must be ErrNoMoreTokens

    Remains() int // how many tokens remains, always used to check if reached limit line

    Taken() int // how many tokens has been taken

    SetLimit(w time.Duration, n int) // change limit settings on fly

    String() string // print limiter text message
}

Usage

See:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoMoreTokens = errors.New("no more tokens, pls wait and retry")
)

nolint

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	Take() error // Take take one token, if met error, must be ErrNoMoreTokens

	Remains() int // how many tokens remains, always used to check if reached limit line

	Taken() int // how many tokens has been taken

	SetLimit(w time.Duration, n int) // change limit settings on fly

	String() string // print limiter text message
}

Limiter controls how frequently events are allowed to happen

func NewLimiter

func NewLimiter(w time.Duration, n int) Limiter

NewLimiter new a event limiter

Jump to

Keyboard shortcuts

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