|
14 | 14 | import java.io.IOException;
|
15 | 15 | import java.math.BigDecimal;
|
16 | 16 | import java.net.HttpURLConnection;
|
17 |
| -import java.util.*; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.Scanner; |
| 23 | +import java.util.concurrent.TimeUnit; |
18 | 24 |
|
19 | 25 | /**
|
20 | 26 | * Implementation of the BitGo client.
|
@@ -181,7 +187,7 @@ public WalletTransferResponse listWalletTransfers(String coin, String walletId,
|
181 | 187 | reqPropMap = new HashMap<>();
|
182 | 188 | reqPropMap.put("prevId", prevId);
|
183 | 189 | }
|
184 |
| - HttpURLConnection conn = httpGet(url, reqPropMap); |
| 190 | + HttpURLConnection conn = httpGetRetry500(url, reqPropMap); |
185 | 191 |
|
186 | 192 | final WalletTransferResponse resp = SerializationUtil.mapper.readValue(conn.getInputStream(), WalletTransferResponse.class);
|
187 | 193 | log.trace("listWalletTransactions response: {}", resp);
|
@@ -214,6 +220,32 @@ private HttpURLConnection httpGet(String url, Map<String, String> reqParams) thr
|
214 | 220 | return unsafe ? HttpHelper.getUnsafe(url, getAuth(), reqParams) : HttpHelper.get(url, getAuth(), reqParams);
|
215 | 221 | }
|
216 | 222 |
|
| 223 | + private HttpURLConnection httpGetRetry500(String url, Map<String, String> reqParams) throws IOException { |
| 224 | + |
| 225 | + HttpURLConnection conn = null; |
| 226 | + |
| 227 | + int retryCounter = 0; |
| 228 | + while (retryCounter < 3) { |
| 229 | + conn = unsafe ? HttpHelper.getUnsafe(url, getAuth(), reqParams) : HttpHelper.get(url, getAuth(), reqParams); |
| 230 | + if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 231 | + break; |
| 232 | + } else if (conn.getResponseCode() >= 500 && conn.getResponseCode() < 600) { |
| 233 | + long sleepTimeMillis = TimeUnit.SECONDS.toMillis(5); |
| 234 | + log.info("Got responseCode={} with message={}, sleeping for {}ms, retryCounter={}", conn.getResponseCode(), conn.getResponseMessage(), sleepTimeMillis, retryCounter); |
| 235 | + try { |
| 236 | + Thread.sleep(sleepTimeMillis); |
| 237 | + } catch (InterruptedException e) { |
| 238 | + log.error("Error", e); |
| 239 | + } |
| 240 | + retryCounter++; |
| 241 | + } else { |
| 242 | + //Some other exception, don't retry |
| 243 | + break; |
| 244 | + } |
| 245 | + } |
| 246 | + return conn; |
| 247 | + } |
| 248 | + |
217 | 249 | private String getAuth() {
|
218 | 250 | return token;
|
219 | 251 | }
|
|
0 commit comments