ratelimit

package
v1.0.1004 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(time.Duration)
}

Clock is the minimum necessary interface to instantiate a rate limiter with a clock or mock clock, compatible with clocks created using github.com/andres-erbsen/clock.

type Config

type Config struct {
	ID          string
	Alias       string
	Type        string
	Max         float64
	Current     int64
	EveryMinute bool
	AutoRemove  bool
}

type Limiter

type Limiter interface {
	// Take should block to make sure that the RPS is met.
	Take() time.Time
	FixedTake()
	Current() int64
	Done()
}

Limiter is used to rate-limit some process, possibly across goroutines. The process is expected to call Take() before every iteration, which may block to throttle the goroutine.

func New

func New(cfg Config, rate int, rep func(Config, int64), opts ...Option) Limiter

New returns a Limiter that will limit to the given RPS.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Limiter.

var WithoutSlack Option = slackOption(0)

WithoutSlack configures the limiter to be strict and not to accumulate previously "unspent" requests for future bursts of traffic.

func Per

func Per(per time.Duration) Option

Per allows configuring limits for different time windows.

The default window is one second, so New(100) produces a one hundred per second (100 Hz) rate limiter.

New(2, Per(60*time.Second)) creates a 2 per minute rate limiter.

func WithClock

func WithClock(clock Clock) Option

WithClock returns an option for ratelimit.New that provides an alternate Clock implementation, typically a mock Clock for testing.

func WithSlack

func WithSlack(slack int) Option

WithSlack configures custom slack. Slack allows the limiter to accumulate "unspent" requests for future bursts of traffic.

Jump to

Keyboard shortcuts

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