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 Details

    • InMemoryRateLimitStore

      public InMemoryRateLimitStore()
      Creates an empty store.
  • Method Details

    • recordEvent

      public void recordEvent(RateLimitKey key, Instant at)
      Description copied from interface: RateLimitStore
      Appends one event timestamp under key.
      Specified by:
      recordEvent in interface RateLimitStore
      Parameters:
      key - key the event belongs to; must not be null
      at - event instant; must not be null
    • countSince

      public int countSince(RateLimitKey key, Instant since)
      Description copied from interface: RateLimitStore
      Returns the number of events under key whose timestamp is at or after since.
      Specified by:
      countSince in interface RateLimitStore
      Parameters:
      key - key to query; must not be null
      since - inclusive lower bound; events at exactly this instant count; must not be null
      Returns:
      non-negative event count
    • reset

      public void reset(RateLimitKey key)
      Description copied from interface: RateLimitStore
      Drops every recorded event under key.
      Specified by:
      reset in interface RateLimitStore
      Parameters:
      key - key to clear; must not be null
    • purgeOlderThan

      public int purgeOlderThan(Instant cutoff)
      Description copied from interface: RateLimitStore
      Across every key, drops events whose timestamp is strictly before cutoff. Intended as a retention sweep.
      Specified by:
      purgeOlderThan in interface RateLimitStore
      Parameters:
      cutoff - retention boundary; must not be null
      Returns:
      number of events purged