Skip to content

Commit 2fddc77

Browse files
Review feedback
Also added a test for getCause().getCause()
1 parent b2bb415 commit 2fddc77

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@
1616

1717
package com.google.cloud.storage.contrib.nio;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
20-
2119
import com.google.cloud.ReadChannel;
2220
import com.google.cloud.storage.BlobId;
2321
import com.google.cloud.storage.BlobInfo;
2422
import com.google.cloud.storage.Storage;
2523
import com.google.cloud.storage.StorageException;
2624

25+
import javax.annotation.CheckReturnValue;
26+
import javax.annotation.concurrent.ThreadSafe;
2727
import java.io.IOException;
2828
import java.nio.ByteBuffer;
2929
import java.nio.channels.ClosedChannelException;
3030
import java.nio.channels.NonWritableChannelException;
3131
import java.nio.channels.SeekableByteChannel;
3232
import java.nio.file.NoSuchFileException;
3333

34-
import javax.annotation.CheckReturnValue;
35-
import javax.annotation.concurrent.ThreadSafe;
34+
import static com.google.common.base.Preconditions.checkArgument;
3635

3736
/**
3837
* Cloud Storage read channel.
@@ -137,10 +136,15 @@ public int read(ByteBuffer dst) throws IOException {
137136
}
138137

139138
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);
139+
while (exs != null) {
140+
if (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+
|| exs.getCause() instanceof java.net.SocketTimeoutException) return true;
145+
exs = exs.getCause();
146+
}
147+
return false;
144148
}
145149

146150
private void sleepForAttempt(int attempt) {

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannelTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.junit.runner.RunWith;
3939
import org.junit.runners.JUnit4;
4040

41+
import javax.net.ssl.SSLHandshakeException;
4142
import java.io.IOException;
4243
import java.nio.ByteBuffer;
4344
import java.nio.channels.ClosedChannelException;
@@ -92,6 +93,18 @@ public void testReadRetry() throws IOException {
9293
verify(gcsChannel, times(2)).read(any(ByteBuffer.class));
9394
}
9495

96+
@Test
97+
public void testReadRetrySSLHandshake() throws IOException {
98+
ByteBuffer buffer = ByteBuffer.allocate(1);
99+
when(gcsChannel.read(eq(buffer)))
100+
.thenThrow(new StorageException(new IOException("something", new IOException("thing", new SSLHandshakeException("connection closed due to throttling")))))
101+
.thenReturn(1);
102+
assertThat(chan.position()).isEqualTo(0L);
103+
assertThat(chan.read(buffer)).isEqualTo(1);
104+
assertThat(chan.position()).isEqualTo(1L);
105+
verify(gcsChannel, times(2)).read(any(ByteBuffer.class));
106+
}
107+
95108
@Test
96109
public void testReadRetryEventuallyGivesUp() throws IOException {
97110
ByteBuffer buffer = ByteBuffer.allocate(1);

0 commit comments

Comments
 (0)