Documentation
¶
Index ¶
- type Cache
- type Logger
- type Option
- func WithCache(cache Cache) Option
- func WithExceedErrorFormatter(exceedErrorFormatter exceedErrorFormatterFunc) Option
- func WithGlobalLimitRules(rules []Rule) Option
- func WithLogger(logger Logger) Option
- func WithNamespace(namespace string) Option
- func WithRateKeyExtender(rateKeyExtender rateKeyExtenderFunc) Option
- func WithRateKeyFormatter(rateKeyFormatter rateKeyFormatterFunc) Option
- type RateLimiter
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Increment(ctx context.Context, key string, ttl time.Duration) (int64, error)
}
Cache defines storage behavior for fixed-window rate limiting.
Implementations MUST provide atomic increment semantics.
Increment atomically increments the counter for the given key and MUST ensure that:
- The TTL is set only if the key did not previously exist (i.e. on the first increment).
- The TTL MUST NOT be extended or modified on subsequent increments.
This behavior guarantees fixed-window semantics.
If TTL is extended on every increment, the algorithm becomes a sliding window, which is NOT the intended behavior of this interface.
Implementations should ensure atomicity (e.g. Redis Lua script).
type Option ¶
type Option func(*RateLimiter)
Option configures RateLimiter.
func WithCache ¶
WithCache sets a custom storage backend.
The provided cache must implement atomic fixed-window semantics.
func WithExceedErrorFormatter ¶
func WithExceedErrorFormatter(exceedErrorFormatter exceedErrorFormatterFunc) Option
WithExceedErrorFormatter overrides the error returned when one or more rate limit rules are exceeded.
func WithGlobalLimitRules ¶
WithGlobalLimitRules configures rules that apply to all RPC methods.
func WithLogger ¶
WithLogger sets a custom logger implementation.
func WithNamespace ¶
WithNamespace sets a namespace prefix for all generated storage keys.
Useful when sharing the same cache across multiple services.
func WithRateKeyExtender ¶
func WithRateKeyExtender(rateKeyExtender rateKeyExtenderFunc) Option
WithRateKeyExtender overrides the default rate key extension logic.
It allows adding custom identifiers (e.g. user ID) to the rate key.
func WithRateKeyFormatter ¶
func WithRateKeyFormatter(rateKeyFormatter rateKeyFormatterFunc) Option
WithRateKeyFormatter overrides the storage key formatting logic.
Intended for advanced customization of key structure.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a fixed-window rate limiting middleware for gRPC. The limiter enforces fixed-window semantics.
func New ¶
func New(opts ...Option) *RateLimiter
New creates a new RateLimiter with default configuration.
By default, it uses an in-memory cache, no-op logger, default namespace, and standard key formatting behavior.
func (*RateLimiter) UnaryServerInterceptor ¶
func (rl *RateLimiter) UnaryServerInterceptor() grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a gRPC unary server interceptor that enforces fixed-window rate limiting for incoming requests.
It evaluates global and per-method rules, extracts rate key attributes from protobuf messages, and rejects requests that exceed configured limits.
type Rule ¶
Rule describes a single fixed-window rate limiting rule.
func RateLimitRulesToModel ¶
func RateLimitRulesToModel(rs []*ratelimiterpb.Rule) []Rule
RateLimitRulesToModel converts protobuf Rule definitions into internal Rule models used by the rate limiter.