Class BackoffThrottler


  • @NotThreadSafe
    public final class BackoffThrottler
    extends java.lang.Object
    Used to throttle code execution in presence of failures using exponential backoff logic.

    The formula used to calculate the next sleep interval is:

     jitter = random number in the range [-maxJitterCoefficient, +maxJitterCoefficient];
     sleepTime = min(pow(backoffCoefficient, failureCount - 1) * initialSleep * (1 + jitter), maxSleep);
     
    where initialSleep is either set to regularInitialSleep or congestionInitialSleep based on the most recent failure. Note that it means that attempt X can possibly get a shorter throttle than attempt X-1, if a non-congestion failure occurs after a congestion failure. This is the expected behaviour for all SDK.

    Example usage:

    
     BackoffThrottler throttler = new BackoffThrottler(50, 1000, 60000, 2, 0.1);
     while(!stopped) {
         try {
             long throttleMs = throttler.getSleepTime();
             if (throttleMs > 0) {
                 Thread.sleep(throttleMs);
             }
             // some code that can fail and should be throttled
             ...
             throttler.success();
         }
         catch (Exception e) {
             throttler.failure(
                 (e instanceof StatusRuntimeException)
                     ? ((StatusRuntimeException) e).getStatus().getCode()
                     : Status.Code.UNKNOWN);
         }
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      BackoffThrottler​(java.time.Duration regularInitialSleep, java.time.Duration congestionInitialSleep, java.time.Duration maxSleep, double backoffCoefficient, double maxJitterCoefficient)
      Construct an instance of the throttler.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void failure​(io.grpc.Status.Code failureCode)
      Increment failure count and set last failure code.
      int getAttemptCount()  
      long getSleepTime()  
      void success()
      Reset failure count to 0 and clear last failure code.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BackoffThrottler

        public BackoffThrottler​(java.time.Duration regularInitialSleep,
                                java.time.Duration congestionInitialSleep,
                                @Nullable
                                java.time.Duration maxSleep,
                                double backoffCoefficient,
                                double maxJitterCoefficient)
        Construct an instance of the throttler.
        Parameters:
        regularInitialSleep - time to sleep on the first failure (assuming regular failures)
        congestionInitialSleep - time to sleep on the first failure (for congestion failures)
        maxSleep - maximum time to sleep independently of number of failures
        backoffCoefficient - coefficient used to calculate the next time to sleep
        maxJitterCoefficient - maximum jitter coefficient (in the range [0.0, 1.0[) to randomly add or subtract to sleep time
    • Method Detail

      • getSleepTime

        public long getSleepTime()
      • getAttemptCount

        public int getAttemptCount()
      • success

        public void success()
        Reset failure count to 0 and clear last failure code.
      • failure

        public void failure​(io.grpc.Status.Code failureCode)
        Increment failure count and set last failure code.