// Returns vulcan library compatible middleware func (r *ConnLimit) NewMiddleware() (middleware.Middleware, error) { mapper, err := limit.VariableToMapper(r.Variable) if err != nil { return nil, err } return connlimit.NewConnectionLimiter(mapper, r.Connections) }
// Returns vulcan library compatible middleware func (r *RateLimit) NewMiddleware() (middleware.Middleware, error) { mapper, err := limit.VariableToMapper(r.Variable) if err != nil { return nil, err } rate := tokenbucket.Rate{Units: int64(r.Requests), Period: time.Second * time.Duration(r.PeriodSeconds)} return tokenbucket.NewTokenLimiterWithOptions(mapper, rate, tokenbucket.Options{Burst: r.Burst}) }
func NewConnLimit(connections int64, variable string) (*ConnLimit, error) { if _, err := limit.VariableToMapper(variable); err != nil { return nil, err } if connections < 0 { return nil, fmt.Errorf("Connections should be > 0, got %d", connections) } return &ConnLimit{ Connections: connections, Variable: variable, }, nil }
func NewRateLimit(requests int, variable string, burst int64, periodSeconds int) (*RateLimit, error) { if _, err := limit.VariableToMapper(variable); err != nil { return nil, err } if requests <= 0 { return nil, fmt.Errorf("Requests should be > 0, got %d", requests) } if burst < 0 { return nil, fmt.Errorf("Burst should be >= 0, got %d", burst) } if periodSeconds <= 0 { return nil, fmt.Errorf("Period seconds should be > 0, got %d", periodSeconds) } return &RateLimit{ Requests: requests, Variable: variable, Burst: burst, PeriodSeconds: periodSeconds, }, nil }