com.google.api.client.util.ExponentialBackOff Java Examples
The following examples show how to use
com.google.api.client.util.ExponentialBackOff.
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 |
/** 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: RetryHttpInitializerWrapper.java From beam with Apache License 2.0 | 6 votes |
/** 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 #3
Source File: RetryConfig.java From firebase-admin-java with Apache License 2.0 | 6 votes |
private RetryConfig(Builder builder) { if (builder.retryStatusCodes != null) { this.retryStatusCodes = ImmutableList.copyOf(builder.retryStatusCodes); } else { this.retryStatusCodes = ImmutableList.of(); } this.retryOnIOExceptions = builder.retryOnIOExceptions; checkArgument(builder.maxRetries >= 0, "maxRetries must not be negative"); this.maxRetries = builder.maxRetries; this.sleeper = checkNotNull(builder.sleeper); this.backOffBuilder = new ExponentialBackOff.Builder() .setInitialIntervalMillis(INITIAL_INTERVAL_MILLIS) .setMaxIntervalMillis(builder.maxIntervalMillis) .setMultiplier(builder.backOffMultiplier) .setRandomizationFactor(0); // Force validation of arguments by building the BackOff object this.backOffBuilder.build(); }
Example #4
Source File: RetryConfigTest.java From firebase-admin-java with Apache License 2.0 | 6 votes |
@Test public void testEmptyBuilder() { RetryConfig config = RetryConfig.builder().build(); assertTrue(config.getRetryStatusCodes().isEmpty()); assertEquals(0, config.getMaxRetries()); assertEquals(2 * 60 * 1000, config.getMaxIntervalMillis()); assertEquals(2.0, config.getBackOffMultiplier(), 0.01); assertSame(Sleeper.DEFAULT, config.getSleeper()); ExponentialBackOff backOff = (ExponentialBackOff) config.newBackOff(); assertEquals(2 * 60 * 1000, backOff.getMaxIntervalMillis()); assertEquals(2.0, backOff.getMultiplier(), 0.01); assertEquals(500, backOff.getInitialIntervalMillis()); assertEquals(0.0, backOff.getRandomizationFactor(), 0.01); assertNotSame(backOff, config.newBackOff()); }
Example #5
Source File: MainActivity.java From apps-script-mobile-addons with Apache License 2.0 | 6 votes |
/** * Attempts to initialize credentials and service object (prior to a call * to the API); uses the account provided by the calling app. This * requires the GET_ACCOUNTS permission to be explicitly granted by the * user; this will be requested here if it is not already granted. The * AfterPermissionGranted annotation indicates that this function will be * rerun automatically whenever the GET_ACCOUNTS permission is granted. */ @AfterPermissionGranted(REQUEST_PERMISSION_GET_ACCOUNTS) private void createCredentialsAndService() { if (EasyPermissions.hasPermissions( MainActivity.this, Manifest.permission.GET_ACCOUNTS)) { mCredential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()) .setSelectedAccountName(mAccount.name); mService = new com.google.api.services.script.Script.Builder( mTransport, mJsonFactory, setHttpTimeout(mCredential)) .setApplicationName(getString(R.string.app_name)) .build(); updateButtonEnableStatus(); // Callback to retry the API call with valid service/credentials callAppsScriptTask(mLastFunctionCalled); } else { // Request the GET_ACCOUNTS permission via a user dialog EasyPermissions.requestPermissions( MainActivity.this, getString(R.string.get_accounts_rationale), REQUEST_PERMISSION_GET_ACCOUNTS, Manifest.permission.GET_ACCOUNTS); } }
Example #6
Source File: GoogleDriveApiImpl.java From science-journal with Apache License 2.0 | 6 votes |
@Override public DriveApi init( HttpTransport transport, JsonFactory jsonFactory, AppAccount appAccount, Context applicationContext) { List<String> scopeList = Arrays.asList(SCOPES); GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(applicationContext, scopeList) .setBackOff(new ExponentialBackOff()) .setSelectedAccount(appAccount.getAccount()); this.driveApi = new Drive.Builder(transport, jsonFactory, credential) .setApplicationName(applicationContext.getPackageName()) .build(); return this; }
Example #7
Source File: YouTubeSingleton.java From YouTube-In-Background with MIT License | 6 votes |
private YouTubeSingleton(Context context) { String appName = context.getString(R.string.app_name); credential = GoogleAccountCredential .usingOAuth2(context, Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()); youTube = new YouTube.Builder( new NetHttpTransport(), new JacksonFactory(), httpRequest -> {} ).setApplicationName(appName).build(); youTubeWithCredentials = new YouTube.Builder( new NetHttpTransport(), new JacksonFactory(), credential ).setApplicationName(appName).build(); }
Example #8
Source File: DeleteVariants.java From dataflow-java with Apache License 2.0 | 6 votes |
@ProcessElement public void processElement(DoFn<String, Integer>.ProcessContext context) throws Exception { String variantId = context.element(); // Call the deletion operation via exponential backoff so that "Rate Limit Exceeded" // quota issues do not cause the pipeline to fail. ExponentialBackOff backoff = new ExponentialBackOff.Builder().build(); while (true) { try { genomics.variants().delete(variantId).execute(); Metrics.counter(DeleteVariantFn.class, "Number of variants deleted").inc(); context.output(1); return; } catch (Exception e) { if (e.getMessage().startsWith("429 Too Many Requests")) { LOG.warn("Backing-off per: ", e); long backOffMillis = backoff.nextBackOffMillis(); if (backOffMillis == BackOff.STOP) { throw e; } Thread.sleep(backOffMillis); } else { throw e; } } } }
Example #9
Source File: GmailServiceModule.java From mail-importer with Apache License 2.0 | 6 votes |
@Override protected void configure() { requireBinding(CommandLineArguments.class); bind(GmailService.class).in(Singleton.class); bind(Credential.class) .toProvider(Authorizer.class) .in(Singleton.class); bind(ExponentialBackOff.Builder.class) .toInstance(new ExponentialBackOff.Builder() .setInitialIntervalMillis(1000) .setMultiplier(2) .setRandomizationFactor(0.5) .setMaxIntervalMillis(60000) .setMaxElapsedTimeMillis(300000)); }
Example #10
Source File: RetryPolicy.java From connector-sdk with Apache License 2.0 | 5 votes |
/** * Returns {@link BackOff} instance used for implementing exponential back off for failed * requests. */ @Override public BackOff createBackOffInstance() { return new ExponentialBackOff.Builder() .setInitialIntervalMillis(INITIAL_DELAY_SECONDS * 1000) .setMultiplier(MULTIPLIER) .build(); }
Example #11
Source File: HttpEventPublisherTest.java From beam with Apache License 2.0 | 5 votes |
@Test public void configureBackOffDefaultTest() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { HttpEventPublisher publisherDefaultBackOff = HttpEventPublisher.newBuilder() .withUrl("http://example.com") .withToken("test-token") .withDisableCertificateValidation(false) .build(); assertEquals( ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS, publisherDefaultBackOff.getConfiguredBackOff().getMaxElapsedTimeMillis()); }
Example #12
Source File: GoogleCredentials.java From halyard with Apache License 2.0 | 5 votes |
public static HttpRequestInitializer retryRequestInitializer() { return request -> { request.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(2)); request.setReadTimeout((int) TimeUnit.MINUTES.toMillis(2)); request.setUnsuccessfulResponseHandler( new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())); }; }
Example #13
Source File: GoogleCredentials.java From halyard with Apache License 2.0 | 5 votes |
public static HttpRequestInitializer setHttpTimeout( final com.google.auth.oauth2.GoogleCredentials credentials) { return new HttpCredentialsAdapter(credentials) { public void initialize(HttpRequest request) throws IOException { super.initialize(request); request.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(2)); request.setReadTimeout((int) TimeUnit.MINUTES.toMillis(2)); request.setUnsuccessfulResponseHandler( new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())); } }; }
Example #14
Source File: ShortyzApplication.java From shortyz with GNU General Public License v3.0 | 5 votes |
public void updateCredential(SharedPreferences prefs){ credential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(GMConstants.SCOPES)) .setBackOff(new ExponentialBackOff()) .setSelectedAccountName(prefs.getString(GMConstants.PREF_ACCOUNT_NAME, null)); if(credential != null && credential.getSelectedAccount() != null) { gmailService = new com.google.api.services.gmail.Gmail.Builder( transport, jsonFactory, credential) .setApplicationName("Shortyz") .build(); } else { gmailService = null; } }
Example #15
Source File: GoogleUtils.java From kork with Apache License 2.0 | 5 votes |
static HttpRequestInitializer setTimeoutsAndRetryBehavior(final GoogleCredentials credentials) { return new HttpCredentialsAdapter(credentials) { public void initialize(HttpRequest request) throws IOException { super.initialize(request); request.setConnectTimeout(CONNECT_TIMEOUT); request.setReadTimeout(READ_TIMEOUT); HttpBackOffUnsuccessfulResponseHandler unsuccessfulResponseHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()); unsuccessfulResponseHandler.setBackOffRequired( HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ON_SERVER_ERROR); request.setUnsuccessfulResponseHandler(unsuccessfulResponseHandler); } }; }
Example #16
Source File: PistachiosClient.java From Pistachio with Apache License 2.0 | 5 votes |
public RetryWaiter(Meter failureMeter0) { failureMeter = failureMeter0; backoff = (new ExponentialBackOff.Builder()).setInitialIntervalMillis(initialIntervalMillis) .setMaxElapsedTimeMillis(maxElapsedTimeMillis) .setMaxIntervalMillis(maxIntervalMillis) .build(); }
Example #17
Source File: CredentialFactory.java From hadoop-connectors with Apache License 2.0 | 5 votes |
@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 #18
Source File: RetryHttpInitializer.java From hadoop-connectors with Apache License 2.0 | 5 votes |
public CredentialOrBackoffResponseHandler() { HttpBackOffUnsuccessfulResponseHandler errorCodeHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()); errorCodeHandler.setBackOffRequired( response -> BASE_HTTP_BACKOFF_REQUIRED.isRequired(response) || response.getStatusCode() == HTTP_SC_TOO_MANY_REQUESTS); if (sleeperOverride != null) { errorCodeHandler.setSleeper(sleeperOverride); } this.delegateHandler = errorCodeHandler; }
Example #19
Source File: GoogleCloudStorageReadChannel.java From hadoop-connectors with Apache License 2.0 | 5 votes |
/** Creates new generic BackOff used for retries. */ @VisibleForTesting ExponentialBackOff createBackOff() { return new ExponentialBackOff.Builder() .setInitialIntervalMillis(readOptions.getBackoffInitialIntervalMillis()) .setRandomizationFactor(readOptions.getBackoffRandomizationFactor()) .setMultiplier(readOptions.getBackoffMultiplier()) .setMaxIntervalMillis(readOptions.getBackoffMaxIntervalMillis()) .setMaxElapsedTimeMillis(readOptions.getBackoffMaxElapsedTimeMillis()) .setNanoClock(clock) .build(); }
Example #20
Source File: CoopLockOperationDao.java From hadoop-connectors with Apache License 2.0 | 5 votes |
public void checkpointRenameOperation( String bucketName, String operationId, Instant operationInstant, boolean copySucceeded) throws IOException { URI operationLockPath = getOperationFilePath( bucketName, OPERATION_LOCK_FILE_FORMAT, CoopLockOperationType.RENAME, operationId, operationInstant); ExponentialBackOff backOff = newLockModifyBackoff(); for (int i = 0; i < 10; i++) { try { modifyOperationLock( operationId, operationLockPath, l -> { RenameOperation operation = GSON.fromJson(l, RenameOperation.class); operation .setLockExpiration( Instant.now().plusMillis(options.getLockExpirationTimeoutMilli())) .setCopySucceeded(copySucceeded); return GSON.toJson(operation); }); return; } catch (IOException e) { logger.atWarning().withCause(e).log( "Failed to checkpoint '%s' lock for %s operation, attempt #%d", operationLockPath, operationId, i + 1); sleepUninterruptibly(Duration.ofMillis(backOff.nextBackOffMillis())); } } throw new IOException( String.format( "Failed to checkpoint '%s' lock for %s operation", operationLockPath, operationId)); }
Example #21
Source File: CoopLockOperationDao.java From hadoop-connectors with Apache License 2.0 | 5 votes |
private static ExponentialBackOff newLockModifyBackoff() { return new ExponentialBackOff.Builder() .setInitialIntervalMillis(LOCK_MODIFY_RETRY_BACK_OFF_MILLIS) .setMultiplier(1.1) .setRandomizationFactor(0.2) .setMaxIntervalMillis((int) (LOCK_MODIFY_RETRY_BACK_OFF_MILLIS * 1.25)) .setMaxElapsedTimeMillis(Integer.MAX_VALUE) .build(); }
Example #22
Source File: BackOffUtil.java From secor with Apache License 2.0 | 5 votes |
public BackOffUtil(boolean isExponential) { if (isExponential) { // aggressive backOff = new ExponentialBackOff.Builder() .setInitialIntervalMillis(2) .setMaxElapsedTimeMillis(Integer.MAX_VALUE) .setMaxIntervalMillis(Integer.MAX_VALUE) .setMultiplier(1.5) .setRandomizationFactor(0) .build(); } else { // conservative backOff = new FixedBackOff(); } }
Example #23
Source File: AccountWorkflow.java From googleads-shopping-samples with Apache License 2.0 | 5 votes |
@Override public void execute() throws IOException { System.out.println("---------------------------------"); System.out.println("Running Account service workflow:"); System.out.println(); System.out.println("Retrieving account information."); Account account = content.accounts().get(config.getMerchantId(), config.getMerchantId()).execute(); AccountUtils.printAccount(account); if (!config.getIsMCA()) { return; } AccountsListSample.listAccountsForMerchant(config.getMerchantId(), content); Account subaccount = new Account(); subaccount.setName(AccountUtils.SAMPLE_ACCOUNT_NAME); System.out.println("Creating new sub-account."); Account result = content.accounts().insert(config.getMerchantId(), subaccount).execute(); AccountUtils.printAccount(result); BigInteger accountId = result.getId(); System.out.printf("Retrieving new sub-account %s.%n", accountId); // Newly created accounts may not be immediately accessible, so retry until available // or until our back off strategy runs out. ExponentialBackOff backOff = new ExponentialBackOff.Builder() .setInitialIntervalMillis(5000) .setMaxIntervalMillis(30000) .build(); subaccount = retryFailures(content.accounts().get(config.getMerchantId(), accountId), backOff); AccountUtils.printAccount(subaccount); System.out.printf("Deleting sub-account %s.%n", accountId); content.accounts().delete(config.getMerchantId(), accountId).execute(); }
Example #24
Source File: ParallelReportDownload.java From googleads-java-lib with Apache License 2.0 | 5 votes |
private ReportDownloadCallable( ImmutableAdWordsSession session, AdWordsServicesInterface adWordsServices, ReportDefinition reportDefinition, File reportOutputFile, ExponentialBackOff backOff) { this.session = session; this.adWordsServices = adWordsServices; this.reportDefinition = reportDefinition; this.reportOutputFile = reportOutputFile; this.backOff = backOff; }
Example #25
Source File: BaseApiService.java From connector-sdk with Apache License 2.0 | 5 votes |
/** Initialize {@link HttpRequest} to setup exponential back off and automatic retries. */ @Override public void initialize(HttpRequest request) throws IOException { BackOff backOff = new ExponentialBackOff(); request.setUnsuccessfulResponseHandler(new LoggingResponseHandler(retryPolicy, backOff)); request.setIOExceptionHandler(new LoggingIOExceptionHandler(backOff)); }
Example #26
Source File: BaseGmailProvider.java From PrivacyStreams with Apache License 2.0 | 5 votes |
private void checkGmailApiRequirements() { String accountName = PreferenceManager.getDefaultSharedPreferences(getContext()) .getString(PREF_ACCOUNT_NAME, null); if (accountName != null) { GoogleAccountCredential mCredential = GoogleAccountCredential.usingOAuth2( getContext().getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()); mCredential.setSelectedAccountName(accountName); if (!DeviceUtils.isGooglePlayServicesAvailable(getContext())) { DeviceUtils.acquireGooglePlayServices(getContext()); } else{ mService = new Gmail.Builder( AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), mCredential) .setApplicationName(AppUtils.getApplicationName(getContext())) .build(); authorized = true; } } else { GmailAuthorizationActivity.setListener(this); getContext().startActivity(new Intent(getContext(), GmailAuthorizationActivity.class)); } }
Example #27
Source File: HttpClient.java From ProjectAres with GNU Affero General Public License v3.0 | 5 votes |
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 #28
Source File: HttpEventPublisher.java From beam with Apache License 2.0 | 5 votes |
/** * Validates and builds a {@link HttpEventPublisher} object. * * @return {@link HttpEventPublisher} */ HttpEventPublisher build() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { checkNotNull(token(), "Authentication token needs to be specified via withToken(token)."); checkNotNull(genericUrl(), "URL needs to be specified via withUrl(url)."); if (disableCertificateValidation() == null) { LOG.info("Certificate validation disabled: {}", DEFAULT_DISABLE_CERTIFICATE_VALIDATION); setDisableCertificateValidation(DEFAULT_DISABLE_CERTIFICATE_VALIDATION); } if (maxElapsedMillis() == null) { LOG.info( "Defaulting max backoff time to: {} milliseconds ", ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS); setMaxElapsedMillis(ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS); } CloseableHttpClient httpClient = getHttpClient(DEFAULT_MAX_CONNECTIONS, disableCertificateValidation()); setTransport(new ApacheHttpTransport(httpClient)); setRequestFactory(transport().createRequestFactory()); return autoBuild(); }
Example #29
Source File: ForceOAuthClient.java From salesforce-jdbc with MIT License | 5 votes |
private BackOff getBackOff() { return new ExponentialBackOff.Builder() .setInitialIntervalMillis(500) .setMaxElapsedTimeMillis(30000) .setMaxIntervalMillis(10000) .setMultiplier(1.5) .setRandomizationFactor(0.5) .build(); }
Example #30
Source File: GmailAuthorizationActivity.java From PrivacyStreams with Apache License 2.0 | 5 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (gmailResultListener!=null) { if (! DeviceUtils.isDeviceOnline(this)) { Logging.warn("No network connection available."); } if (! DeviceUtils.isGooglePlayServicesAvailable(this)) { DeviceUtils.acquireGooglePlayServices(this); } mCredential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()); if (mCredential.getSelectedAccountName() == null) { chooseAccount(); } if (getIntent().getAction()!=null) { if(getIntent().getAction().equalsIgnoreCase("UserRecoverableAuthIOException")) startActivityForResult((Intent) getIntent(). getExtras().get("request_authorization"), REQUEST_AUTHORIZATION); } } else { finish(); } }