[go: nahoru, domu]

Skip to content

Commit

Permalink
http issue 210: add back-off handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
peleyal committed Apr 19, 2013
1 parent e8c4cab commit 4bb60cd
Show file tree
Hide file tree
Showing 12 changed files with 833 additions and 23 deletions.
4 changes: 4 additions & 0 deletions findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@
<!-- Usage of a &#64;Beta class or method -->
<Class name="com.google.api.client.json.JsonParser"/>
</And>
<And>
<Bug pattern="BETA_CLASS_USAGE,BETA_METHOD_USAGE"/>
<Class name="com.google.api.client.http.HttpRequest"/>
</And>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@

package com.google.api.client.http;

import com.google.api.client.util.Beta;

import java.io.IOException;

/**
* {@link Beta} <br/>
* Strategy interface to control back off between retry attempts.
*
* @since 1.7
* @author Ravi Mistry
* @deprecated (scheduled to be removed in the 1.16). Use
* {@link HttpBackOffUnsuccessfulResponseHandler} instead.
*/
@Deprecated
@Beta
public interface BackOffPolicy {

/**
Expand Down Expand Up @@ -52,13 +59,13 @@ public interface BackOffPolicy {
* This method should be used as follows:
*
* <pre>
* long backoffTime = backoffPolicy.getNextBackoffMs();
* if (backoffTime == BackoffPolicy.STOP) {
* // Stop retrying.
* } else {
* // Retry after backoffTime.
* }
* </pre>
* long backoffTime = backoffPolicy.getNextBackoffMs();
* if (backoffTime == BackoffPolicy.STOP) {
* // Stop retrying.
* } else {
* // Retry after backoffTime.
* }
*</pre>
*
* @return the number of milliseconds to wait when backing off requests, or {@link #STOP} if no
* more retries should be made
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

package com.google.api.client.http;

import com.google.api.client.util.Beta;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.client.util.NanoClock;

import java.io.IOException;

/**
* {@link Beta} <br/>
* Implementation of {@link BackOffPolicy} that increases the back off period for each retry attempt
* using a randomization function that grows exponentially.
*
Expand Down Expand Up @@ -73,7 +75,12 @@
*
* @since 1.7
* @author Ravi Mistry
* @deprecated (scheduled to be removed in the 1.16). Use
* {@link HttpBackOffUnsuccessfulResponseHandler} with {@link ExponentialBackOff}
* instead.
*/
@Beta
@Deprecated
public class ExponentialBackOffPolicy implements BackOffPolicy {

/**
Expand Down Expand Up @@ -257,6 +264,7 @@ public static Builder builder() {
}

/**
* {@link Beta} <br/>
* Builder for {@link ExponentialBackOffPolicy}.
*
* <p>
Expand All @@ -265,6 +273,8 @@ public static Builder builder() {
*
* @since 1.7
*/
@Beta
@Deprecated
public static class Builder {

/** Exponential back-off builder. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.client.http;

import com.google.api.client.util.BackOff;
import com.google.api.client.util.BackOffUtils;
import com.google.api.client.util.Beta;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.Sleeper;

import java.io.IOException;

/**
* {@link Beta} <br/>
* {@link HttpIOExceptionHandler} implementation with {@link BackOff}.
*
* <p>
* It is designed to work with only one {@link HttpRequest} at a time. As a result you MUST create a
* new instance of {@link HttpBackOffIOExceptionHandler} with a new instance of {@link BackOff} for
* each instance of {@link HttpRequest}.
* </p>
*
* <p>
* Sample usage:
* </p>
*
* <pre>
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff());
* </pre>
*
* <p>
* Note: Implementation doesn't call {@link BackOff#reset} at all, since it expects a new
* {@link BackOff} instance.
* </p>
*
* <p>
* Implementation is not thread-safe
* </p>
*
* @author Eyal Peled
* @since 1.15
*/
@Beta
public class HttpBackOffIOExceptionHandler implements HttpIOExceptionHandler {

/** Back-off policy. */
private final BackOff backOff;

/** Sleeper. */
private Sleeper sleeper = Sleeper.DEFAULT;

/**
* Constructs a new instance from a {@link BackOff}.
*
* @param backOff back-off policy
*/
public HttpBackOffIOExceptionHandler(BackOff backOff) {
this.backOff = Preconditions.checkNotNull(backOff);
}

/** Returns the back-off. */
public final BackOff getBackOff() {
return backOff;
}

/** Returns the sleeper. */
public final Sleeper getSleeper() {
return sleeper;
}

/**
* Sets the sleeper.
*
* <p>
* The default value is {@link Sleeper#DEFAULT}.
* </p>
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public HttpBackOffIOExceptionHandler setSleeper(Sleeper sleeper) {
this.sleeper = Preconditions.checkNotNull(sleeper);
return this;
}

/**
* {@inheritDoc}
*
* <p>
* Handles the request with {@link BackOff}. That means that if back-off is required a call to
* {@link Sleeper#sleep(long)} will be made.
* </p>
*/
public boolean handleIOException(HttpRequest request, boolean supportsRetry) throws IOException {
if (!supportsRetry) {
return false;
}
try {
return BackOffUtils.next(sleeper, backOff);
} catch (InterruptedException exception) {
return false;
}
}
}
Loading

0 comments on commit 4bb60cd

Please sign in to comment.