redisrate

package
v2.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2023 License: MIT, BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGcraLimiter

func NewGcraLimiter(rdb Rediser, key string, r float64, period time.Duration, b int) ratelimit.Limiter

NewGcraLimiter returns a new Limiter.

func NewGcraLimiterLimit

func NewGcraLimiterLimit(rdb Rediser, key string, l ratelimit.Limit) ratelimit.Limiter

NewGcraLimiterLimit returns a new Limiter.

func NewGcraLimiterLimitFn

func NewGcraLimiterLimitFn(rdb Rediser, key string, fn LimitFn) ratelimit.Limiter

NewGcraLimiterLimitFn returns a new Limiter.

Types

type GcraLimiter

type GcraLimiter struct {
	// contains filtered or unexported fields
}

func (*GcraLimiter) Allow

func (gl *GcraLimiter) Allow() bool

func (*GcraLimiter) AllowCtx

func (gl *GcraLimiter) AllowCtx(ctx context.Context) bool

func (*GcraLimiter) AllowE

func (gl *GcraLimiter) AllowE() (bool, error)

func (*GcraLimiter) AllowECtx

func (gl *GcraLimiter) AllowECtx(ctx context.Context) (bool, error)

func (*GcraLimiter) AllowN

func (gl *GcraLimiter) AllowN(ctx context.Context, n int) (res *Result, err error)

AllowN reports whether n events may happen at time now.

func (*GcraLimiter) ReserveE

func (gl *GcraLimiter) ReserveE() (time.Duration, bool, error)

func (*GcraLimiter) ReserveECtx

func (gl *GcraLimiter) ReserveECtx(ctx context.Context) (time.Duration, bool, error)

func (*GcraLimiter) Reset

func (gl *GcraLimiter) Reset(ctx context.Context) error

Reset gets a key and reset all limitations and previous usages

func (*GcraLimiter) Wait

func (gl *GcraLimiter) Wait(ctx context.Context) error

Wait you'd better pass a timeout or cancelable context

type LimitFn

type LimitFn func(ctx context.Context) ratelimit.Limit

type Rediser

type Rediser 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
	Del(ctx context.Context, keys ...string) *redis.IntCmd
}

type Result

type Result struct {
	// Limit is the limit that was used to obtain this result.
	Limit ratelimit.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
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL