Documentation ¶
Overview ¶
Package ratelimit implements a rate limiter.
Index ¶
Constants ¶
View Source
const NotFound = -1
NotFound will be returned if it fails to get value.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter implements a token bucket limiter at rate `r` tokens per second with burst size of `b` tokens.
type SlidingWindowLimiter ¶
type SlidingWindowLimiter struct {
// contains filtered or unexported fields
}
SlidingWindowLimiter implements a limiter with sliding window counter.
func NewSlidingWindowLimiter ¶
func NewSlidingWindowLimiter(r Limit, expire int, store Store) *SlidingWindowLimiter
NewSlidingWindowLimiter returns a new Limiter at rate `r` tokens per second, and the key expires in `expire` seconds.
func (*SlidingWindowLimiter) Allow ¶
func (s *SlidingWindowLimiter) Allow(key string) bool
Allow is the shortcut for AllowN(time.Now(), key, 1).
type Store ¶
type Store interface { // Incr add `increment` to field `timestamp` in `key` Incr(key string, timestamp int64, increment int) error // SetIncr set `key` and add `increment` to field `timestamp` in `key` SetIncr(key string, timestamp int64, increment int) error // Expire set `key` to expire in `expire` seconds Expire(key string, expire int) error // Get returns value of field `timestamp` in `key` Get(key string, timestamp int64) int // Exists check if `key` exists Exists(key string) bool }
Store represents a store for limiter state.
Click to show internal directories.
Click to hide internal directories.