Documentation ¶
Overview ¶
Package fixed_window_counter provides a rate limiter implemented by sliding window counter algorithm.
Index ¶
Constants ¶
const Inf = Limit(math.MaxInt64)
Inf is the infinite rate limit; it allows all events.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limit ¶
type Limit int64
Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter controls how frequently events are allowed to happen. The zero value is a valid Limiter, but it will reject all events.
RateLimiter has two main method: Allow and Wait
If the count of actionKey is greater or equal to the limit in a period time, Allow returns false
If the count of actionKey is greater or equal to the limit in a period time, Wait blocks until the count of actionKey is less than the limit or its associated context.Context is canceled.
func NewRateLimiter ¶
func NewRateLimiter(client *redis.Client, limit Limit, actionKey string, period time.Duration, interval time.Duration) *RateLimiter
NewRateLimiter returns a new RateLimiter that allows the action up to happening limit times in a period time.