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) Secure ¶ added in v0.0.7
func (l NopeLimiter) Secure(string)
Secure 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 ¶
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.