Documentation
¶
Overview ¶
Package ratelimit provides HTTP rate limiting for generated or user-owned request-time handlers.
Index ¶
- Constants
- func Addon() gowdk.Addon
- func DefaultErrorHandler(writer http.ResponseWriter, _ *http.Request, err error)
- func DefaultLimitHandler(writer http.ResponseWriter, _ *http.Request, _ Result)
- func KeyByRemoteAddr(request *http.Request) string
- func WriteHeaders(writer http.ResponseWriter, result Result)
- type ErrorHandler
- type InMemoryOptions
- type InMemoryStore
- type KeyFunc
- type LimitHandler
- type Limiter
- type Options
- type RedisClient
- type RedisOptions
- type RedisStore
- type Result
- type Store
Constants ¶
const ( // ImportPath is the canonical Go import path for the rate-limit addon. ImportPath = "github.com/cssbruno/gowdk/addons/ratelimit" // HeaderLimit reports the configured request limit for the current window. HeaderLimit = "X-RateLimit-Limit" // HeaderRemaining reports how many requests remain in the current window. HeaderRemaining = "X-RateLimit-Remaining" // HeaderReset reports the Unix timestamp when the current window resets. HeaderReset = "X-RateLimit-Reset" )
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(writer http.ResponseWriter, _ *http.Request, err error)
DefaultErrorHandler writes HTTP 500 for limiter failures.
func DefaultLimitHandler ¶
func DefaultLimitHandler(writer http.ResponseWriter, _ *http.Request, _ Result)
DefaultLimitHandler writes HTTP 429 for blocked requests.
func KeyByRemoteAddr ¶
KeyByRemoteAddr returns the request RemoteAddr host. It intentionally ignores forwarded headers because those are only safe behind trusted proxy handling.
func WriteHeaders ¶
func WriteHeaders(writer http.ResponseWriter, result Result)
WriteHeaders writes rate-limit metadata response headers.
Types ¶
type ErrorHandler ¶
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
ErrorHandler writes the response for rate-limit store or configuration failures.
type InMemoryOptions ¶
InMemoryOptions configures the in-memory store.
type InMemoryStore ¶
type InMemoryStore struct {
// contains filtered or unexported fields
}
InMemoryStore stores fixed-window counters in the current process.
func NewInMemoryStore ¶
func NewInMemoryStore(options InMemoryOptions) *InMemoryStore
NewInMemoryStore creates a concurrency-safe process-local store.
type LimitHandler ¶
type LimitHandler func(http.ResponseWriter, *http.Request, Result)
LimitHandler writes the response for blocked requests.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter applies fixed-window rate limits to HTTP requests.
func (*Limiter) AllowRequest ¶
AllowRequest records a request and returns the rate-limit decision.
type Options ¶
type Options struct {
Limit int
Window time.Duration
Store Store
KeyFunc KeyFunc
LimitHandler LimitHandler
ErrorHandler ErrorHandler
Now func() time.Time
}
Options configures a Limiter.
type RedisClient ¶
type RedisClient interface {
EvalInt64s(ctx context.Context, script string, keys []string, args ...string) ([]int64, error)
}
RedisClient adapts a Redis client to the rate limiter's fixed-window script. Implementations should run script atomically and return the Lua integer array.
type RedisOptions ¶
type RedisOptions struct {
Client RedisClient
KeyPrefix string
}
RedisOptions configures a Redis-backed store.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore stores fixed-window counters in Redis.
func NewRedisStore ¶
func NewRedisStore(options RedisOptions) (*RedisStore, error)
NewRedisStore creates a Redis-backed store without depending on a concrete Redis client package.