Java Code Examples for com.google.api.client.http.HttpRequest#setNumberOfRetries()

The following examples show how to use com.google.api.client.http.HttpRequest#setNumberOfRetries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryOnUnspecifiedHttpStatus() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(404);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(404, e.getStatusCode());
  }

  assertEquals(0, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 2
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOnHttpClientErrorWhenSpecified() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(429);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(429, e.getStatusCode());
  }

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertArrayEquals(new long[]{500, 1000, 2000, 4000}, sleeper.getDelays());
  assertEquals(MAX_RETRIES + 1, failingRequest.getCount());
}
 
Example 3
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testExponentialBackOffDoesNotExceedMaxInterval() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(10);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(10, sleeper.getCount());
  assertArrayEquals(
      new long[]{500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 120000, 120000},
      sleeper.getDelays());
  assertEquals(11, failingRequest.getCount());
}
 
Example 4
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryAfterGivenAsSeconds() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(
      503, ImmutableMap.of("retry-after", "2"));
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(MAX_RETRIES, sleeper.getCount());
  assertArrayEquals(new long[]{2000, 2000, 2000, 2000}, sleeper.getDelays());
  assertEquals(MAX_RETRIES + 1, failingRequest.getCount());
}
 
Example 5
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidRetryAfterFailsOverToExpBackOff() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(
      503, ImmutableMap.of("retry-after", "not valid"));
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(4);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(4, sleeper.getCount());
  assertArrayEquals(new long[]{500, 1000, 2000, 4000}, sleeper.getDelays());
  assertEquals(5, failingRequest.getCount());
}
 
Example 6
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryWhenRetryAfterIsTooLong() throws IOException {
  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(
      503, ImmutableMap.of("retry-after", "121"));
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(0, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 7
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryAfterInterruption() throws IOException {
  MockSleeper sleeper = new MockSleeper() {
    @Override
    public void sleep(long millis) throws InterruptedException {
      super.sleep(millis);
      throw new InterruptedException();
    }
  };
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(1, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 8
Source File: RetryInitializer.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) {
  request.setNumberOfRetries(retryConfig.getMaxRetries());
  request.setUnsuccessfulResponseHandler(newUnsuccessfulResponseHandler(request));
  if (retryConfig.isRetryOnIOExceptions()) {
    request.setIOExceptionHandler(newIOExceptionHandler());
  }
}
 
Example 9
Source File: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryAfterGivenAsDate() throws IOException {
  SimpleDateFormat dateFormat =
          new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
  dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  Date date = new Date(1000);
  Clock clock = new FixedClock(date.getTime());
  String retryAfter = dateFormat.format(new Date(date.getTime() + 30000));

  MultipleCallSleeper sleeper = new MultipleCallSleeper();
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper), clock);
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(
      503, ImmutableMap.of("retry-after", retryAfter));
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(4);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(4, sleeper.getCount());
  assertArrayEquals(new long[]{30000, 30000, 30000, 30000}, sleeper.getDelays());
  assertEquals(5, failingRequest.getCount());
}
 
Example 10
Source File: HttpHandler.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/** Sets attributes of the request that are common to all requests for this handler. */
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
  // Do not throw if execute fails, since Axis will handle unmarshalling the
  // fault.
  httpRequest.setThrowExceptionOnExecuteError(false);

  // For consistency with the default Axis HTTPSender and CommonsHTTPSender, do not
  // follow redirects.
  httpRequest.setFollowRedirects(false);

  // Retry should be handled by the client.
  httpRequest.setNumberOfRetries(0);
}
 
Example 11
Source File: HttpClientRequestInitializer.java    From steem-java-api-wrapper with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
    request.setConnectTimeout(SteemJConfig.getInstance().getIdleTimeout());
    request.setReadTimeout(SteemJConfig.getInstance().getResponseTimeout());
    request.setNumberOfRetries(0);
}
 
Example 12
Source File: RetryHttpInitializer.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(HttpRequest request) {
  // Credential must be the interceptor to fill in accessToken fields.
  request.setInterceptor(credential);

  // Request will be retried if server errors (5XX) or I/O errors are encountered.
  request.setNumberOfRetries(options.getMaxRequestRetries());

  // Set the timeout configurations.
  request.setConnectTimeout(Math.toIntExact(options.getConnectTimeout().toMillis()));
  request.setReadTimeout(Math.toIntExact(options.getReadTimeout().toMillis()));

  // IOExceptions such as "socket timed out" of "insufficient bytes written" will follow a
  // straightforward backoff.
  HttpBackOffIOExceptionHandler exceptionHandler =
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff());
  if (sleeperOverride != null) {
    exceptionHandler.setSleeper(sleeperOverride);
  }

  // Supply a new composite handler for unsuccessful return codes. 401 Unauthorized will be
  // handled by the Credential, 410 Gone will be logged, and 5XX will be handled by a backoff
  // handler.
  LoggingResponseHandler loggingResponseHandler =
      new LoggingResponseHandler(
          new CredentialOrBackoffResponseHandler(),
          exceptionHandler,
          ImmutableSet.of(HttpStatus.SC_GONE, HttpStatus.SC_SERVICE_UNAVAILABLE),
          ImmutableSet.of(HTTP_SC_TOO_MANY_REQUESTS));
  request.setUnsuccessfulResponseHandler(loggingResponseHandler);
  request.setIOExceptionHandler(loggingResponseHandler);

  if (isNullOrEmpty(request.getHeaders().getUserAgent())
      && !isNullOrEmpty(options.getDefaultUserAgent())) {
    logger.atFiner().log(
        "Request is missing a user-agent, adding default value of '%s'",
        options.getDefaultUserAgent());
    request.getHeaders().setUserAgent(options.getDefaultUserAgent());
  }

  request.getHeaders().putAll(options.getHttpHeaders());
}