rate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func (l *Limiter) Rate() float64

Rate returns the current rate limit.

func (*Limiter) Remove

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

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