Class InMemoryRateLimitStore
java.lang.Object
com.svenruppert.jsentinel.ratelimiting.InMemoryRateLimitStore
- All Implemented Interfaces:
RateLimitStore
@ExperimentalJSentinelApi
public final class InMemoryRateLimitStore
extends Object
implements RateLimitStore
In-memory
RateLimitStore backed by a per-key
ConcurrentLinkedDeque of timestamps.
recordEvent(RateLimitKey, Instant) appends to the tail in
amortised O(1). countSince(RateLimitKey, Instant) walks the
deque tail-to-head and stops once an entry before the cutoff is
found — assumes timestamps are recorded in approximately
monotonically-increasing order, which is the standard case for
sliding-window throttling.
Retention is the application's responsibility via
purgeOlderThan(Instant); without periodic purges the
deques would grow unboundedly under sustained traffic.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcountSince(RateLimitKey key, Instant since) Returns the number of events underkeywhose timestamp is at or aftersince.intpurgeOlderThan(Instant cutoff) Across every key, drops events whose timestamp is strictly beforecutoff.voidrecordEvent(RateLimitKey key, Instant at) Appends one event timestamp underkey.voidreset(RateLimitKey key) Drops every recorded event underkey.
-
Constructor Details
-
InMemoryRateLimitStore
public InMemoryRateLimitStore()Creates an empty store.
-
-
Method Details
-
recordEvent
Description copied from interface:RateLimitStoreAppends one event timestamp underkey.- Specified by:
recordEventin interfaceRateLimitStore- Parameters:
key- key the event belongs to; must not benullat- event instant; must not benull
-
countSince
Description copied from interface:RateLimitStoreReturns the number of events underkeywhose timestamp is at or aftersince.- Specified by:
countSincein interfaceRateLimitStore- Parameters:
key- key to query; must not benullsince- inclusive lower bound; events at exactly this instant count; must not benull- Returns:
- non-negative event count
-
reset
Description copied from interface:RateLimitStoreDrops every recorded event underkey.- Specified by:
resetin interfaceRateLimitStore- Parameters:
key- key to clear; must not benull
-
purgeOlderThan
Description copied from interface:RateLimitStoreAcross every key, drops events whose timestamp is strictly beforecutoff. Intended as a retention sweep.- Specified by:
purgeOlderThanin interfaceRateLimitStore- Parameters:
cutoff- retention boundary; must not benull- Returns:
- number of events purged
-