Interface SchedulingBucket

    • Method Detail

      • tryConsume

        CompletableFuture<Boolean> tryConsume​(long numTokens,
                                              long maxWaitNanos,
                                              ScheduledExecutorService scheduler)
        Tries to consume the specified number of tokens from the bucket.

        The algorithm for all type of buckets is following:

        • Implementation issues asynchronous request to back-end behind the bucket(for local bucket it is just a synchronous call) in way which specific for each particular back-end.
        • Then uncompleted future returned to the caller.
        • If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
        • When back-end provides signal(through callback) that request is done(for local bucket response got immediately), then following post-processing rules will be applied:
          • If tokens were consumed then future immediately completed by true.
          • If tokens were not consumed because were not enough tokens in the bucket and maxWaitNanos nanoseconds is not enough time to refill deficit, then future immediately completed by false.
          • If tokens were reserved(effectively consumed) then task to delayed completion will be scheduled to the scheduler via ScheduledExecutorService.schedule(Runnable, long, TimeUnit), when delay equals to time required to refill the deficit of tokens. After scheduler executes task the future completed by true.
        It is strongly not recommended to do any heavy work in thread which completes the future, because typically this will be a back-end thread which handles NIO selectors, blocking this thread will take negative performance effect to back-end throughput, so you always should resume control flow in another executor via methods like CompletableFuture.thenApplyAsync(Function, Executor).
        Parameters:
        numTokens - The number of tokens to consume from the bucket.
        maxWaitNanos - limit of time(in nanoseconds) which thread can wait.
        scheduler - used to delayed future completion
      • consume

        CompletableFuture<Void> consume​(long numTokens,
                                        ScheduledExecutorService scheduler)
        Consumes the specified number of tokens from the bucket.

        The algorithm for all type of buckets is following:

        • Implementation issues asynchronous request to back-end behind the bucket(for local bucket it is just a synchronous call) in way which specific for each particular back-end.
        • Then uncompleted future returned to the caller.
        • If back-end provides signal(through callback) that asynchronous request failed, then future completed exceptionally.
        • When back-end provides signal(through callback) that request is done(for local bucket response got immediately), then following post-processing rules will be applied:
          • If tokens were consumed then future immediately completed.
          • Else tokens reserved(effectively consumed) and task to delayed completion will be scheduled to the scheduler via ScheduledExecutorService.schedule(Runnable, long, TimeUnit), when delay equals to time required to refill the deficit of tokens. After scheduler executes task the future completed.
        It is strongly not recommended to do any heavy work in thread which completes the future, because typically this will be a back-end thread which handles NIO selectors, blocking this thread will take negative performance effect to back-end throughput, so you always should resume control flow in another executor via methods like CompletableFuture.thenApplyAsync(Function, Executor).
        Parameters:
        numTokens - The number of tokens to consume from the bucket.
        scheduler - used to delayed future completion