Skip to content

Commit

Permalink
feat(client): adjust retry behavior (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Oct 24, 2023
1 parent b01cf1b commit e9552ac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down

0 comments on commit e9552ac

Please sign in to comment.