Documentation
¶
Index ¶
- func ClientIP(r *http.Request) string
- type ConcurrentQueueStrategy
- type ConcurrentStrategy
- type ExceededHandler
- type FixedWindowStrategy
- type LeakyBucketStrategy
- type RateLimiter
- func Concurrent(capacity int) *RateLimiter
- func ConcurrentQueue(capacity, size int) *RateLimiter
- func FixedWindow(rate int, unit time.Duration) *RateLimiter
- func FixedWindowPerHour(rate int) *RateLimiter
- func FixedWindowPerMinute(rate int) *RateLimiter
- func FixedWindowPerSecond(rate int) *RateLimiter
- func LeakyBucket(perRequest time.Duration, size int) *RateLimiter
- func New(strategy Strategy) *RateLimiter
- type Strategy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ConcurrentQueueStrategy ¶
type ConcurrentQueueStrategy struct {
Capacity int // max concurrent at a time
Size int // queue size
// contains filtered or unexported fields
}
ConcurrentQueueStrategy implements Strategy that allow only max concurrent requests at a time other requests will queue, until queue full then the request wil drop
func (*ConcurrentQueueStrategy) After ¶
func (b *ConcurrentQueueStrategy) After(string) time.Duration
After always return 0, since we don't know when request will finish
func (*ConcurrentQueueStrategy) Put ¶
func (b *ConcurrentQueueStrategy) Put(key string)
Put removes requests count
func (*ConcurrentQueueStrategy) Take ¶
func (b *ConcurrentQueueStrategy) Take(key string) bool
Take returns true if current requests less than capacity
type ConcurrentStrategy ¶
type ConcurrentStrategy struct {
Capacity int // Max concurrent at a time
// contains filtered or unexported fields
}
ConcurrentStrategy implements Strategy that allow only max concurrent requests at a time other requests will drop
func (*ConcurrentStrategy) After ¶
func (b *ConcurrentStrategy) After(string) time.Duration
After always return 0, since we don't know when request will finish
func (*ConcurrentStrategy) Put ¶
func (b *ConcurrentStrategy) Put(key string)
Put removes requests count
func (*ConcurrentStrategy) Take ¶
func (b *ConcurrentStrategy) Take(key string) bool
Take returns true if current requests less than capacity
type ExceededHandler ¶
ExceededHandler type
type FixedWindowStrategy ¶
type FixedWindowStrategy struct {
Max int // Max token per window
Size time.Duration // Window size
// contains filtered or unexported fields
}
FixedWindowStrategy implements Strategy using fixed window algorithm
func (*FixedWindowStrategy) After ¶
func (b *FixedWindowStrategy) After(key string) time.Duration
After returns next time that can take again
func (*FixedWindowStrategy) Take ¶
func (b *FixedWindowStrategy) Take(key string) bool
Take takes a token from bucket, return true if token available to take
type LeakyBucketStrategy ¶
type LeakyBucketStrategy struct {
PerRequest time.Duration // time per request
Size int // queue size
// contains filtered or unexported fields
}
LeakyBucketStrategy implements Strategy using leaky bucket algorithm
func (*LeakyBucketStrategy) After ¶
func (b *LeakyBucketStrategy) After(key string) time.Duration
After returns time that can take again
func (*LeakyBucketStrategy) Take ¶
func (b *LeakyBucketStrategy) Take(key string) bool
Take waits until token can be take, unless queue full will return false
type RateLimiter ¶
type RateLimiter struct {
Key func(r *http.Request) string
ExceededHandler ExceededHandler
Strategy Strategy
ReleaseOnWriteHeader bool // release token when write response's header
ReleaseOnHijacked bool // release token when hijacked
}
RateLimiter middleware
func Concurrent ¶
func Concurrent(capacity int) *RateLimiter
Concurrent creates new concurrent rate limiter
func ConcurrentQueue ¶
func ConcurrentQueue(capacity, size int) *RateLimiter
ConcurrentQueue creates new concurrent queue rate limiter
func FixedWindow ¶
func FixedWindow(rate int, unit time.Duration) *RateLimiter
FixedWindow creates new fixed window rate limiter
func FixedWindowPerHour ¶
func FixedWindowPerHour(rate int) *RateLimiter
FixedWindowPerHour creates new rate limiter per hour
func FixedWindowPerMinute ¶
func FixedWindowPerMinute(rate int) *RateLimiter
FixedWindowPerMinute creates new rate limiter per minute
func FixedWindowPerSecond ¶
func FixedWindowPerSecond(rate int) *RateLimiter
FixedWindowPerSecond creates new rate limiter per second
func LeakyBucket ¶
func LeakyBucket(perRequest time.Duration, size int) *RateLimiter
LeakyBucket creates new leaky bucket rate limiter
func (RateLimiter) ServeHandler ¶
func (m RateLimiter) ServeHandler(h http.Handler) http.Handler
ServeHandler implements middleware interface