View Javadoc

1   package com.atlassian.asap.core.keys.publickey;
2   
3   import com.google.common.base.Preconditions;
4   import org.apache.http.HttpResponse;
5   import org.apache.http.client.ServiceUnavailableRetryStrategy;
6   import org.apache.http.protocol.HttpContext;
7   
8   /**
9    * A strategy that follows the best practices defined by Amazon for S3 buckets.
10   *
11   * <p><a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/ErrorBestPractices.html">Amazon S3 error handling best
12   * practices</a> recommend to retry any 5xx error when talking to S3. The
13   * {@link org.apache.http.impl.client.DefaultServiceUnavailableRetryStrategy} provided by the Apache HTTP client
14   * library does not cover all 5xx errors, only 503.
15   *
16   * <p>This class does not currently implement exponential backoff or jitter.
17   */
18  public class S3ServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
19      private final int maxRetries;
20      private final long retryInterval;
21  
22      /**
23       * @param maxRetries    maximum number of attempts to make the request
24       * @param retryInterval interval (in ms) to wait between retries
25       */
26      public S3ServiceUnavailableRetryStrategy(int maxRetries, long retryInterval) {
27          Preconditions.checkArgument(maxRetries >= 0, "maxRetries must be non-negative");
28          Preconditions.checkArgument(retryInterval >= 0, "retryInterval must be non-negative");
29  
30          this.maxRetries = maxRetries;
31          this.retryInterval = retryInterval;
32      }
33  
34      @Override
35      public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
36          return response.getStatusLine().getStatusCode() / 100 == 5 &&
37                  executionCount <= maxRetries;
38      }
39  
40      @Override
41      public long getRetryInterval() {
42          return retryInterval;
43      }
44  }