Combined approach of slidingLogs and fixedWindow with lower storage costs than slidingLogs and improved boundary behavior by calculating a weighted score between two windows.

Pro:

Good performance allows this to scale to very high loads.

Con:

Nothing major.

IMPORT_PATH: "@daiso-tech/core/rate-limiter/policies"

Implements

Constructors

Methods

  • Extracts the number of attempts used from the current metrics. Used by the adapter to provide state information to callers. This is the current attempt count within the configured limit.

    Parameters

    • currentMetrics: Partial<Record<number, number>>

      The current tracking metrics

    • currentDate: Date

      Current date/time for calculations (e.g., accounting for expired requests)

    Returns number

    Number of attempts consumed in the current window

  • Calculates the expiration date for the current metrics. Determines when the rate limiter state should be automatically cleaned up.

    This method is critical for persistence: data persisted beyond this date should be deleted. If this method returns a date in the past, the adapter should treat the rate limiter as expired and allow requests with fresh metrics calculation.

    Common patterns:

    • Fixed-window: Return date of window end (e.g., start + duration)
    • Sliding-window: Return earliest tracked request + duration

    Parameters

    • _currentMetrics: Partial<Record<number, number>>
    • currentDate: Date

      Current date/time for calculating expiration

    Returns Date

    Date when this rate limiter state should expire and be cleaned up

  • Creates initial metrics for a new rate limiter. Called when initializing a rate limiter for the first time or after expiration. Should set up the data structures needed to track attempts in the current window.

    Parameters

    • currentDate: Date

      Current date/time for initializing window boundaries

    Returns Partial<Record<number, number>>

    Fresh metrics object ready to track attempts

  • Determines if the rate limiter should block the current request. Evaluates current metrics against the configured limit. Returns true if the attempt limit has been exceeded (request should be blocked).

    Parameters

    • currentMetrics: Partial<Record<number, number>>

      The current tracking metrics

    • limit: number

      Maximum allowed attempts in the window

    • currentDate: Date

      Current date/time for window boundary checks

    Returns boolean

    true if request should be blocked, false if allowed

  • Updates metrics to record a new attempt. Called each time a request is evaluated by the rate limiter. Must return a new metrics object; the input should not be mutated.

    The returned metrics are stored and used for subsequent evaluations. For persistent storage, this updated state is serialized and persisted.

    Parameters

    • currentMetrics: Partial<Record<number, number>>

      The metrics before this attempt

    • currentDate: Date

      Current date/time for the attempt

    Returns Partial<Record<number, number>>

    New metrics object with the attempt recorded