Documentation
¶
Index ¶
- type Limit
- type Limiter
- func (l Limiter) Allow(ctx context.Context, key string) (*Result, error)
- func (l Limiter) AllowAtMost(ctx context.Context, key string, limit Limit, n int) (*Result, error)
- func (l Limiter) AllowN(ctx context.Context, key string, n int) (*Result, error)
- func (l *Limiter) Reset(ctx context.Context, key string) error
- func (l *Limiter) SetCustomLimit(key string, limit Limit)
- type LimiterOption
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter controls how frequently events are allowed to happen.
func NewLimiter ¶
func NewLimiter(rdb rueidis.Client, opts ...LimiterOption) *Limiter
NewLimiter returns a new Limiter.
func (Limiter) AllowAtMost ¶
func (l Limiter) AllowAtMost( ctx context.Context, key string, limit Limit, n int, ) (*Result, error)
AllowAtMost reports whether at most n events may happen at time now. It returns number of allowed events that is less than or equal to n.
func (*Limiter) SetCustomLimit ¶ added in v1.2.0
SetCustomLimit adds or updates a per-key rate limit after construction.
type LimiterOption ¶
type LimiterOption func(*Limiter)
func WithCustomLimits ¶
func WithCustomLimits(limits map[string]Limit) LimiterOption
WithCustomLimits sets per-key rate limits. These are compiled once at construction time, so per-call strconv overhead is avoided.
func WithPrefix ¶ added in v1.1.0
func WithPrefix(prefix string) LimiterOption
func WithRateLimit ¶ added in v1.0.0
func WithRateLimit(limit Limit) LimiterOption
type Result ¶
type Result struct {
// Limit is the limit that was used to obtain this result.
Limit Limit
// Allowed is the number of events that may happen at time now.
Allowed int
// Remaining is the maximum number of requests that could be
// permitted instantaneously for this key given the current
// state. For example, if a rate limiter allows 10 requests per
// second and has already received 6 requests for this key this
// second, Remaining would be 4.
Remaining int
// RetryAfter is the time until the next request will be permitted.
// It should be -1 unless the rate limit has been exceeded.
RetryAfter time.Duration
// ResetAfter is the time until the RateLimiter returns to its
// initial state for a given key. For example, if a rate limiter
// manages requests per second and received one request 200ms ago,
// Reset would return 800ms. You can also think of this as the time
// until Limit and Remaining will be equal.
ResetAfter time.Duration
}
Click to show internal directories.
Click to hide internal directories.