rate

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 4 Imported by: 4

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