1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
package ratelimit
import (
"fmt"
"github.com/vulcand/oxy/v2/utils"
)
// TokenLimiterOption token limiter option type.
type TokenLimiterOption func(l *TokenLimiter) error
// ErrorHandler sets error handler of the server.
func ErrorHandler(h utils.ErrorHandler) TokenLimiterOption {
return func(cl *TokenLimiter) error {
cl.errHandler = h
return nil
}
}
// ExtractRates sets the rate extractor.
func ExtractRates(e RateExtractor) TokenLimiterOption {
return func(cl *TokenLimiter) error {
cl.extractRates = e
return nil
}
}
// Capacity sets the capacity.
func Capacity(capacity int) TokenLimiterOption {
return func(cl *TokenLimiter) error {
if capacity <= 0 {
return fmt.Errorf("bad capacity: %v", capacity)
}
cl.capacity = capacity
return nil
}
}
// Logger defines the logger the TokenLimiter will use.
func Logger(l utils.Logger) TokenLimiterOption {
return func(tl *TokenLimiter) error {
tl.log = l
return nil
}
}
|