Class RateLimitGuard.Builder
- Enclosing class:
- RateLimitGuard
RateLimitGuard.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the guard.burst(int value) Sets the burst capacity (maximum tokens a bucket can hold).exemptPaths(String... values) Sets the set of request paths that bypass throttling.keyBy(Function<RestRequest, String> value) Sets the per-request key resolver.permitsPerHour(int value) Sets the steady-state refill rate in permits per hour.permitsPerMinute(int value) Sets the steady-state refill rate in permits per minute.permitsPerSecond(int value) Sets the steady-state refill rate in permits per second.storage(RateLimitGuard.Storage value) Sets the bucket-state storage backend.Sets a callback invoked when a request is rejected due to an empty bucket.xForwardedForAware(boolean value) EnablesX-Forwarded-For-aware key resolution.
-
Constructor Details
-
Builder
protected Builder()Constructor.
-
-
Method Details
-
permitsPerSecond
Sets the steady-state refill rate in permits per second.- Parameters:
value- Permits per second. Must be> 0.- Returns:
- This object.
-
permitsPerMinute
Sets the steady-state refill rate in permits per minute.Equivalent to
permitsPerSecond(value / 60.0).- Parameters:
value- Permits per minute. Must be> 0.- Returns:
- This object.
-
permitsPerHour
Sets the steady-state refill rate in permits per hour.Equivalent to
permitsPerSecond(value / 3600.0).- Parameters:
value- Permits per hour. Must be> 0.- Returns:
- This object.
-
burst
Sets the burst capacity (maximum tokens a bucket can hold).- Parameters:
value- Burst capacity. Must be> 0.- Returns:
- This object.
-
keyBy
Sets the per-request key resolver.Default is
ServletRequestWrapper.getRemoteAddr(). Common alternatives include the user-principal name, an API-key header, or a tenant id. When the resolver returnsnull the empty string is used.- Parameters:
value- Resolver function. Must not benull .- Returns:
- This object.
-
xForwardedForAware
EnablesX-Forwarded-For-aware key resolution.When enabled, the first comma-separated hop of the
X-Forwarded-Forheader takes precedence over the configuredkeyBy(Function)resolver. Only opt in when a trusted reverse proxy strips and rewrites the header; otherwise clients can spoof it to defeat IP-based throttling.- Parameters:
value-true to honorX-Forwarded-For.- Returns:
- This object.
-
exemptPaths
Sets the set of request paths that bypass throttling.Matched against
HttpServletRequestWrapper.getPathInfo()andRestRequest.getServletPath()(exact match). Defaults to/healthz,/readyz,/livez— the built-in probe paths. Pass an empty array to clear the defaults.- Parameters:
values- Exempt paths. Must not benull .- Returns:
- This object.
-
whenLimitExceeded
public RateLimitGuard.Builder whenLimitExceeded(BiConsumer<RestRequest, RateLimitGuard.RateLimitInfo> value) Sets a callback invoked when a request is rejected due to an empty bucket.Useful for structured logging or metrics emission. The callback runs before
TooManyRequestsis thrown and must not block. Exceptions raised inside the callback propagate to the caller.- Parameters:
value- Callback. Must not benull .- Returns:
- This object.
-
storage
Sets the bucket-state storage backend.Default is
RateLimitGuard.Storage.inMemory(). Substitute a distributed backend (Redis, DynamoDB, etc.) when running multiple pods that share a single rate envelope.- Parameters:
value- Storage backend. Must not benull .- Returns:
- This object.
-
build
Builds the guard.- Returns:
- A new
RateLimitGuard.
-