Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRateLimitExceeded is returned when rate limit is exceeded ErrRateLimitExceeded = errors.New("rate limit exceeded") )
Functions ¶
This section is empty.
Types ¶
type APIKeyExtractor ¶
type APIKeyExtractor struct{}
APIKeyExtractor extracts API key from header or query parameter
type Algorithm ¶
type Algorithm interface {
// Allow checks if a request is allowed
// Returns:
// - allowed: whether the request is allowed
// - remaining: remaining requests/tokens
// - resetTime: when the rate limit will reset
// - err: any error that occurred
Allow(ctx context.Context, storage Storage, key string, limit int64, window time.Duration, burst int64) (allowed bool, remaining int64, resetTime time.Time, err error)
}
Algorithm defines the interface for rate limiting algorithms
type AlgorithmFactory ¶
type AlgorithmFactory struct{}
AlgorithmFactory creates algorithm instances
func (*AlgorithmFactory) NewAlgorithm ¶
func (f *AlgorithmFactory) NewAlgorithm(algorithmType string) Algorithm
NewAlgorithm creates an algorithm instance based on algorithm type
type ClientIDExtractor ¶
type ClientIDExtractor struct{}
ClientIDExtractor extracts a client identifier from X-Client-ID or query client_id.
type ExtractorFactory ¶
type ExtractorFactory struct{}
ExtractorFactory creates key extractors based on configuration
func (*ExtractorFactory) NewExtractor ¶
func (f *ExtractorFactory) NewExtractor(keyType, keyHeader string) KeyExtractor
NewExtractor creates a key extractor based on key type
type FixedWindowAlgorithm ¶
type FixedWindowAlgorithm struct{}
FixedWindowAlgorithm implements fixed window rate limiting
type HeaderExtractor ¶
type HeaderExtractor struct {
HeaderName string
}
HeaderExtractor extracts value from a custom header
type KeyExtractor ¶
KeyExtractor extracts a key from the request for rate limiting
type LeakyBucketAlgorithm ¶
type LeakyBucketAlgorithm struct{}
LeakyBucketAlgorithm implements leaky bucket rate limiting backed by Storage (Application.Cache).
type RateLimit ¶
RateLimit implements rate limiting plugin
func (*RateLimit) OnResponse ¶
OnResponse is called after receiving response
type Storage ¶
type Storage interface {
Allow(ctx context.Context, key string, limit int64, window time.Duration) (allowed bool, remaining int64, resetTime time.Time, err error)
Reset(ctx context.Context, key string) error
Close() error
// LoadState / SaveState persist token-bucket and leaky-bucket state in Application.Cache().
// kind is "tb" or "lb"; key is the same logical client key used by extractors.
LoadState(ctx context.Context, kind, key string, dest interface{}) error
SaveState(ctx context.Context, kind, key string, src interface{}, ttl time.Duration) error
}
Storage is the counter backend for rate-limit algorithms.
type TokenBucketAlgorithm ¶
type TokenBucketAlgorithm struct{}
TokenBucketAlgorithm implements token bucket rate limiting backed by Storage (Application.Cache).