Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidParams = errors.New("[err] invalid params") ErrUnknown = errors.New("[err] unknown") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { Get(key string) (interface{}, bool, error) MGet(keys ...string) ([]interface{}, bool, error) Set(key string, value interface{}, ttl time.Duration) error Increment(key string, n int64, ttl time.Duration) error Ping(ctx context.Context) bool Close() error }
Cache is an interface which supports fundamental functionality for storage. This is used by RateLimiter.
func NewRedisCache ¶
NewRedisCache returns cache for redis
type ErrorHandler ¶
type ErrorHandler func(error)
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is an interface for dependency injection.
type OptionFunc ¶
type OptionFunc func(l *limiter)
OptionFunc is a function for Option interface.
func WithErrorHandler ¶
func WithErrorHandler(h ErrorHandler) OptionFunc
WithErrorHandler returns a function which sets handler to process error.
func WithEstimatePeriod ¶
func WithEstimatePeriod(d time.Duration) OptionFunc
WithEstimatePeriod returns a function which sets a period to estimate threshold.
func WithMaxLimit ¶
func WithMaxLimit(max int64) OptionFunc
WithMaxLimit returns a function which sets maximum limit per minute in limiter.
func WithMinLimit ¶
func WithMinLimit(min int64) OptionFunc
WithMinLimit returns a function which sets minimum limit per minute in limiter.
func WithRevaluateLimit ¶
func WithRevaluateLimit(f RevaluateLimit) OptionFunc
WithRevaluateLimit returns a function which sets function used to revaluate limit.
type RateLimiter ¶
type RateLimiter interface { Acquire() (bool, error) CurrentLimit() (int64, error) HealthCheck(ctx context.Context) bool Close() error }
func NewRateLimiter ¶
func NewRateLimiter(serviceName string, cache Cache, opts ...Option) (RateLimiter, error)
NewRateLimiter returns RateLimiter interface.
type RevaluateLimit ¶
type RevaluateLimit func(currentlyLimit, estimatedTimeWindowSize, estimatedTimeWindowUsedCount int64) int64
RevaluateLimit is to revaluate sending limit per 1 minute. currentlyLimit is currently applied limit. estimatedTimeWindowSize is the size of time-window for the duration of an estimated period. estimatedTimeWindowUsedCount is the sum of sending push count of time-window for the duration of an estimated period. Its return value is new limit per 1 minute.