ratelimit

package
v0.0.0-...-719b004 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RedisReqPerMinPattern             string = "dynlimits_reqpermin_%s"
	RedisSlidingCountersWindowPattern string = "dynlimits_scw_%d_%s"
)

Variables

This section is empty.

Functions

func AddToRedisSlidingCountersWindow

func AddToRedisSlidingCountersWindow(conn redis.Conn, key string,
	timestampSec int64) error

AdToRedisSlidingCountersWindow increments the counters for a a give second timestamp

func NewRedisPool

func NewRedisPool(conf *RedisPoolConf) *redis.Pool

NewRedisPool creates a redis pool to be used as the storage for rate limit data

func SetRedisRateLimit

func SetRedisRateLimit(conn redis.Conn, key string, reqPerMin int64) error

SetRedisRateLimit updates the number of allowed requests per minute for a given key

Types

type InMemRateLimit

type InMemRateLimit struct {
	TimestampSec int64
	StartIdx     int64
	Window       [60]int64
	Sum          int64
	ReqPerMin    int64
}

InMemRateLimit implements a circular buffer with the requests performed in each second of the last minute

func NewInMemRateLimit

func NewInMemRateLimit(reqPerMin int64) *InMemRateLimit

NewInMemRateLimit creates a new InMemRateLimit structure to keep track of a rate limit

func (*InMemRateLimit) GetSlidingCountersWindow

func (imrl *InMemRateLimit) GetSlidingCountersWindow(
	atTimestampSec int64) *SlidingCountersWindow

GetSlinginCountersWindows creates a new SlidingCounterWindow for a given timestamp in seconds.

func (*InMemRateLimit) Inc

func (imrl *InMemRateLimit) Inc(timestampSec int64)

Inc increments the given

func (*InMemRateLimit) NumEmptySlotsAtStart

func (imrl *InMemRateLimit) NumEmptySlotsAtStart() int

NumEmptySlotsAtStarts gives the number of empty seconds at the start of the counting window that is the same than the minimum number of seconds that must pass before new requests are added to the minute window

func (*InMemRateLimit) UpdateSlidingCountersWindow

func (imrl *InMemRateLimit) UpdateSlidingCountersWindow(toUpdate *SlidingCountersWindow,
	timestampSec int64)

UpdateSlidingCountersWindow gets an SlidingCountersWindows from the InMemRateLimit at a given current timestamp

type RedisPoolConf

type RedisPoolConf struct {
	Address          string
	MaxIdleMinutes   int
	ConnectTimeoutMs int64
	ReadTimeoutMs    int64
}

RedisPoolConf contains the params to configure a redispool to be used as the source of ratelimit data

func NewRedisPoolConf

func NewRedisPoolConf(address string) *RedisPoolConf

NewRedisPoolConf creates a new redis pool configuration with default values

type SlidingCountersWindow

type SlidingCountersWindow struct {
	TimestampSec int64
	Window       [60]int64
	Sum          int64
	ReqPerMin    int64
}

func GetRedisSlidingCountersWindow

func GetRedisSlidingCountersWindow(conn redis.Conn, key string,
	timestampSec int64) (*SlidingCountersWindow, error)

GetRedisSlidingCountersWindow returns an SlidingCountersWindow for a given Key

func (*SlidingCountersWindow) NumEmptySlotsAtStart

func (scw *SlidingCountersWindow) NumEmptySlotsAtStart() int

NumEmptySlotsAtStarts gives the number of empty seconds at the start of the counting window that is the same than the minimum number of seconds that must pass before new requests are added to the minute window

func (*SlidingCountersWindow) Print

func (scw *SlidingCountersWindow) Print()

Print debug info for the window

Jump to

Keyboard shortcuts

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