Class InMemoryRateLimitPolicy

java.lang.Object
com.svenruppert.jsentinel.ratelimiting.InMemoryRateLimitPolicy
All Implemented Interfaces:
RateLimitPolicy

@ExperimentalJSentinelApi public final class InMemoryRateLimitPolicy extends Object implements RateLimitPolicy
Default 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 Details

    • InMemoryRateLimitPolicy

      public InMemoryRateLimitPolicy(RateLimitStore store, JSentinelAuditService auditService, int limit, Duration window)
      Parameters:
      store - non-null
      auditService - non-null
      limit - max events per window; strictly positive
      window - 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-null
      auditService - non-null
      limit - max events per window; strictly positive
      window - sliding-window duration; non-null, strictly positive
      clock - time source; non-null
  • Method Details

    • tryAcquire

      public RateLimitDecision tryAcquire(RateLimitKey key)
      Description copied from interface: RateLimitPolicy
      Attempts to record one event under key against 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:
      tryAcquire in interface RateLimitPolicy
      Parameters:
      key - non-null key; the scope is the application's choice — IP, subject, endpoint, or composed
      Returns:
      admit / throttle decision, never null
    • reset

      public void reset(RateLimitKey key)
      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 than now - window across all keys. Idempotent; safe to schedule.
      Returns:
      number of events removed
    • limit

      public int limit()
      Returns:
      configured per-window limit
    • window

      public Duration window()
      Returns:
      configured window duration