ratelimiter

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LimitStatus

type LimitStatus struct {
	// IsLimited is true when a given key should be rate-limited
	IsLimited bool
	// LimitDuration is not nil when IsLimited is true. It's the time for which a given key should be blocked before CurrentRate falls below declared in constructor requests limit
	LimitDuration *time.Duration
	// CurrentRate is approximated current requests rate per window size (declared in the constructor)
	CurrentRate float64
}

LimitStatus represents current status of limitation for a given key

type LimitStore

type LimitStore interface {
	// Inc increments current window limit counter for key
	Inc(key string, window time.Time) error
	// Inc increments current window limit counter for key
	Dec(key string, count int64, window time.Time) error
	// Get gets value of previous window counter and current window counter for key
	Get(key string, previousWindow, currentWindow time.Time) (prevValue, currValue int64, err error)
}

LimitStore is the interface that represents limiter internal data store. Any database struct that implements LimitStore should have functions for incrementing counter of a given key and getting counter values of a given key for previous and current window

type MapLimitStore

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

MapLimitStore represents internal limiter data database where data are stored in golang maps

func NewMapLimitStore

func NewMapLimitStore(expirationTime, flushInterval time.Duration) (m *MapLimitStore)

NewMapLimitStore creates new in-memory data store for internal limiter data. Each element of MapLimitStore is set as expired after expirationTime from its last counter increment. Expired elements are removed with a period specified by the flushInterval argument

func (*MapLimitStore) Dec

func (m *MapLimitStore) Dec(key string, count int64, window time.Time) error

Dec decrements current window limit counter for key

func (*MapLimitStore) Get

func (m *MapLimitStore) Get(key string, previousWindow, currentWindow time.Time) (prevValue, currValue int64, err error)

Get gets value of previous window counter and current window counter for key

func (*MapLimitStore) Inc

func (m *MapLimitStore) Inc(key string, window time.Time) error

Inc increments current window limit counter for key

func (*MapLimitStore) Size

func (m *MapLimitStore) Size() int

Size returns current length of data map

type RateLimiter

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

RateLimiter is a simple rate-limiter for any resources inspired by Cloudflare's approach: https://blog.cloudflare.com/counting-things-a-lot-of-different-things/

func New

func New(dataStore LimitStore, requestsLimit int64, windowSize time.Duration) *RateLimiter

New creates new rate limiter. A dataStore is internal limiter data store, requestsLimit and windowSize are parameters of limiter e.g. requestsLimit: 5 and windowSize: 1*time.Minute means that limiter allows up to 5 requests per minute

func (*RateLimiter) Check

func (r *RateLimiter) Check(key string, currentTime time.Time) (limitStatus *LimitStatus, err error)

Check checks status of rate-limiting for a key. It returns error when limiter data could not be read

func (*RateLimiter) Dec

func (r *RateLimiter) Dec(key string, count int64, currentTime time.Time) error

Dec decrements limiter counter for a given key or returns error when it's not possible

func (*RateLimiter) Inc

func (r *RateLimiter) Inc(key string, currentTime time.Time) error

Inc increments limiter counter for a given key or returns error when it's not possible

Jump to

Keyboard shortcuts

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