Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle Retry-After header #151

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.rybalkinsd.kohttp.interceptors
import okhttp3.Interceptor
import okhttp3.Response
import java.net.SocketTimeoutException
import kotlin.math.max

/**
* Retry Interceptor
Expand All @@ -21,7 +22,7 @@ class RetryInterceptor(
private val failureThreshold: Int = 3,
private val invocationTimeout: Long = 0,
private val ratio: Int = 1,
private val errorStatuses: List<Int> = listOf(503, 504)
private val errorStatuses: List<Int> = listOf(429, 503, 504)
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
Expand All @@ -34,7 +35,13 @@ class RetryInterceptor(
delay = performAndReturnDelay(delay)
}
val response = chain.proceed(request)
if (!isRetry(response, attemptsCount)) return response

val nextTime = calculateNextRetry(response, attemptsCount, delay, chain.readTimeoutMillis())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a room for improvement here.

if () statement else return logic looks rather complicated

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to have two stages here:

  1. check if we're going to retry ?
  2. calculate next delay.

Probably, we need to modify performAndReturnDelay

if (nextTime != null) {
delay = nextTime
} else {
return response
}
} catch (e: SocketTimeoutException) {
if (attemptsCount >= failureThreshold) throw e
}
Expand All @@ -52,6 +59,14 @@ class RetryInterceptor(

private fun shouldDelay(attemptsCount: Int) = invocationTimeout > 0 && attemptsCount > 0

internal fun isRetry(response: Response, attemptsCount: Int): Boolean =
attemptsCount < failureThreshold && response.code() in errorStatuses
internal fun calculateNextRetry(response: Response, attemptsCount: Int, delay: Long, maxDelayTime: Int): Long? {
val retryAfter = response.header("Retry-After")?.toLongOrNull()?.let { it * 1000 }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (retryAfter != null && retryAfter > maxDelayTime) return null

return if (attemptsCount < failureThreshold && response.code() in errorStatuses) {
max(retryAfter ?: 0, delay)
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ class RetryInterceptorTest {
fun `not need retry if status code is 200`() {
val request = Request.Builder().url(HttpUrl.Builder().host(localhost).scheme("http").build()).build()
val response = Response.Builder().code(200).protocol(Protocol.HTTP_1_1).message("").request(request).build()
Assert.assertFalse(RetryInterceptor().isRetry(response, 1))
Assert.assertNull(RetryInterceptor().calculateNextRetry(response, 1, 2, 30))
}

@Test
fun `need retry if status code in error codes list`() {
val request = Request.Builder().url(HttpUrl.Builder().host(localhost).scheme("http").build()).build()
val response = Response.Builder().code(503).protocol(Protocol.HTTP_1_1).message("").request(request).build()
Assert.assertTrue(RetryInterceptor().isRetry(response, 1))
Assert.assertNotNull(RetryInterceptor().calculateNextRetry(response, 1, 2, 30))
}

private fun getCall(client: OkHttpClient) {
Expand Down