Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ public val HttpRequestRetry: ClientPlugin<HttpRequestRetryConfig> = createClient
return subRequest
}

onRequest { request, _ ->
val maxRetriesValue = request.attributes.getOrNull(MaxRetriesPerRequestAttributeKey) ?: maxRetries
request.attributes.put(MaxRetriesPerRequestAttributeKey, maxRetriesValue)
}

on(Send) { request ->
var retryCount = 0
val shouldRetry = request.attributes.getOrNull(ShouldRetryPerRequestAttributeKey) ?: shouldRetry
Expand Down Expand Up @@ -376,8 +381,8 @@ public val HttpRequestRetry: ClientPlugin<HttpRequestRetryConfig> = createClient
}

/**
* A context for [HttpRequestRetry.Configuration.shouldRetry]
* and [HttpRequestRetry.Configuration.shouldRetryOnException]
* A context for [HttpRequestRetryConfig.shouldRetry]
* and [HttpRequestRetryConfig.shouldRetryOnException]
*
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.plugins.HttpRetryShouldRetryContext)
*/
Expand All @@ -389,7 +394,7 @@ public class HttpRetryShouldRetryContext(
)

/**
* A context for [HttpRequestRetry.Configuration.delayMillis].
* A context for [HttpRequestRetryConfig.delayMillis].
* Contains a non-null [response] or [cause] but not both.
*
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.plugins.HttpRetryDelayContext)
Expand All @@ -401,7 +406,7 @@ public class HttpRetryDelayContext internal constructor(
)

/**
* A context for [HttpRequestRetry.Configuration.modifyRequest].
* A context for [HttpRequestRetryConfig.modifyRequest].
* Contains a non-null [response] or [cause] but not both.
*
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.plugins.HttpRetryModifyRequestContext)
Expand Down Expand Up @@ -445,8 +450,7 @@ public fun HttpRequestBuilder.retry(block: HttpRequestRetryConfig.() -> Unit) {
attributes.put(ModifyRequestPerRequestAttributeKey, configuration.modifyRequest)
}

private val MaxRetriesPerRequestAttributeKey =
AttributeKey<Int>("MaxRetriesPerRequestAttributeKey")
internal val MaxRetriesPerRequestAttributeKey = AttributeKey<Int>("MaxRetriesPerRequestAttributeKey")

private val ShouldRetryPerRequestAttributeKey =
AttributeKey<HttpRetryShouldRetryContext.(HttpRequest, HttpResponse) -> Boolean>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlinx.coroutines.cancel

/**
* HttpSend pipeline interceptor function
Expand Down Expand Up @@ -89,8 +89,13 @@ public class HttpSend private constructor(
.trimMargin()
}
context.setBody(content)

val realSender: Sender = DefaultSender(plugin.maxSendCount, scope)
val maxRetriesFromRetryPlugin = context.attributes.getOrNull(MaxRetriesPerRequestAttributeKey)
val maxRetries = if (maxRetriesFromRetryPlugin != null) {
maxRetriesFromRetryPlugin + 1 // +1 for the initial request
} else {
plugin.maxSendCount
}
val realSender: Sender = DefaultSender(maxRetries, scope)
var interceptedSender = realSender
for (interceptor in plugin.interceptors.reversed()) {
interceptedSender = InterceptedSender(interceptor, interceptedSender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,35 @@ class HttpRequestRetryTest {
assertEquals(HttpStatusCode.OK, response.status)
}
}

@Test
fun testRetryHasPriorityOverHttpSend() = testWithEngine(MockEngine) {
val maxRetriesCount = 5
var counter = 1
config {
engine {
addHandler {
if (counter < maxRetriesCount) {
counter++
respondError(HttpStatusCode.InternalServerError)
} else {
respondOk()
}
}
}

install(HttpRequestRetry) {
retryOnServerErrors(maxRetriesCount)
delayMillis { 0L }
}
install(HttpSend) {
maxSendCount = 1
}
}

test { client ->
assertEquals(HttpStatusCode.OK, client.get("/").status)
assertEquals(maxRetriesCount, counter)
}
}
}