Skip to content

Commit 1593376

Browse files
Address reviewer comments
Simplify backoff computation, add loop variant variable, and code formatting changes.
1 parent 7c81e40 commit 1593376

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import java.nio.channels.SeekableByteChannel;
3232
import java.nio.file.NoSuchFileException;
3333

34+
import javax.net.ssl.SSLException;
35+
import java.io.EOFException;
36+
import java.net.SocketException;
37+
import java.net.SocketTimeoutException;
38+
3439
import static com.google.common.base.Preconditions.checkArgument;
3540

3641
/**
@@ -137,29 +142,27 @@ public int read(ByteBuffer dst) throws IOException {
137142

138143
private static boolean isReopenable(Throwable exs) {
139144
Throwable throwable = exs;
140-
while (throwable != null) {
145+
// ensures finite iteration
146+
int maxDepth = 10;
147+
while (throwable != null && maxDepth-- > 0) {
148+
Throwable cause = throwable.getCause();
141149
if (throwable.getMessage().contains("Connection closed prematurely")
142-
|| throwable.getCause() instanceof javax.net.ssl.SSLException
143-
|| throwable.getCause() instanceof java.io.EOFException
144-
|| throwable.getCause() instanceof java.net.SocketException
145-
|| throwable.getCause() instanceof java.net.SocketTimeoutException) {
150+
|| cause instanceof SSLException
151+
|| cause instanceof EOFException
152+
|| cause instanceof SocketException
153+
|| cause instanceof SocketTimeoutException) {
146154
return true;
147155
}
148-
throwable = throwable.getCause();
156+
throwable = cause;
149157
}
150158
return false;
151159
}
152160

153161
private void sleepForAttempt(int attempt) {
154-
long delay = 1000;
162+
long firstdelay = 1000;
155163
long maxDelay = 120_000;
156164
// exponential backoff
157-
for (int i=0; i<attempt; i++) {
158-
delay *= 2;
159-
if (delay > maxDelay) {
160-
delay = maxDelay;
161-
}
162-
}
165+
long delay = firstdelay * Math.min(1 << attempt, maxDelay);
163166
try {
164167
Thread.sleep(delay);
165168
} catch (InterruptedException iex) {

0 commit comments

Comments
 (0)