rate

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 5 modules, 8 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

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
}

Limiter is a simple rate limiter to control how frequently events are allowed to happen. It trades some accuracy on resets for speed.

func NewLimiter

func NewLimiter(l int64) *Limiter

NewLimiter creates a new rate limiter.

func (*Limiter) IsAllowed

func (l *Limiter) IsAllowed(n int64, now xtime.UnixNano) bool

IsAllowed returns whether n events may happen now. NB(xichen): If a large request comes in, this could potentially block following requests in the same second from going through. This is a non-issue if the limit is much bigger than the typical batch size, which is indeed the case in the aggregation layer as each batch size is usually fairly small. The limiter is racy on window changes, and can be overly aggressive rejecting requests. As the limits are usually at least 10k+, the error is worth the speedup.

func (*Limiter) Limit

func (l *Limiter) Limit() int64

Limit returns the current limit. A zero limit means no limit is set.

func (*Limiter) Reset

func (l *Limiter) Reset(limit int64)

Reset resets the internal state. It does not reset all the values atomically, but it should not be a problem, as dynamic rate limits in aggregator are usually reset under a lock.

Jump to

Keyboard shortcuts

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