Class RateLimiter<T>
java.lang.Object
io.github.teceli.resiliencia.patterns.ratelimiter.RateLimiter<T>
- All Implemented Interfaces:
Resilient<T>
RateLimiter pattern: bound how many calls may start per time window (fixed window,
limit calls per period). Windows are aligned to the instant the limiter was
created and advance in whole periods, as measured by the Clock.
Callers over the limit either fail fast with RateLimiterException (default,
maxWait zero) or wait via Clock.sleep(long) for up to maxWait until the
next window opens; blocking a virtual thread is cheap.
Holds live state (the current window and its used permits). Immutable in configuration and
thread-safe by design — share one instance across all callers that must compete for the same
budget. Each withX method returns a new, independent RateLimiter with a fresh window.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.teceli.resiliencia.core.api.Resilient
Resilient.Operation<T> -
Method Summary
Modifier and TypeMethodDescriptioncall(Resilient.Operation<T> operation) Execute an operation with resilience guarantees.intlimit()The configured maximum number of calls perperiod.maxWait()How long an excess call may wait for the next window before being rejected.name()The name identifying this rate limiter instance, used in events and rejection exceptions.static <T> RateLimiter<T> ARateLimiterallowinglimitcalls perperiod, rejecting excess calls immediately (maxWaitzero).outcome(Resilient.Operation<T> operation) Execute an operation and capture the result or failure as an Outcome.The kind of this pattern, used for internal comparisons (e.g.The name of this pattern, e.g.period()The configured length of the fixed window.Use a customClockinstead of the system clock, e.g. a manual/virtual clock in tests to make window and wait assertions deterministic and instant.withLimit(int limit) Maximum number of calls allowed perperiod.withListener(ResilienceEvent.Listener listener) Add a listener notified of everyRateLimiterEventemitted by this instance.withMaxWait(Duration maxWait) How long an excess call may wait for the next window before being rejected.withPeriod(Duration period) Length of the fixed window over whichlimitcalls are allowed.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.github.teceli.resiliencia.core.api.Resilient
callAsync, hasOwnDeadline
-
Method Details
-
of
ARateLimiterallowinglimitcalls perperiod, rejecting excess calls immediately (maxWaitzero). Refine viawithXmethods, e.g.withMaxWait(java.time.Duration)to let excess calls wait for the next window instead. -
withLimit
Maximum number of calls allowed perperiod. Must be at least 1. -
withPeriod
Length of the fixed window over whichlimitcalls are allowed. Must be positive. -
withMaxWait
How long an excess call may wait for the next window before being rejected. Zero (the default) rejects immediately. -
withListener
Add a listener notified of everyRateLimiterEventemitted by this instance. Listener exceptions are logged and otherwise ignored — a broken listener never affects the outcome. -
withClock
Use a customClockinstead of the system clock, e.g. a manual/virtual clock in tests to make window and wait assertions deterministic and instant. -
name
The name identifying this rate limiter instance, used in events and rejection exceptions. -
limit
public int limit()The configured maximum number of calls perperiod. -
period
The configured length of the fixed window. -
maxWait
How long an excess call may wait for the next window before being rejected. -
patternName
Description copied from interface:ResilientThe name of this pattern, e.g. "retry", "timeout", "circuit-breaker". Used for identification (e.g. by Policy) without coupling to concrete pattern types. Defaults to "custom" for user-defined Resilient implementations.- Specified by:
patternNamein interfaceResilient<T>
-
patternKind
Description copied from interface:ResilientThe kind of this pattern, used for internal comparisons (e.g. Policy order validation). UnlikeResilient.patternName(), which is a free-form observability label, this is a closed enum the library can reason about exhaustively. Defaults toPatternKind.CUSTOMfor user-defined Resilient implementations.- Specified by:
patternKindin interfaceResilient<T>
-
call
Description copied from interface:ResilientExecute an operation with resilience guarantees. May throw ResilienciaException or a specific pattern exception.- Specified by:
callin interfaceResilient<T>- Throws:
ResilientException
-
outcome
Description copied from interface:ResilientExecute an operation and capture the result or failure as an Outcome. Never throws for a recordedException— always returns Success, Failure, or a pattern-specific outcome. AnErrorthrown by the operation propagates uncaught instead of being captured as a Failure: fatal JVM conditions (e.g.OutOfMemoryError) should not be treated as a recoverable result.
-