Documentation ¶
Overview ¶
Package limitware provides middleware for rate limiting HTTP handlers.
Index ¶
- func APIKeyFunc(ctx context.Context, db *database.Database, scope string, hmacKey []byte) httplimit.KeyFunc
- func IPAddressKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.KeyFunc
- func UserIDKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.KeyFunc
- type Middleware
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyFunc ¶
func APIKeyFunc(ctx context.Context, db *database.Database, scope string, hmacKey []byte) httplimit.KeyFunc
APIKeyFunc returns a default key function for ratelimiting on our API key header. Since APIKeys are assumed to be "public" at some point, they are rate limited by [realm,ip], and API keys have a 1-1 mapping to a realm.
func IPAddressKeyFunc ¶
IPAddressKeyFunc uses the client IP to rate limit.
Types ¶
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware is a handler/mux that can wrap other middlware to implement HTTP rate limiting. It can rate limit based on an arbitrary KeyFunc, and supports anything that implements limiter.Store.
func NewMiddleware ¶
func NewMiddleware(ctx context.Context, s limiter.Store, f httplimit.KeyFunc, opts ...Option) (*Middleware, error)
NewMiddleware creates a new middleware suitable for use as an HTTP handler. This function returns an error if either the Store or KeyFunc are nil.
func (*Middleware) Handle ¶
func (m *Middleware) Handle(next http.Handler) http.Handler
Handle returns the HTTP handler as a middleware. This handler calls Take() on the store and sets the common rate limiting headers. If the take is successful, the remaining middleware is called. If take is unsuccessful, the middleware chain is halted and the function renders a 429 to the caller with metadata about when it's safe to retry.
type Option ¶ added in v0.5.0
type Option func(m *Middleware) *Middleware
Option is an option to the middleware.
func AllowOnError ¶ added in v0.5.0
AllowOnError instructs the middleware to fail (internal server error) on connection errors. The default behavior is to fail on errors to Take.