com.google.api.client.http.HttpBackOffIOExceptionHandler Java Examples

The following examples show how to use com.google.api.client.http.HttpBackOffIOExceptionHandler. 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: RetryHttpInitializerWrapper.java    From deployment-examples with MIT License 6 votes vote down vote up
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
  request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
  final HttpUnsuccessfulResponseHandler backoffHandler =
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
  request.setInterceptor(wrappedCredential);
  request.setUnsuccessfulResponseHandler(
      (request1, response, supportsRetry) -> {
        if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
          // If credential decides it can handle it, the return code or message indicated
          // something specific to authentication, and no backoff is desired.
          return true;
        } else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
          // Otherwise, we defer to the judgement of our internal backoff handler.
          LOG.info("Retrying " + request1.getUrl().toString());
          return true;
        } else {
          return false;
        }
      });
  request.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
 
Example #2
Source File: FirebaseRequestInitializerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryConfigWithIOExceptionHandling() throws Exception {
  FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
      .setCredentials(new MockGoogleCredentials("token"))
      .build());
  RetryConfig retryConfig = RetryConfig.builder()
      .setMaxRetries(MAX_RETRIES)
      .setRetryOnIOExceptions(true)
      .build();
  HttpRequest request = TestUtils.createRequest();

  FirebaseRequestInitializer initializer = new FirebaseRequestInitializer(app, retryConfig);
  initializer.initialize(request);

  assertEquals(0, request.getConnectTimeout());
  assertEquals(0, request.getReadTimeout());
  assertEquals("Bearer token", request.getHeaders().getAuthorization());
  assertEquals(MAX_RETRIES, request.getNumberOfRetries());
  assertTrue(request.getIOExceptionHandler() instanceof HttpBackOffIOExceptionHandler);
  assertNotNull(request.getUnsuccessfulResponseHandler());
}
 
Example #3
Source File: RetryHttpInitializerWrapper.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
  request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
  final HttpUnsuccessfulResponseHandler backoffHandler =
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
  request.setInterceptor(wrappedCredential);
  request.setUnsuccessfulResponseHandler(
      (request1, response, supportsRetry) -> {
        if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
          // If credential decides it can handle it, the return code or message indicated
          // something specific to authentication, and no backoff is desired.
          return true;
        } else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
          // Otherwise, we defer to the judgement of our internal backoff handler.
          LOG.info("Retrying " + request1.getUrl().toString());
          return true;
        } else {
          return false;
        }
      });
  request.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
 
Example #4
Source File: RetryInitializerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnableRetry() throws IOException {
  RetryConfig retryConfig = retryOnIOAndServiceUnavailableErrors(new MockSleeper());
  RetryInitializer initializer = new RetryInitializer(retryConfig);
  HttpRequest request = TestUtils.createRequest();

  initializer.initialize(request);

  assertEquals(MAX_RETRIES, request.getNumberOfRetries());
  assertTrue(request.getUnsuccessfulResponseHandler() instanceof RetryHandlerDecorator);
  RetryUnsuccessfulResponseHandler retryHandler =
      ((RetryHandlerDecorator) request.getUnsuccessfulResponseHandler()).getRetryHandler();
  assertSame(retryConfig, retryHandler.getRetryConfig());
  assertTrue(request.getIOExceptionHandler() instanceof HttpBackOffIOExceptionHandler);
}
 
Example #5
Source File: HttpClient.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private HttpRequestFactory createRequestFactory() {
    return new NetHttpTransport().createRequestFactory(request -> {
        request.setConnectTimeout(HttpClient.this.config.getConnectTimeout());
        request.setReadTimeout(HttpClient.this.config.getReadTimeout());
        request.setNumberOfRetries(HttpClient.this.config.getRetries());
        request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff.Builder().build()));
    });
}
 
Example #6
Source File: CredentialFactory.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
  if (credential != null) {
    httpRequest.setInterceptor(credential);
  }
  httpRequest.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
  httpRequest.setUnsuccessfulResponseHandler(
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
}
 
Example #7
Source File: BaseApiService.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
public LoggingIOExceptionHandler(BackOff backOff) {
  delegate = new HttpBackOffIOExceptionHandler(backOff);
}
 
Example #8
Source File: RetryInitializer.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
private HttpIOExceptionHandler newIOExceptionHandler() {
  return new HttpBackOffIOExceptionHandler(retryConfig.newBackOff())
      .setSleeper(retryConfig.getSleeper());
}
 
Example #9
Source File: ForceOAuthClient.java    From salesforce-jdbc with MIT License 4 votes vote down vote up
private HttpIOExceptionHandler buildIOExceptionHandler() {
    return new HttpBackOffIOExceptionHandler(getBackOff());
}
 
Example #10
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());
}
 
Example #11
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
public void initialize(HttpRequest request) {
  request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(BackOff.ZERO_BACKOFF));
  request.setUnsuccessfulResponseHandler(
      new HttpBackOffUnsuccessfulResponseHandler(BackOff.ZERO_BACKOFF));
}