Documentation ¶
Index ¶
- func Key(key string) func(r *http.Request) (string, error)
- func KeyByEndpoint(r *http.Request) (string, error)
- func KeyByIP(r *http.Request) (string, error)
- func KeyByRealIP(r *http.Request) (string, error)
- func Limit(requestLimit int, windowLength time.Duration, options ...Option) func(next http.Handler) http.Handler
- func LimitAll(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler
- func LimitByIP(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler
- func LimitByRealIP(requestLimit int, windowLength time.Duration) func(next http.Handler) http.Handler
- func LimitCounterKey(key string, window time.Time) uint64
- func NewLocalLimitCounter(windowLength time.Duration) *localCounter
- func WithIncrement(ctx context.Context, value int) context.Context
- func WithRequestLimit(ctx context.Context, value int) context.Context
- type KeyFunc
- type LimitCounter
- type Option
- func WithErrorHandler(h func(http.ResponseWriter, *http.Request, error)) Option
- func WithKeyByIP() Option
- func WithKeyByRealIP() Option
- func WithKeyFuncs(keyFuncs ...KeyFunc) Option
- func WithLimitCounter(c LimitCounter) Option
- func WithLimitHandler(h http.HandlerFunc) Option
- func WithNoop() Option
- func WithResponseHeaders(headers ResponseHeaders) Option
- type RateLimiter
- func (l *RateLimiter) Counter() LimitCounter
- func (l *RateLimiter) Handler(next http.Handler) http.Handler
- func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string) bool
- func (l *RateLimiter) RespondOnLimit(w http.ResponseWriter, r *http.Request, key string) bool
- func (l *RateLimiter) Status(key string) (bool, float64, error)
- type ResponseHeaders
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LimitByRealIP ¶ added in v0.6.0
func NewLocalLimitCounter ¶ added in v0.12.0
NewLocalLimitCounter creates an instance of localCounter, which is an in-memory implementation of http.LimitCounter.
All methods are guaranteed to always return nil error.
func WithIncrement ¶ added in v0.8.0
Types ¶
type LimitCounter ¶
type Option ¶
type Option func(rl *RateLimiter)
func WithErrorHandler ¶ added in v0.12.1
func WithKeyByIP ¶ added in v0.7.0
func WithKeyByIP() Option
func WithKeyByRealIP ¶ added in v0.7.0
func WithKeyByRealIP() Option
func WithKeyFuncs ¶
func WithLimitCounter ¶
func WithLimitCounter(c LimitCounter) Option
func WithLimitHandler ¶
func WithLimitHandler(h http.HandlerFunc) Option
func WithResponseHeaders ¶ added in v0.11.0
func WithResponseHeaders(headers ResponseHeaders) Option
type RateLimiter ¶ added in v0.13.1
type RateLimiter struct {
// contains filtered or unexported fields
}
func NewRateLimiter ¶
func NewRateLimiter(requestLimit int, windowLength time.Duration, options ...Option) *RateLimiter
func (*RateLimiter) Counter ¶ added in v0.13.1
func (l *RateLimiter) Counter() LimitCounter
func (*RateLimiter) Handler ¶ added in v0.13.1
func (l *RateLimiter) Handler(next http.Handler) http.Handler
func (*RateLimiter) OnLimit ¶ added in v0.13.1
func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string) bool
OnLimit checks the rate limit for the given key and updates the response headers accordingly. If the limit is reached, it returns true, indicating that the request should be halted. Otherwise, it increments the request count and returns false. This method does not send an HTTP response, so the caller must handle the response themselves or use the RespondOnLimit() method instead.
func (*RateLimiter) RespondOnLimit ¶ added in v0.14.0
func (l *RateLimiter) RespondOnLimit(w http.ResponseWriter, r *http.Request, key string) bool
RespondOnLimit checks the rate limit for the given key and updates the response headers accordingly. If the limit is reached, it automatically sends an HTTP response and returns true, signaling the caller to halt further request processing. If the limit is not reached, it increments the request count and returns false, allowing the request to proceed.
type ResponseHeaders ¶ added in v0.11.0
type ResponseHeaders struct { Limit string // Default: X-RateLimit-Limit Remaining string // Default: X-RateLimit-Remaining Increment string // Default: X-RateLimit-Increment Reset string // Default: X-RateLimit-Reset RetryAfter string // Default: Retry-After }
Set custom response headers. If empty, the header is omitted.