Пример #1
0
  /**
   * Returns true if a failed request should be retried.
   *
   * @param originalRequest The original service request that is being executed.
   * @param method The current HTTP method being executed.
   * @param exception The client/service exception from the failed request.
   * @param requestCount The number of times the current request has been attempted.
   * @return True if the failed request should be retried.
   */
  private boolean shouldRetry(
      AmazonWebServiceRequest originalRequest,
      HttpRequestBase method,
      AmazonClientException exception,
      int requestCount,
      RetryPolicy retryPolicy) {
    final int retries = requestCount - 1;

    int maxErrorRetry = config.getMaxErrorRetry();
    // We should use the maxErrorRetry in
    // the RetryPolicy if either the user has not explicitly set it in
    // ClientConfiguration, or the RetryPolicy is configured to take
    // higher precedence.
    if (maxErrorRetry < 0 || !retryPolicy.isMaxErrorRetryInClientConfigHonored()) {
      maxErrorRetry = retryPolicy.getMaxErrorRetry();
    }

    // Immediately fails when it has exceeds the max retry count.
    if (retries >= maxErrorRetry) return false;

    // Never retry on requests containing non-repeatable entity
    if (method instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) method).getEntity();
      if (entity != null && !entity.isRepeatable()) {
        if (log.isDebugEnabled()) {
          log.debug("Entity not repeatable");
        }
        return false;
      }
    }

    // Pass all the context information to the RetryCondition and let it
    // decide whether it should be retried.
    return retryPolicy.getRetryCondition().shouldRetry(originalRequest, exception, retries);
  }
Пример #2
0
  /**
   * Sleep for a period of time on failed request to avoid flooding a service with retries.
   *
   * @param originalRequest The original service request that is being executed.
   * @param previousException Exception information for the previous attempt, if any.
   * @param requestCount current request count (including the next attempt after the delay)
   * @param retryPolicy The retry policy configured in this http client.
   */
  private void pauseBeforeNextRetry(
      AmazonWebServiceRequest originalRequest,
      AmazonClientException previousException,
      int requestCount,
      RetryPolicy retryPolicy) {
    final int retries =
        requestCount // including next attempt
            - 1 // number of attempted requests
            - 1; // number of attempted retries

    long delay =
        retryPolicy
            .getBackoffStrategy()
            .delayBeforeNextRetry(originalRequest, previousException, retries);

    if (log.isDebugEnabled()) {
      log.debug(
          "Retriable error detected, "
              + "will retry in "
              + delay
              + "ms, attempt number: "
              + retries);
    }

    try {
      Thread.sleep(delay);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new AmazonClientException(e.getMessage(), e);
    }
  }