Documentation ¶
Index ¶
- Constants
- func AddToRedisSlidingCountersWindow(conn redis.Conn, key string, timestampSec int64) error
- func NewRedisPool(conf *RedisPoolConf) *redis.Pool
- func SetRedisRateLimit(conn redis.Conn, key string, reqPerMin int64) error
- type InMemRateLimit
- func (imrl *InMemRateLimit) GetSlidingCountersWindow(atTimestampSec int64) *SlidingCountersWindow
- func (imrl *InMemRateLimit) Inc(timestampSec int64)
- func (imrl *InMemRateLimit) NumEmptySlotsAtStart() int
- func (imrl *InMemRateLimit) UpdateSlidingCountersWindow(toUpdate *SlidingCountersWindow, timestampSec int64)
- type RedisPoolConf
- type SlidingCountersWindow
Constants ¶
const ( RedisReqPerMinPattern string = "dynlimits_reqpermin_%s" RedisSlidingCountersWindowPattern string = "dynlimits_scw_%d_%s" )
Variables ¶
This section is empty.
Functions ¶
func AddToRedisSlidingCountersWindow ¶
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
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 ¶
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