Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
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) Request ¶
func (l NopeLimiter) Request(n uint64) bool
Request always return true.
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 two phases - The caller first requests for 'n' tokens, does some work, then 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 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 (*RunningTotalLimiter) Commit ¶
func (rl *RunningTotalLimiter) Commit(n uint64)
Commit makes the requested n tokens permanent for the configured period. It's the caller's responsibility to always request the tokens before committing them.
func (*RunningTotalLimiter) Request ¶
func (rl *RunningTotalLimiter) Request(n uint64) bool
Request reqeusts for 'n' tokens and returns if granted or not. If granted, the tokens must be either withdrawed or committed some time later, or we'll run out of tokens.
func (*RunningTotalLimiter) Withdraw ¶
func (rl *RunningTotalLimiter) Withdraw(n uint64)
Withdraw withdraws 'n' grant previously requested.