rate

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package rate provides a rate limiter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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.

Informally, in any large enough time interval, the Limiter limits the rate to r tokens per second, with a maximum burst size of b events.

Limiter is thread-safe.

func NewLimiter

func NewLimiter(r float64, b float64) *Limiter

NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens.

func NewLimiterWithCustomTime added in v1.1.0

func NewLimiterWithCustomTime(
	r float64, b float64, nowFn func() time.Time, sleepFn func(d time.Duration),
) *Limiter

NewLimiterWithCustomTime returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens. The limiter uses the given functions to retrieve the current time and to sleep (useful for testing).

func (*Limiter) Rate added in v1.1.0

func (l *Limiter) Rate() float64

Rate returns the current rate limit.

func (*Limiter) Remove added in v1.1.0

func (l *Limiter) Remove(n float64)

Remove removes tokens for an operation that bypassed any waiting; it can put the token bucket into debt, delaying future operations.

func (*Limiter) SetRate added in v1.1.0

func (l *Limiter) SetRate(r float64)

SetRate updates the rate limit.

func (*Limiter) Wait

func (l *Limiter) Wait(n float64)

Wait sleeps until enough tokens are available. If n is more than the burst, the token bucket will go into debt, delaying future operations.

Jump to

Keyboard shortcuts

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