limitware

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package limitware provides middleware for rate limiting HTTP handlers.

Index

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

func IPAddressKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.KeyFunc

IPAddressKeyFunc uses the client IP to rate limit.

func UserIDKeyFunc added in v0.8.0

func UserIDKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.KeyFunc

UserIDKeyFunc pulls the user out of the request context and uses that to ratelimit. It falls back to rate limiting by the client ip.

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

func AllowOnError(v bool) Option

AllowOnError instructs the middleware to fail (internal server error) on connection errors. The default behavior is to fail on errors to Take.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL