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
@@ -1,8 +1,14 @@
package io.github.rybalkinsd.kohttp.interceptors

import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.Response
import java.net.SocketTimeoutException
import java.time.Duration
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
import kotlin.math.max

/**
* Retry Interceptor
Expand All @@ -21,37 +27,64 @@ 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 {
val request = chain.request()
val maxDelayTime = chain.readTimeoutMillis()
var attemptsCount = 0
var delay = invocationTimeout
while (true) {
try {
if (shouldDelay(attemptsCount)) {
delay = performAndReturnDelay(delay)
}
if (shouldDelay(attemptsCount)) performDelay(delay)
val response = chain.proceed(request)
if (!isRetry(response, attemptsCount)) return response
delay = calculateNextRetry(response.headers(), response.code(), attemptsCount, delay, maxDelayTime)
?: return response
} catch (e: SocketTimeoutException) {
if (attemptsCount >= failureThreshold) throw e
}
attemptsCount++
}
}

internal fun performAndReturnDelay(delay: Long): Long {
private fun shouldDelay(attemptsCount: Int) = invocationTimeout > 0 && attemptsCount > 0

private fun performDelay(delay: Long) {
try {
Thread.sleep(delay)
} catch (ignored: InterruptedException) {
}
return delay * ratio
}

private fun shouldDelay(attemptsCount: Int) = invocationTimeout > 0 && attemptsCount > 0
internal fun calculateNextRetry(responseHeaders: Headers, responseCode: Int, attemptsCount: Int, delay: Long, maxDelayTime: Int): Long? {
val retryAfter = parseRetryAfter(responseHeaders)
if (retryAfter != null && retryAfter > maxDelayTime) return null

return if (attemptsCount < failureThreshold && responseCode in errorStatuses) {
max(retryAfter ?: 0, delay * ratio)
} else {
null
}
}

internal fun isRetry(response: Response, attemptsCount: Int): Boolean =
attemptsCount < failureThreshold && response.code() in errorStatuses
internal fun parseRetryAfter(headers: Headers): Long? {
val retryAfter = headers["Retry-After"] ?: return null
return parseRetryAfterAsNumber(retryAfter) ?: parseRetryAfterAsDate(retryAfter)
}

private fun parseRetryAfterAsNumber(headerValue: String): Long? {
return headerValue.toLongOrNull()?.let { it * 1000 }
}

private fun parseRetryAfterAsDate(headerValue: String): Long? {
return try {
val responseDate = ZonedDateTime.parse(headerValue, DateTimeFormatter.RFC_1123_DATE_TIME)
val now = ZonedDateTime.now()
val duration = Duration.between(now, responseDate)
duration.toMillis().takeIf { it > 0 }
} catch (ex: DateTimeParseException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import java.net.SocketTimeoutException
import java.security.MessageDigest
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull


class RetryInterceptorTest {
Expand Down Expand Up @@ -92,12 +95,13 @@ class RetryInterceptorTest {
}

@Test
fun `delay increase with step`() {
val retryInterceptor = RetryInterceptor(ratio = 2)
fun `delay increase by ratio`() {
val ratio = 2
val retryInterceptor = RetryInterceptor(ratio = ratio)
val invocationTimeout: Long = 1000
assert(retryInterceptor.performAndReturnDelay(invocationTimeout) == invocationTimeout * 2)
assert(retryInterceptor.performAndReturnDelay(invocationTimeout * 2) == invocationTimeout * 4)
assert(retryInterceptor.performAndReturnDelay(invocationTimeout * 4) == invocationTimeout * 8)
val responseHeaders = Headers.of()
val responseCode = 429
assertEquals(invocationTimeout * ratio, retryInterceptor.calculateNextRetry(responseHeaders, responseCode, 0, invocationTimeout, 10000))
}

@Test
Expand Down Expand Up @@ -138,14 +142,51 @@ 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.headers(), response.code(), 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.headers(), response.code(), 1, 2, 30))
}

@Test
fun `get Retry-After Header's value as next retry`() {
val maxDelayTime = 2001
val retryAfterSeconds = 2L
val retryAfter = RetryInterceptor().calculateNextRetry(Headers.of(mapOf("Retry-After" to retryAfterSeconds.toString())), 429, 1, 2, maxDelayTime)
assertEquals(retryAfterSeconds * 1000, retryAfter)
}

@Test
fun `get null as next retry if Retry-After Header's value exceeds maxDelayTime`() {
val maxDelayTime = 1999
val retryAfterSeconds = 2L
val retryAfter = RetryInterceptor().calculateNextRetry(Headers.of(mapOf("Retry-After" to retryAfterSeconds.toString())), 429, 1, 2, maxDelayTime)
assertNull(retryAfter)
}

@Test
fun `parse RetryAfter header whose unit is second`() {
val interceptor = RetryInterceptor()
val retryAfter = interceptor.parseRetryAfter(Headers.of(mapOf("Retry-After" to "2")))
assertEquals(2 * 1000, retryAfter)
}

@Test
fun `parse RetryAfter header whose unit is date`() {
val interceptor = RetryInterceptor()
val retryAfter = interceptor.parseRetryAfter(Headers.of(mapOf("Retry-After" to "Mon, 21 Oct 2115 07:28:00 GMT")))
assertNotNull(retryAfter)
}

@Test
fun `return null if RetryAfter header's date value is invalid`() {
val interceptor = RetryInterceptor()
val retryAfter = interceptor.parseRetryAfter(Headers.of(mapOf("Retry-After" to "Wed,, 21 Oct 2115 07:28:00 GMT")))
assertNull(retryAfter)
}

private fun getCall(client: OkHttpClient) {
Expand Down