Documentation ¶
Overview ¶
Package ratelimiter is a ratelimiter based on cloudflare's approach
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 the time for which a given key should be blocked LimitDuration *time.Duration // CurrentRate is approximated current requests rate per window size 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 // Get gets value of previous window counter and current window counter for key Get(key string, previousWindow, currentWindow time.Time) (prevValue int64, currValue int64, err error) }
LimitStore is the interface that represents limiter internal data store
type MapLimitStore ¶
type MapLimitStore struct {
// contains filtered or unexported fields
}
MapLimitStore represents a data structure for in-memory storage of ratelimiter information
func NewMapLimitStore ¶
func NewMapLimitStore(expirationTime time.Duration, flushInterval time.Duration) (m *MapLimitStore)
NewMapLimitStore creates new in-memory data store for internal limiter data
func (*MapLimitStore) Get ¶
func (m *MapLimitStore) Get(key string, previousWindow, currentWindow time.Time) (prevValue int64, currValue int64, err error)
Get gets value of previous window counter and current window counter
func (*MapLimitStore) Inc ¶
func (m *MapLimitStore) Inc(key string, window time.Time) error
Inc increments current window limit counter
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 defining the structure of a rate limiter object
func New ¶
func New(dataStore LimitStore, requestsLimit int64, windowSize time.Duration) *RateLimiter
New creates new rate limiter
func (*RateLimiter) Check ¶
func (r *RateLimiter) Check(key string) (limitStatus *LimitStatus, err error)
Check checks status of the rate limit key
func (*RateLimiter) Inc ¶
func (r *RateLimiter) Inc(key string) error
Inc increments the limit counter for a given key