Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMalformedLimit is used after attempting to Parse a malformed limit string ErrMalformedLimit = errors.New("malformed string") // ErrMalformedLimitNumber is used when the first number (the limit) is not a number ErrMalformedLimitNumber = errors.New("malformed limit") // ErrInvalidLimitNumber is used when the first number (the limit) is < 0 ErrInvalidLimitNumber = errors.New("limit must be > 0") // ErrInvalidDuration is used when the duration is < 0s ErrInvalidDuration = errors.New("duration must be > 0s") )
Functions ¶
Types ¶
type Limit ¶
Limit is a limiter used with New to execuate a ratelimiter
func MustParseLimit ¶
MustParseLimit calls ParseLimit and panics if there is an error
func MustParseLimits ¶
MustParseLimits parses limits and then panics if there is an error
func ParseLimit ¶
ParseLimit parses a limiter string limit should be in the format of <count>/<duration>/g where count is the maximum count of requests, duration is the length of time, and /g means that the limiter is global and not ip specific
Example:
1/1m = one request per minute per IP address 10/24h/g = ten requests per day globally
func ParseLimits ¶
ParseLimits parses a slice of limits with ParseLimit
type NopLimiter ¶
type NopLimiter struct{}
NopLimiter is a limiter that returns false for all Limit() queries
type RedisLimiter ¶
type RedisLimiter struct { Pool redisPool Limits []Limit LimitOnError bool OnError func(ip string, err error) }
RedisLimiter is a rate limit which can evaluate an IP address to determine if it should be rate limited using Redis as a backend
func NewRedisLimiter ¶
func NewRedisLimiter(pool redisPool, limits []Limit) *RedisLimiter
NewRedisLimiter creates a properly initialized RedisLimiter
func (*RedisLimiter) Limit ¶
func (l *RedisLimiter) Limit(ip string) bool
Limit checks an IP address to see if it should be ratelimited. It returns true if the IP address should be ratelimited and false otherwise any errors encountered will return *RedisLimiter.LimitOnError plus the error
type WhitelistedLimiter ¶
WhitelistedLimiter wraps a ratelimiter with a whitelist
func NewWhitelistedLimiter ¶
func NewWhitelistedLimiter(limiter Limiter, whitelist []string) *WhitelistedLimiter
NewWhitelistedLimiter constructs a new WhitelistedLimiter
func (*WhitelistedLimiter) Limit ¶
func (w *WhitelistedLimiter) Limit(ip string) bool
Limit checks the whitelist for whitelisted IP addresses and then return false if any match. If none match then it defers to w.Limiter.Limit