Documentation
¶
Overview ¶
Package ratelimit provides an efficient token bucket implementation that can be used to limit the rate concurrent HTTP traffic. See http://en.wikipedia.org/wiki/Token_bucket.
Index ¶
Constants ¶
const Version = "0.1.0"
Version defines the current package semantic version.
Variables ¶
var RateLimitResponder = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(429) w.Write([]byte("Too Many Requests")) }
RateLimitResponder is used as default function to repond when the rate limit is reached. You can customize it via Limiter.SetResponder(fn).
Functions ¶
This section is empty.
Types ¶
type Exception ¶
type Exception Filter
Exception represents the Limiter exception function signature.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter implements a token bucket rate limiter middleware. Rate limiter can support multiple rate limit strategies, such as time based limiter.
func NewRateLimiter ¶
NewRateLimiter creates a rate limiter middleware which limits the amount of requests accepted per second.
func NewTimeLimiter ¶
NewTimeLimiter creates a new time based rate limiter middleware.
func (*Limiter) Exception ¶
Exception registers whitelist exception. If the exception function matches, the traffic won't be limited.
func (*Limiter) Filter ¶
Filter registers a new rate limiter whitelist filter. If the filter matches, the traffic won't be limited.
func (*Limiter) LimitHTTP ¶
LimitHTTP limits an incoming HTTP request. If some filter passes, the request won't be limited. This method is used internally, but made public for public testing.
func (*Limiter) Register ¶
func (l *Limiter) Register(mw layer.Middleware)
Register registers the middleware handler.
func (*Limiter) SetResponder ¶
func (l *Limiter) SetResponder(fn http.HandlerFunc)
SetResponder sets a custom function to reply in case of rate limit reached.
