Skip to content

Commit

Permalink
KTOR-5466 Connect timeout is not respected when using the HttpRequest…
Browse files Browse the repository at this point in the history
…Retry plugin
  • Loading branch information
rsinukov committed Feb 22, 2023
1 parent 5ba3db9 commit 324425a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package io.ktor.client.plugins

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.network.sockets.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.utils.*
import io.ktor.events.*
import io.ktor.http.*
import io.ktor.util.*
Expand Down Expand Up @@ -167,14 +169,16 @@ public class HttpRequestRetry internal constructor(configuration: Configuration)
/**
* Enables retrying a request if an exception is thrown during the [HttpSend] phase
* and specifies the number of retries.
* By default, [HttpRequestTimeoutException] is not retried. Set [retryOnTimeout] to `true` to retry on timeout.
* By default, [HttpRequestTimeoutException], [ConnectTimeoutException] and [SocketTimeoutException]
* are not retried.
* Set [retryOnTimeout] to `true` to retry on timeout.
* Note, that in this case, [HttpTimeout] plugin should be installed after [HttpRequestRetry].
*/
public fun retryOnException(maxRetries: Int = -1, retryOnTimeout: Boolean = false) {
retryOnExceptionIf(maxRetries) { _, cause ->
when {
cause.isTimeoutException() -> retryOnTimeout
cause is CancellationException -> false
cause is HttpRequestTimeoutException && retryOnTimeout -> true
else -> true
}
}
Expand Down Expand Up @@ -392,3 +396,10 @@ private val RetryDelayPerRequestAttributeKey =
AttributeKey<HttpRequestRetry.DelayContext.(Int) -> Long>(
"RetryDelayPerRequestAttributeKey"
)

private fun Throwable.isTimeoutException(): Boolean {
val exception = unwrapCancellationException()
return exception is HttpRequestTimeoutException
|| exception is ConnectTimeoutException
|| exception is SocketTimeoutException
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ class HttpRequestRetryTest {
}

test { client ->
assertFailsWith<HttpRequestTimeoutException> {
client.get {}
}
val response = client.get {}
assertEquals(HttpStatusCode.OK, response.status)
}
}
}

0 comments on commit 324425a

Please sign in to comment.