Documentation ¶
Overview ¶
Package circularbuffer designed to be a solution for storing rate limit data as described in https://docs.google.com/document/d/19BqdCancXavg4dqrFey-brM-VWCoTvqMHS1Gmn_jsC0 and https://github.com/zalando/skipper/issues/424
Index ¶
- type CircularBuffer
- func (cb *CircularBuffer) Add(t time.Time) bool
- func (cb *CircularBuffer) Allow(s string) booldeprecated
- func (cb *CircularBuffer) AllowContext(ctx context.Context, s string) bool
- func (cb *CircularBuffer) Cap() int
- func (*CircularBuffer) Close()
- func (cb *CircularBuffer) Current(string) time.Time
- func (cb *CircularBuffer) Delta(string) time.Duration
- func (cb *CircularBuffer) Free() bool
- func (cb *CircularBuffer) InUse() bool
- func (cb *CircularBuffer) Len() int
- func (cb *CircularBuffer) Next() time.Time
- func (cb *CircularBuffer) Oldest(string) time.Time
- func (cb *CircularBuffer) Resize(_ string, n int)
- func (cb *CircularBuffer) RetryAfter(string) int
- type ClientRateLimiter
- func (rl *ClientRateLimiter) Allow(s string) booldeprecated
- func (rl *ClientRateLimiter) AllowContext(ctx context.Context, s string) bool
- func (rl *ClientRateLimiter) Close()
- func (rl *ClientRateLimiter) Current(s string) time.Time
- func (rl *ClientRateLimiter) DeleteOld()
- func (rl *ClientRateLimiter) Delta(s string) time.Duration
- func (rl *ClientRateLimiter) Oldest(s string) time.Time
- func (rl *ClientRateLimiter) Resize(s string, n int)
- func (rl *ClientRateLimiter) RetryAfter(s string) int
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CircularBuffer ¶
CircularBuffer has slots to store times as int64 and an offset, which marks the next free entry. Slots are fixed in size.
func NewCircularBuffer ¶
func NewCircularBuffer(l int, t time.Duration) *CircularBuffer
func (*CircularBuffer) Add ¶
func (cb *CircularBuffer) Add(t time.Time) bool
Add adds an element to the next free bucket in the buffer and returns true. It returns false if there is no free bucket. Example
[_ _ _ _] ^ [1 _ _ _] ^ [1 2 _ _] ^ [1 2 3 _] ^ [1 2 3 4] ^
func (*CircularBuffer) Allow
deprecated
added in
v0.2.0
func (cb *CircularBuffer) Allow(s string) bool
Allow returns true if there is a free bucket and we should not rate limit, if not it will return false, which means ratelimit.
Deprecated: In favour of AllowContext
func (*CircularBuffer) AllowContext ¶ added in v0.7.2
func (cb *CircularBuffer) AllowContext(ctx context.Context, s string) bool
Allow returns true if there is a free bucket and we should not rate limit, if not it will return false, which means ratelimit.
func (*CircularBuffer) Cap ¶
func (cb *CircularBuffer) Cap() int
func (*CircularBuffer) Close ¶ added in v0.2.0
func (*CircularBuffer) Close()
Close implements the RateLimiter interface to shutdown, nothing to do.
func (*CircularBuffer) Current ¶ added in v0.5.0
func (cb *CircularBuffer) Current(string) time.Time
Current implements the RateLimiter interface
func (*CircularBuffer) Delta ¶ added in v0.3.0
func (cb *CircularBuffer) Delta(string) time.Duration
Delta returns the diffence between the current and the oldest value in the buffer, i.e. maxHits / Delta() => rate
func (*CircularBuffer) Free ¶
func (cb *CircularBuffer) Free() bool
Free returns if there is space or the bucket is full for the current time. Example:
time.Now(): 5 timeWindow: 2 [1 2 3 4] ^ 5-2 = 3 --> 2 free slots [1,2] are too old and are Free already
func (*CircularBuffer) InUse ¶
func (cb *CircularBuffer) InUse() bool
func (*CircularBuffer) Len ¶
func (cb *CircularBuffer) Len() int
func (*CircularBuffer) Next ¶ added in v0.5.0
func (cb *CircularBuffer) Next() time.Time
func (*CircularBuffer) Oldest ¶ added in v0.7.0
func (cb *CircularBuffer) Oldest(string) time.Time
Oldest implements the RateLimiter interface
func (*CircularBuffer) Resize ¶ added in v0.4.0
func (cb *CircularBuffer) Resize(_ string, n int)
Resize resizes the circular buffer to the given size. Resizing to a size <= 0 is not performed
func (*CircularBuffer) RetryAfter ¶ added in v0.5.0
func (cb *CircularBuffer) RetryAfter(string) int
RetryAfter returns how many seconds one should wait until the next request is allowed.
type ClientRateLimiter ¶ added in v0.2.0
ClientRateLimiter implements the RateLimiter interface and does rate limiting based on the the String passed to Allow(). This can be used to limit per client calls to the backend. For example you can slow down user enumeration or dictionary attacks to /login APIs.
func NewClientRateLimiter ¶ added in v0.2.0
func NewClientRateLimiter(maxHits int, d, cleanInterval time.Duration) *ClientRateLimiter
NewRateLimiter returns a new initialized RateLimitter with maxHits is the maximal number of hits per time.Duration d.
func (*ClientRateLimiter) Allow
deprecated
added in
v0.2.0
func (rl *ClientRateLimiter) Allow(s string) bool
Allow tries to add s to a circularbuffer and returns true if we have a free bucket, if not it will return false, which means ratelimit.
Deprecated: In favour of allow context
func (*ClientRateLimiter) AllowContext ¶ added in v0.7.2
func (rl *ClientRateLimiter) AllowContext(ctx context.Context, s string) bool
AllowContext tries to add s to a circularbuffer and returns true if we have a free bucket, if not it will return false, which means ratelimit with an additional context.Context.
func (*ClientRateLimiter) Close ¶ added in v0.2.0
func (rl *ClientRateLimiter) Close()
Close will stop the cleanup goroutine
func (*ClientRateLimiter) Current ¶ added in v0.6.0
func (rl *ClientRateLimiter) Current(s string) time.Time
func (*ClientRateLimiter) DeleteOld ¶ added in v0.2.0
func (rl *ClientRateLimiter) DeleteOld()
DeleteOld removes old entries from state bag
func (*ClientRateLimiter) Delta ¶ added in v0.3.0
func (rl *ClientRateLimiter) Delta(s string) time.Duration
Delta returns the diffence between the current and the oldest value in the buffer, i.e. maxHits / Delta() => rate
func (*ClientRateLimiter) Oldest ¶ added in v0.7.0
func (rl *ClientRateLimiter) Oldest(s string) time.Time
func (*ClientRateLimiter) Resize ¶ added in v0.4.0
func (rl *ClientRateLimiter) Resize(s string, n int)
Resize resizes the given circular buffer to the given size. Resizing to a size <= 0 is not performed
func (*ClientRateLimiter) RetryAfter ¶ added in v0.5.0
func (rl *ClientRateLimiter) RetryAfter(s string) int
RetryAfter returns how many seconds one should wait until the next request is allowed.
type RateLimiter ¶
type RateLimiter interface { // Allow returns true if call should be allowed, false in case // you should rate limit. // // Deprecated: In favour of AllowContext Allow(string) bool // AllowContext is like Allow but accepts an additional // context.Context, e.g. to support OpenTracing. AllowContext(context.Context, string) bool // Close cleans up the RateLimiter implementation. Close() Oldest(string) time.Time Delta(string) time.Duration Resize(string, int) // RetryAfter returns how many seconds until the next allowed request RetryAfter(string) int }
RateLimiter is an interface which can be used to implement rate limiting.
func NewRateLimiter ¶
func NewRateLimiter(maxHits int, d time.Duration) RateLimiter
NewRateLimiter returns a new initialized RateLimitter with maxHits as the maximal number of hits per time.Duration d. This can be used to implement maximum number of requests for a backend to protect from a known scaling limit.