Documentation ¶
Overview ¶
Package counter provides functions for distributed rate limiting.
Index ¶
- Variables
- func WithFixedWindow() func(*params)
- func WithLimit(size time.Duration, limit uint, options ...func(*params)) *params
- func WithName(name string) func(*params)
- func WithRate(rate uint) func(*params)
- func WithSlidingWindow() func(*params)
- type Counter
- type Limiter
- type RedisClient
- type Result
Constants ¶
This section is empty.
Variables ¶
var ErrUnexpectedRedisResponse = errors.New("counter: unexpected redis response")
ErrUnexpectedRedisResponse is the error returned when Redis command returns response of unexpected type.
Functions ¶
func WithFixedWindow ¶ added in v0.6.0
func WithFixedWindow() func(*params)
WithFixedWindow sets fixed window algorithm for the limit.
func WithLimit ¶ added in v0.7.0
WithLimit creates parameters to build a limit.
By default a limit uses fixed window algorithm, may be set with options. Each limit is created with pseudo-random name which may be set with options. The rate of decreasing the window size on each next application of the limit by default equal 1, may be set with options.
func WithName ¶ added in v0.6.0
func WithName(name string) func(*params)
WithName sets unique name for the limit, every Redis key will be prefixed with this name.
func WithRate ¶ added in v0.6.0
func WithRate(rate uint) func(*params)
WithRate sets the rate of decreasing the window size on each next application of the limit.
func WithSlidingWindow ¶ added in v0.6.0
func WithSlidingWindow() func(*params)
WithSlidingWindow sets sliding window algorithm for the limit.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter implements distributed counter.
func FixedWindow ¶ added in v0.2.0
func FixedWindow(client RedisClient, size time.Duration, limit uint) *Counter
FixedWindow creates new counter which implements distributed counter using fixed window algorithm.
func SlidingWindow ¶ added in v0.2.0
func SlidingWindow(client RedisClient, size time.Duration, limit uint) *Counter
SlidingWindow creates new counter which implements distributed counter using sliding window algorithm.
type Limiter ¶ added in v0.5.0
type Limiter interface { // Limit applies the limit. Limit(ctx context.Context, key string) (Result, error) }
Limiter implements distributed rate limiting.
func NewLimiter ¶ added in v0.5.0
func NewLimiter(client RedisClient, first *params, rest ...*params) Limiter
NewLimiter creates new limiter which implements distributed rate limiting.
type RedisClient ¶ added in v0.2.0
type RedisClient interface { Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd ScriptLoad(ctx context.Context, script string) *redis.StringCmd }
RedisClient is redis scripter interface.
type Result ¶ added in v0.2.0
type Result struct {
// contains filtered or unexported fields
}
Result is counter value increment result.