Documentation ¶
Overview ¶
Package ratelimiter is a generated GoMock package.
Index ¶
- Constants
- func LimiterMiddleware(s Store, keyFunc KeyFunc, opts ...Option) func(next http.Handler) http.Handler
- type ExclFunc
- type KeyFunc
- type MockStore
- func (m *MockStore) EXPECT() *MockStoreMockRecorder
- func (m *MockStore) Reset(ctx context.Context) error
- func (m *MockStore) Take(ctx context.Context, key string) (uint64, uint64, uint64, bool, error)
- func (m *MockStore) TakeExcl(ctx context.Context, key string, f ExclFunc) (uint64, uint64, uint64, bool, error)
- type MockStoreMockRecorder
- type Option
- type Options
- type RedisClient
- type RedisConfig
- type Store
Constants ¶
const ( // HeaderRateLimitLimit - maximum number of calls HeaderRateLimitLimit = "X-RateLimit-Limit" // HeaderRateLimitRemaining - Number of calls before restrictions apply HeaderRateLimitRemaining = "X-RateLimit-Remaining" // HeaderRateLimitReset - Limit reset time HeaderRateLimitReset = "X-RateLimit-Reset" // HeaderRetryAfter is the header used to indicate when a client should retry // requests (when the rate limit expires), in UTC time. HeaderRetryAfter = "Retry-After" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type KeyFunc ¶
KeyFunc is a function that accepts an http request and returns a string key that uniquely identifies this request for the purpose of rate limiting.
KeyFuncs are called on each request, so be mindful of performance and implement caching where possible. If a KeyFunc returns an error, the HTTP handler will return Internal Server Error and will NOT take from the limiter store.
type MockStore ¶
type MockStore struct {
// contains filtered or unexported fields
}
MockStore is a mock of Store interface.
func NewMockStore ¶
func NewMockStore(ctrl *gomock.Controller) *MockStore
NewMockStore creates a new mock instance.
func (*MockStore) EXPECT ¶
func (m *MockStore) EXPECT() *MockStoreMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
type MockStoreMockRecorder ¶
type MockStoreMockRecorder struct {
// contains filtered or unexported fields
}
MockStoreMockRecorder is the mock recorder for MockStore.
func (*MockStoreMockRecorder) Reset ¶
func (mr *MockStoreMockRecorder) Reset(ctx interface{}) *gomock.Call
Reset indicates an expected call of Reset.
func (*MockStoreMockRecorder) Take ¶
func (mr *MockStoreMockRecorder) Take(ctx, key interface{}) *gomock.Call
Take indicates an expected call of Take.
func (*MockStoreMockRecorder) TakeExcl ¶
func (mr *MockStoreMockRecorder) TakeExcl(ctx, key, f interface{}) *gomock.Call
TakeExcl indicates an expected call of TakeExcl.
type Option ¶
type Option func(*Options)
func WithDateFormat ¶
WithDateFormat set custom date format into HeaderRetryAfter/HeaderRateLimitReset
func WithSkipper ¶
WithSkipper set skipper function for skipping
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options for middleware
type RedisClient ¶
type RedisClient interface { HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd HSet(ctx context.Context, key string, values ...interface{}) *redis.IntCmd Del(ctx context.Context, keys ...string) *redis.IntCmd Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd TxPipeline() redis.Pipeliner SMembers(ctx context.Context, key string) *redis.StringSliceCmd }
type RedisConfig ¶
type RedisConfig struct { // redis tags for invalidating keys Tags []string // tagging keys TagsPrefix string // redis key prefix for redis store Prefix string // limiter interval Interval time.Duration // limiter max points Points uint64 }
RedisConfig - struct for configure redis store
For example if you want to set the limit of requests per second to 10 req per sec RedisConfig{ Interval: time.Second * 1, Points: 10} or 20 req per 2 minutes RedisConfig{ Interval: time.Minute * 2, Points: 20}
type Store ¶
type Store interface { // Take actual rate limit constraint by key or create new Take(ctx context.Context, key string) (limit, remaining, resetTime uint64, ok bool, err error) // Reset completely clears the store and resets all tokens Reset(ctx context.Context) error // TakeExcl actual rate limit constraint by key or create new with exclusive function TakeExcl(ctx context.Context, key string, f ExclFunc) (limit, remaining, resetTime uint64, ok bool, err error) }
func DefaultRedisStore ¶ added in v0.2.0
func DefaultRedisStore(ctx context.Context, addr string, interval time.Duration, points uint64) (Store, error)
DefaultRedisStore return Store instance of default redis options
func NewRedisStore ¶
func NewRedisStore(instance RedisClient, cfg RedisConfig) Store
NewRedisStore make redis store