Skip to content

Commit 924caf3

Browse files
author
kushal
committed
Add retry for HTTP 5xx errors on v2 list wallet transfers
1 parent 3e32958 commit 924caf3

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

bitgo-java-api/src/main/java/com/bitso/bitgo/v2/BitGoClientImpl.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
import java.io.IOException;
1515
import java.math.BigDecimal;
1616
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;
1824

1925
/**
2026
* Implementation of the BitGo client.
@@ -181,7 +187,7 @@ public WalletTransferResponse listWalletTransfers(String coin, String walletId,
181187
reqPropMap = new HashMap<>();
182188
reqPropMap.put("prevId", prevId);
183189
}
184-
HttpURLConnection conn = httpGet(url, reqPropMap);
190+
HttpURLConnection conn = httpGetRetry500(url, reqPropMap);
185191

186192
final WalletTransferResponse resp = SerializationUtil.mapper.readValue(conn.getInputStream(), WalletTransferResponse.class);
187193
log.trace("listWalletTransactions response: {}", resp);
@@ -214,6 +220,32 @@ private HttpURLConnection httpGet(String url, Map<String, String> reqParams) thr
214220
return unsafe ? HttpHelper.getUnsafe(url, getAuth(), reqParams) : HttpHelper.get(url, getAuth(), reqParams);
215221
}
216222

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+
217249
private String getAuth() {
218250
return token;
219251
}

0 commit comments

Comments
 (0)