diff --git a/lithic-java-core/src/main/kotlin/com/lithic/api/core/http/RetryingHttpClient.kt b/lithic-java-core/src/main/kotlin/com/lithic/api/core/http/RetryingHttpClient.kt index 3209e502..cd8e9b04 100644 --- a/lithic-java-core/src/main/kotlin/com/lithic/api/core/http/RetryingHttpClient.kt +++ b/lithic-java-core/src/main/kotlin/com/lithic/api/core/http/RetryingHttpClient.kt @@ -178,12 +178,12 @@ private constructor( } // Apply exponential backoff, but not more than the max. - val backoffSeconds = min(0.5 * 2.0.pow(retries - 1), 2.0) + val backoffSeconds = min(0.5 * 2.0.pow(retries - 1), 8.0) // Apply some jitter - val jitter = ThreadLocalRandom.current().nextDouble() + val jitter = 1.0 - 0.25 * ThreadLocalRandom.current().nextDouble() - return (TimeUnit.SECONDS.toMillis(1) * backoffSeconds + jitter).toLong() + return (TimeUnit.SECONDS.toMillis(1) * backoffSeconds * jitter).toLong() } private fun sleepAsync(millis: Long): CompletableFuture { diff --git a/lithic-java-core/src/test/kotlin/com/lithic/api/core/http/RetryingHttpClientTest.kt b/lithic-java-core/src/test/kotlin/com/lithic/api/core/http/RetryingHttpClientTest.kt index 0ce9c825..78dfda0a 100644 --- a/lithic-java-core/src/test/kotlin/com/lithic/api/core/http/RetryingHttpClientTest.kt +++ b/lithic-java-core/src/test/kotlin/com/lithic/api/core/http/RetryingHttpClientTest.kt @@ -77,7 +77,8 @@ internal class RetryingHttpClientTest { .willReturn(ok()) .willSetStateTo("COMPLETED") ) - val retryingClient = RetryingHttpClient.builder().httpClient(httpClient).build() + val retryingClient = + RetryingHttpClient.builder().httpClient(httpClient).maxRetries(2).build() val response = retryingClient.execute(request) assertThat(response.statusCode()).isEqualTo(200) verify(3, postRequestedFor(urlPathEqualTo("/something")))