Skip to content

Commit bbc968e

Browse files
committed
Merge pull request #414 from mziccard/retry-on-timeout
Translate SocketTimeoutException to retryable service exception
2 parents 2c90245 + a0f2d20 commit bbc968e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.json.JSONTokener;
4343

4444
import java.net.InetAddress;
45+
import java.net.SocketTimeoutException;
4546
import java.net.URL;
4647
import java.util.HashMap;
4748
import java.util.Map;
@@ -121,9 +122,17 @@ private static DatastoreRpcException translate(DatastoreException exception) {
121122
if (reason == null) {
122123
reason = HTTP_STATUS_TO_REASON.get(exception.getCode());
123124
}
124-
return reason != null
125-
? new DatastoreRpcException(reason)
126-
: new DatastoreRpcException("Unknown", exception.getCode(), false, message);
125+
if (reason != null) {
126+
return new DatastoreRpcException(reason);
127+
} else {
128+
boolean retryable = false;
129+
reasonStr = "Unknown";
130+
if (exception.getCause() instanceof SocketTimeoutException) {
131+
retryable = true;
132+
reasonStr = "Request timeout";
133+
}
134+
return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message);
135+
}
127136
}
128137

129138
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.io.ByteArrayOutputStream;
6969
import java.io.IOException;
7070
import java.io.InputStream;
71+
import java.net.SocketTimeoutException;
7172
import java.util.ArrayList;
7273
import java.util.Iterator;
7374
import java.util.List;
@@ -101,7 +102,11 @@ private static StorageException translate(IOException exception) {
101102
&& ((GoogleJsonResponseException) exception).getDetails() != null) {
102103
translated = translate(((GoogleJsonResponseException) exception).getDetails());
103104
} else {
104-
translated = new StorageException(0, exception.getMessage(), false);
105+
boolean retryable = false;
106+
if (exception instanceof SocketTimeoutException) {
107+
retryable = true;
108+
}
109+
translated = new StorageException(0, exception.getMessage(), retryable);
105110
}
106111
translated.initCause(exception);
107112
return translated;

0 commit comments

Comments
 (0)