Class BatchedFixedIntervalInvocationGate

  • All Implemented Interfaces:
    InvocationGate

    public class BatchedFixedIntervalInvocationGate
    extends Object
    implements InvocationGate
    An InvocationGate that allows a fixed number of successive callers to proceed, then enforces a lull of a fixed Duration.

    Callers on a hot path use isTooSoon(long) to decide whether to skip a costly operation. Up to N invocations may receive false (proceed) in succession. After the N-th success, the gate enters a lull of increment milliseconds during which all callers receive true (skip). When the lull ends, another batch of N is available.

    This extends the idea of FixedIntervalInvocationGate (at most one success per interval) to batches of size N. Token accounting and the lull deadline use atomics so concurrent callers share one batch.

    Since:
    1.6.3
    Author:
    Ceki Gülcü
    See Also:
    InvocationGate, FixedIntervalInvocationGate
    • Method Detail

      • isTooSoon

        public boolean isTooSoon​(long currentTime)
        Returns true if the caller should skip further work; false if this call is allowed to proceed.

        If currentTime is InvocationGate.TIME_UNAVAILABLE (-1), returns false so work can still run when the clock is unavailable.

        During a lull (currentTime before the next open time), returns true. Otherwise tries to consume one batch token. The first batchSize successful consumptions return false. The caller that takes the last token starts a lull of increment and re-arms the batch for after the lull.

        Specified by:
        isTooSoon in interface InvocationGate
        Parameters:
        currentTime - current time in milliseconds, or InvocationGate.TIME_UNAVAILABLE if unknown
        Returns:
        true if further work should be skipped; false if the caller may proceed