rate

package
v0.0.0-...-38e557e Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 2 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	// Run begins emitting tokens for the ratelimiter.
	// A call to Run must be followed by a call to Close.
	Run()
	// Performed consumes one token from the rate limiter.
	// If no tokens are available, the call will block until one is.
	Performed()
	// Close stops the rate limiter. Any future calls to Performed() will block forever.
	// Close never returns an error.
	io.Closer
}

A Limiter supports a constant number of Performed() calls every time a certain amount of time passes.

Calls to Performed() when no "action tokens" are available will block until one is available.

func NewRateLimit

func NewRateLimit(count int, period time.Duration) Limiter

Construct a new Limiter with the given count and duration.

Example
rl := NewRateLimit(100, 1*time.Minute)
go rl.Run()
defer rl.Close()

for _, v := range exampleData {
	rl.Performed()
	// do something with v
	_ = v
}
Output:

func Unlimited

func Unlimited() Limiter

Unlimited returns a Limiter that never blocks. The Run() and Close() calls are no-ops.

Jump to

Keyboard shortcuts

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