Class NonBlockingRateLimiter


  • @ThreadSafe
    public class NonBlockingRateLimiter
    extends java.lang.Object
    A rate limiter implementation that allows callers to reserve permits that may only be available in the future, delegating to them decisions about how to schedule/delay work and whether or not to block execution to do so.
    • Field Detail

      • NANOS_PER_SECOND

        public static final long NANOS_PER_SECOND
    • Constructor Detail

      • NonBlockingRateLimiter

        public NonBlockingRateLimiter​(int permitsPerSecond)
      • NonBlockingRateLimiter

        public NonBlockingRateLimiter​(int permitsPerSecond,
                                      long burstNanos,
                                      com.google.common.base.Ticker ticker)
    • Method Detail

      • setRate

        public void setRate​(int permitsPerSecond)
      • setRate

        public void setRate​(int permitsPerSecond,
                            com.google.common.base.Ticker ticker)
      • getRate

        public int getRate()
        Returns:
        the number of available permits per second
      • reserveAndGetDelay

        public long reserveAndGetDelay​(java.util.concurrent.TimeUnit delayUnit)
        Reserves a single permit slot on the timeline which may not yet be available.
        Returns:
        time until the reserved permit will be available (or zero if it already is) in the specified units
      • tryReserve

        public boolean tryReserve()
        Reserves a single permit slot on the timeline, but only if one is available.
        Returns:
        true if a permit is available, false if one is not
      • getIntervalNanos

        public long getIntervalNanos()
      • getStartedNanos

        public long getStartedNanos()