Skip to content

Commit d98f4f6

Browse files
committed
feat(client): adjust retry behavior (#106)
1 parent 1c5ceda commit d98f4f6

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

lithic-java-core/src/main/kotlin/com/lithic/api/core/http/RetryingHttpClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ private constructor(
178178
}
179179

180180
// Apply exponential backoff, but not more than the max.
181-
val backoffSeconds = min(0.5 * 2.0.pow(retries - 1), 2.0)
181+
val backoffSeconds = min(0.5 * 2.0.pow(retries - 1), 8.0)
182182

183183
// Apply some jitter
184-
val jitter = ThreadLocalRandom.current().nextDouble()
184+
val jitter = 1.0 - 0.25 * ThreadLocalRandom.current().nextDouble()
185185

186-
return (TimeUnit.SECONDS.toMillis(1) * backoffSeconds + jitter).toLong()
186+
return (TimeUnit.SECONDS.toMillis(1) * backoffSeconds * jitter).toLong()
187187
}
188188

189189
private fun sleepAsync(millis: Long): CompletableFuture<Void> {

lithic-java-core/src/test/kotlin/com/lithic/api/core/http/RetryingHttpClientTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ internal class RetryingHttpClientTest {
7777
.willReturn(ok())
7878
.willSetStateTo("COMPLETED")
7979
)
80-
val retryingClient = RetryingHttpClient.builder().httpClient(httpClient).build()
80+
val retryingClient =
81+
RetryingHttpClient.builder().httpClient(httpClient).maxRetries(2).build()
8182
val response = retryingClient.execute(request)
8283
assertThat(response.statusCode()).isEqualTo(200)
8384
verify(3, postRequestedFor(urlPathEqualTo("/something")))

0 commit comments

Comments
 (0)