Documentation
¶
Overview ¶
Package redis provides a Redis-backed ratelimit.Store using atomic Lua scripts, for distributed rate limiting across multiple instances.
Index ¶
- type Store
- func (s *Store) Close() error
- func (s *Store) GiveBackWindow(ctx context.Context, key string) error
- func (s *Store) IncrWindow(ctx context.Context, key string, window time.Duration) (int64, error)
- func (s *Store) PeekWindow(ctx context.Context, key string) (int64, bool, error)
- func (s *Store) Ping(ctx context.Context) error
- func (s *Store) TakeTokens(ctx context.Context, key string, rate, burst, n int) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a Redis-backed Store.
func NewFromAddr ¶
NewFromAddr returns a Store backed by a client connecting to addr.
func (*Store) GiveBackWindow ¶ added in v0.0.6
GiveBackWindow implements ratelimit.WindowGiver: return one count to the window, never below zero.
The decrement is its own script rather than a read-and-set so that two replicas giving a count back at the same moment cannot lose one of the two. DECR keeps the TTL, which is what makes a give-back land in the window it was charged in rather than extending it.
func (*Store) IncrWindow ¶
IncrWindow implements ratelimit.Store.
func (*Store) PeekWindow ¶ added in v0.0.6
PeekWindow implements ratelimit.WindowReader: the window counter as it stands.
A missing key is "no requests counted in this window", which is what the counter's own TTL makes it: IncrWindow sets the expiry on the first increment, so a key that is gone is a window that ended.