Skip to content

Commit b2bb415

Browse files
Improve as per review comments
- longer timeout - check exs.getCause() as well - retry for more exception classes - better phrasing
1 parent b1408f0 commit b2bb415

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannel.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,28 @@ public int read(ByteBuffer dst) throws IOException {
101101
checkOpen();
102102
int amt;
103103
int retries = 0;
104-
int maxRetries = 3;
104+
int maxRetries = Math.max(3, maxChannelReopens);
105105
dst.mark();
106106
while (true) {
107107
try {
108108
dst.reset();
109109
amt = channel.read(dst);
110110
break;
111111
} catch (StorageException exs) {
112-
if (reopens < maxChannelReopens && (
113-
exs.getMessage().contains("Connection closed prematurely")
114-
|| exs.getCause() instanceof javax.net.ssl.SSLHandshakeException
115-
|| exs.getCause() instanceof java.io.EOFException
116-
|| exs.getCause() instanceof java.net.SocketTimeoutException
117-
)) {
112+
if (reopens < maxChannelReopens && (isReopenable(exs) || isReopenable(exs.getCause()))) {
118113
// these errors aren't marked as retryable since the channel is closed;
119-
// but here at this higher level we can retry it.
114+
// but here at this higher level we can retry them.
120115
reopens++;
121116
sleepForAttempt(reopens);
122117
innerOpen();
123118
continue;
124-
} else if ((exs.isRetryable() || exs.getCode() == 500 || exs.getCode() == 503) && retries < maxRetries) {
119+
} else if (retries < maxRetries &&
120+
(exs.isRetryable() || exs.getCode() == 500 || exs.getCode() == 503)) {
125121
retries++;
126122
sleepForAttempt(retries);
127123
continue;
128124
}
129-
throw new StorageException(exs.getCode(), "Retry failed", exs);
125+
throw new StorageException(exs.getCode(), "All retries failed", exs);
130126
}
131127
}
132128
if (amt > 0) {
@@ -140,11 +136,22 @@ public int read(ByteBuffer dst) throws IOException {
140136
}
141137
}
142138

139+
private static boolean isReopenable(Throwable exs) {
140+
return exs != null && (exs.getMessage().contains("Connection closed prematurely")
141+
|| exs.getCause() instanceof javax.net.ssl.SSLException
142+
|| exs.getCause() instanceof java.io.EOFException
143+
|| exs.getCause() instanceof java.net.SocketException);
144+
}
145+
143146
private void sleepForAttempt(int attempt) {
144-
long delay = 500;
147+
long delay = 1000;
148+
long maxDelay = 120_000;
145149
// exponential backoff
146150
for (int i=0; i<attempt; i++) {
147151
delay *= 2;
152+
if (delay > maxDelay) {
153+
delay = maxDelay;
154+
}
148155
}
149156
try {
150157
Thread.sleep(delay);

0 commit comments

Comments
 (0)