int getBackoffIntervalInSeconds()

in src/main/java/com/uber/cadence/internal/testservice/RetryState.java [58:105]


  int getBackoffIntervalInSeconds(String errReason, long currentTimeMillis) {
    RetryPolicy retryPolicy = getRetryPolicy();
    long expirationTime = getExpirationTime();
    if (retryPolicy.getMaximumAttempts() == 0 && expirationTime == 0) {
      return 0;
    }

    if (retryPolicy.getMaximumAttempts() > 0
        && getAttempt() >= retryPolicy.getMaximumAttempts() - 1) {
      // currAttempt starts from 0.
      // MaximumAttempts is the total attempts, including initial (non-retry) attempt.
      return 0;
    }
    long initInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getInitialIntervalInSeconds());
    long nextInterval =
        (long) (initInterval * Math.pow(retryPolicy.getBackoffCoefficient(), getAttempt()));
    long maxInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getMaximumIntervalInSeconds());
    if (nextInterval <= 0) {
      // math.Pow() could overflow
      if (maxInterval > 0) {
        nextInterval = maxInterval;
      } else {
        return 0;
      }
    }

    if (maxInterval > 0 && nextInterval > maxInterval) {
      // cap next interval to MaxInterval
      nextInterval = maxInterval;
    }

    long backoffInterval = nextInterval;
    long nextScheduleTime = currentTimeMillis + backoffInterval;
    if (expirationTime != 0 && nextScheduleTime > expirationTime) {
      return 0;
    }

    // check if error is non-retriable
    List<String> nonRetriableErrorReasons = retryPolicy.getNonRetriableErrorReasons();
    if (nonRetriableErrorReasons != null) {
      for (String err : nonRetriableErrorReasons) {
        if (errReason.equals(err)) {
          return 0;
        }
      }
    }
    return (int) TimeUnit.MILLISECONDS.toSeconds((long) Math.ceil((double) backoffInterval));
  }