Documentation ¶
Index ¶
Constants ¶
const Inf = Limit(math.MaxInt64)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter controls how frequently events are allowed to happen.
RateLimiter has two main method: Allow and Wait
If the count of token in bucket is greater or equal to the 1 in a period time, Allow returns false
If the count of token in bucket is greater or equal to the limit in a period time, Wait blocks until the count of token in bucket is less than the 1 or its associated context.Context is canceled.
func NewRateLimiter ¶
func NewRateLimiter(client *redis.Client, limit Limit, actionKey string, period time.Duration) *RateLimiter
NewRateLimiter returns a new RateLimiter that add a token into the bucket every period and the size of its bucket is limit
func (*RateLimiter) Allow ¶
func (limiter *RateLimiter) Allow(ctx context.Context) bool
Allow is shorthand for AllowN(ctx, 1).
func (*RateLimiter) AllowN ¶
func (limiter *RateLimiter) AllowN(ctx context.Context, n int64) bool
AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip action that exceed the rate limit. Otherwise use Wait.