ratelimitutil

package
v0.0.0-...-1593278 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultWindow      = 10 * time.Second
	DefaultGranularity = 1 * time.Second
	DefaultAvgDuration = 3 * time.Second
)

Inf is the infinite rate limit; it allows all events.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit float64

Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.

func (Limit) String

func (limit Limit) String() string

String returns string of Limit.

type Limiter

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

A Limiter controls how frequently events are allowed to happen. It implements a "token bucket" of size b, initially full and refilled at rate r tokens per second. See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.

However, Limiter is a little different from token-bucket. It is based on a special punishment mechanism, a very large number of events are allowed as long as the number of tokens in bucket is greater or equal to 0. After these large number of events toke tokens from bucket, the number of tokens in bucket may be negative, and the latter events would be "punished", any event should wait for the tokens to be filled to greater or equal to 0.

func NewLimiter

func NewLimiter(r Limit, b float64) *Limiter

NewLimiter returns a new Limiter that allows events up to rate r.

func (*Limiter) AllowN

func (lim *Limiter) AllowN(now time.Time, n int) bool

AllowN reports whether n events may happen at time now.

func (*Limiter) Limit

func (lim *Limiter) Limit() Limit

Limit returns the maximum overall event rate.

func (*Limiter) SetLimit

func (lim *Limiter) SetLimit(newLimit Limit)

SetLimit sets a new Limit for the limiter.

type RateCollector

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

RateCollector helps to collect and calculate values (like throughput, QPS, TPS, etc...), It implements a sliding window with custom size and granularity to store values.

func NewRateCollector

func NewRateCollector(window time.Duration, granularity time.Duration) (*RateCollector, error)

NewRateCollector is shorthand for newRateCollector(window, granularity, time.Now()).

func (*RateCollector) Add

func (r *RateCollector) Add(label string, value float64)

Add is shorthand for add(label, value, time.Now()).

func (*RateCollector) Deregister

func (r *RateCollector) Deregister(label string)

Deregister remove values of RateCollector for specified label.

func (*RateCollector) Max

func (r *RateCollector) Max(label string, now time.Time) (float64, error)

Max is shorthand for max(label, time.Now()).

func (*RateCollector) Min

func (r *RateCollector) Min(label string, now time.Time) (float64, error)

Min is shorthand for min(label, time.Now()).

func (*RateCollector) Rate

func (r *RateCollector) Rate(label string, duration time.Duration) (float64, error)

Rate is shorthand for rate(label, duration, time.Now()).

func (*RateCollector) Register

func (r *RateCollector) Register(label string)

Register init values of RateCollector for specified label.

Jump to

Keyboard shortcuts

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