limiter

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	Request(id string, n uint64, period time.Duration) bool
	Secure(id string)
	Commit(id string)
	Withdraw(id string)
}

Limiter is the interface for RunningTotalLimiter. It's there just so we can have a NopeLimiter which does nothing.

type NopeLimiter

type NopeLimiter struct{}

NopeLimiter does no limit.

func (NopeLimiter) Commit

func (l NopeLimiter) Commit(string)

Commit does nothing.

func (NopeLimiter) Request

func (l NopeLimiter) Request(string, uint64, time.Duration) bool

Request always return true.

func (NopeLimiter) Secure added in v0.0.7

func (l NopeLimiter) Secure(string)

Secure does nothing.

func (NopeLimiter) Withdraw

func (l NopeLimiter) Withdraw(string)

Withdraw does nothing.

type RunningTotalLimiter

type RunningTotalLimiter struct {
	// contains filtered or unexported fields
}

RunningTotalLimiter is a variant of sliding log rate limiter. It keeps all requests with timestamps. What makes it different from a typical rate limiter is that there are three phases - The caller first requests for pre approval of 'n' tokens for a period of time, then secures it before it expires, does some work, and finally either commits the tokens, or withdraws them so they can be requested by others. So at anytime, the running total represents the tokens committed in that period of time, plus those requested but not expired or committed yet.

If we are going to have millions of requests, we should switch to a sliding window implementation like https://github.com/RussellLuo/slidingwindow, which saves space with the cost of some inaccuracy.

func NewRunningTotalLimiter

func NewRunningTotalLimiter(limit uint64, period time.Duration) *RunningTotalLimiter

NewRunningTotalLimiter creates a Limiter which caps the running total in the 'period' to 'limit'.

func (*RunningTotalLimiter) Commit

func (rl *RunningTotalLimiter) Commit(id string)

Commit makes the not-yet-expired or secured tokens associated with the id permanent for the configured period.

func (*RunningTotalLimiter) Request

func (rl *RunningTotalLimiter) Request(id string, n uint64, expiration time.Duration) bool

Request reqeusts for n tokens and returns if granted or not. If granted, the tokens must be secured before expiration.

func (*RunningTotalLimiter) Secure added in v0.0.7

func (rl *RunningTotalLimiter) Secure(id string)

Secure secures previously requested tokens associated with the id so they don't expire automatically.

func (*RunningTotalLimiter) Withdraw

func (rl *RunningTotalLimiter) Withdraw(id string)

Withdraw withdraws the tokens associated with the id.

Jump to

Keyboard shortcuts

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