Class InMemoryRateLimitPolicy
- All Implemented Interfaces:
RateLimitPolicy
RateLimitPolicy backed by a RateLimitStore.
Sliding-window semantics: every tryAcquire
counts the events recorded in the last window; if the
count is below the configured limit a fresh event is
recorded and RateLimitDecision.Allowed is returned;
otherwise the request is refused with
RateLimitDecision.Throttled and a
RateLimitExceeded audit event is published.
The audit emit-side optionally derives a subjectId
from the RateLimitKey.scope() when the scope begins
with the "subject:" prefix — purely a convenience so
audit consumers can pivot on subject without parsing the
composite scope themselves. For other scopes the audit event's
subjectId is empty.
Thread-safe via the underlying store.
For brute-force / password-guessing detection, use
LoginAttemptPolicy instead — same shape but with progressive
backoff and lockout semantics. This policy is for steady-state
traffic shaping.
-
Constructor Summary
ConstructorsConstructorDescriptionInMemoryRateLimitPolicy(RateLimitStore store, JSentinelAuditService auditService, int limit, Duration window) InMemoryRateLimitPolicy(RateLimitStore store, JSentinelAuditService auditService, int limit, Duration window, Clock clock) Full constructor. -
Method Summary
Modifier and TypeMethodDescriptionintlimit()intConvenience: purges every event older thannow - windowacross all keys.voidreset(RateLimitKey key) Resets the per-key state — typically after a successful authentication that cancels the throttle.tryAcquire(RateLimitKey key) Attempts to record one event underkeyagainst the policy's configured per-window limit.window()
-
Constructor Details
-
InMemoryRateLimitPolicy
public InMemoryRateLimitPolicy(RateLimitStore store, JSentinelAuditService auditService, int limit, Duration window) - Parameters:
store- non-nullauditService- non-nulllimit- max events perwindow; strictly positivewindow- sliding-window duration; non-null, strictly positive
-
InMemoryRateLimitPolicy
public InMemoryRateLimitPolicy(RateLimitStore store, JSentinelAuditService auditService, int limit, Duration window, Clock clock) Full constructor.- Parameters:
store- non-nullauditService- non-nulllimit- max events perwindow; strictly positivewindow- sliding-window duration; non-null, strictly positiveclock- time source; non-null
-
-
Method Details
-
tryAcquire
Description copied from interface:RateLimitPolicyAttempts to record one event underkeyagainst the policy's configured per-window limit. The default behaviour mirrors a leaky bucket: an admitted event is counted, a throttled event is not. Concrete implementations document their semantics.- Specified by:
tryAcquirein interfaceRateLimitPolicy- Parameters:
key- non-null key; the scope is the application's choice — IP, subject, endpoint, or composed- Returns:
- admit / throttle decision, never
null
-
reset
Resets the per-key state — typically after a successful authentication that cancels the throttle.- Parameters:
key- non-null
-
purgeOldEvents
public int purgeOldEvents()Convenience: purges every event older thannow - windowacross all keys. Idempotent; safe to schedule.- Returns:
- number of events removed
-
limit
public int limit()- Returns:
- configured per-window limit
-
window
- Returns:
- configured window duration
-