I'm having problem configuring `HttpRequestRetry` correctly. This is the current configuration: ```...
l

Leon Kiefer

almost 2 years ago
I'm having problem configuring
HttpRequestRetry
correctly. This is the current configuration:
fun HttpClientConfig<*>.configureRetries(
        methods: List<HttpMethod> = listOf(
            HttpMethod.Get,
            HttpMethod.Put,
            HttpMethod.Delete
        )
    ) {
        install(HttpRequestRetry) {
            retryIf(10) { request, response ->
                if (request.method in methods) {
                    response.status.value in listOf(502, 503, 504)
                } else {
                    false
                }.also {
                    if (it) {
                        println("Retry request because of status ${response.status.value}")
                    }
                }
            }
            retryOnExceptionIf(10) { _, cause ->
                if (cause is CancellationException) {
                    return@retryOnExceptionIf false
                }
                val shouldRetry = when (cause) {
                    is HttpRequestTimeoutException,
                    is ConnectTimeoutException,
                    is SocketTimeoutException,
                    is java.net.SocketTimeoutException,
                    is java.net.SocketException,
                    is java.io.EOFException -> true

                    is java.io.IOException -> cause.message == "HTTP/1.1 header parser received no bytes"
                            || cause.message == "chunked transfer encoding, state: READING_LENGTH"

                    else -> false
                }
                if (shouldRetry) {
                    println("Retry request because of exception $cause")
                } else {
                    println("Don't retry request because of exception $cause")
                }

                shouldRetry
            }
            exponentialDelay()
        }
    }
However in our production environment requests fail without being retried, with an
java.io.IOException: chunked transfer encoding, state: READING_LENGTH
error. Neither
Retry request because of exception $cause
nor
Don't retry request because of exception $cause
is printed in the console, so it looks like the exception isn't catched at all by
HttpRequestRetry
. We are using the
Java
client and making a GET requests. Ktor version is
2.3.7