|
31 | 31 | import java.nio.channels.SeekableByteChannel; |
32 | 32 | import java.nio.file.NoSuchFileException; |
33 | 33 |
|
| 34 | +import javax.net.ssl.SSLException; |
| 35 | +import java.io.EOFException; |
| 36 | +import java.net.SocketException; |
| 37 | +import java.net.SocketTimeoutException; |
| 38 | + |
34 | 39 | import static com.google.common.base.Preconditions.checkArgument; |
35 | 40 |
|
36 | 41 | /** |
@@ -137,29 +142,27 @@ public int read(ByteBuffer dst) throws IOException { |
137 | 142 |
|
138 | 143 | private static boolean isReopenable(Throwable exs) { |
139 | 144 | 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(); |
141 | 149 | 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) { |
146 | 154 | return true; |
147 | 155 | } |
148 | | - throwable = throwable.getCause(); |
| 156 | + throwable = cause; |
149 | 157 | } |
150 | 158 | return false; |
151 | 159 | } |
152 | 160 |
|
153 | 161 | private void sleepForAttempt(int attempt) { |
154 | | - long delay = 1000; |
| 162 | + long firstdelay = 1000; |
155 | 163 | long maxDelay = 120_000; |
156 | 164 | // 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); |
163 | 166 | try { |
164 | 167 | Thread.sleep(delay); |
165 | 168 | } catch (InterruptedException iex) { |
|
0 commit comments