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 @@ -18,10 +19,10 @@ import java.net.SocketTimeoutException
* @author UDarya
* */
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 failureThreshold: Int = 3,
Copy link
Owner

Choose a reason for hiding this comment

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

Please avoid formatting change

private val invocationTimeout: Long = 0,
private val ratio: Int = 1,
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()
Copy link
Owner

Choose a reason for hiding this comment

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

probably need an ext function to get header by name.

BTW, did you check what is the metric of Retry-After values? s/ms ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably need an ext function to get header by name.

OK!

BTW, did you check what is the metric of Retry-After values? s/ms ?

The unit of Retry-After value is seconds. But in my code it is millisecond.
Sorry, I'll fix it.

Copy link
Owner

@rybalkinsd rybalkinsd Aug 27, 2019

Choose a reason for hiding this comment

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

no worries at all, that's why we're doing reviews :)

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