Overview
TheDefaultRateLimitKeyResolver is the default implementation that creates stable keys from scope and annotation/method metadata. It constructs keys in the format scope:identifier.
Package: io.github.v4runsharma.ratelimiter.key
Source: DefaultRateLimitKeyResolver.java:11
Key format
Keys follow the pattern:scopeis normalized from the annotation’s scope parameter (defaults to “global”)identifieris either:- The annotation’s
keyparameter (if provided) - Or
className#methodName(fully qualified class name + method name)
- The annotation’s
Methods
resolveKey
- Normalize scope from annotation (default: “global”)
- If annotation has a static
keyparameter, use it - Otherwise, use
className#methodName
The invocation context. Must not be null and must contain a non-null annotation.
A stable key string in the format
scope:identifier.DefaultRateLimitKeyResolver.java:14-24
Usage examples
With default scope and no key
global:com.example.OrderService#processOrder
With user scope and no key
user:com.example.UserService#getUserData
With static key
ip:public-api
With IP scope
ip:com.example.ApiController#rateLimitedEndpoint
Key generation rules
Scope normalization
- If null or blank → defaults to
"global" - Converted to uppercase and matched against
RateLimitScopeenum - Returned in lowercase (e.g., “USER” → “user”)
DefaultRateLimitKeyResolver.java:26-31
Identifier resolution
-
With static key:
-
Without static key:
Complete example
When to use custom resolver
Consider implementing a customRateLimitKeyResolver when you need:
- Dynamic key components: Include runtime values like user ID from authentication context
- IP-based limiting: Extract client IP from request headers
- API key limiting: Use API key from request headers
- Parameter-based keys: Include method parameter values in the key
- Complex composite keys: Combine multiple contextual elements
Related
- RateLimitKeyResolver - Interface and custom implementations
- RateLimitScope - Available scope values
- @RateLimit - Annotation with scope and key parameters