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,13 @@
package io.github.rybalkinsd.kohttp.interceptors

import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.Response
import java.net.SocketTimeoutException
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.Instant
import kotlin.math.max

/**
* Retry Interceptor
Expand All @@ -21,37 +26,58 @@ 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 {

//https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date#Syntax
private val httpDateFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz")

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

internal fun isRetry(response: Response, attemptsCount: Int): Boolean =
attemptsCount < failureThreshold && response.code() in errorStatuses
return if (attemptsCount < failureThreshold && responseCode in errorStatuses) {
max(retryAfter ?: 0, delay * ratio)
} else {
null
}
}

internal fun parseRetryAfter(headers: Headers): Long? {
val retryAfter = headers["Retry-After"] ?: return null
return retryAfter.toLongOrNull()?.let { it * 1000 } ?: try {
val date = httpDateFormat.parse(retryAfter).toInstant().epochSecond
Copy link
Owner

Choose a reason for hiding this comment

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

Working with time is tricky. I strongly suggest several things:

  1. Do not do any custom calculations. with timestamps(epoch, etc )

  2. Use joda time primitives. It will take care of all things like Arabic, Japanese, or Thai calendars.

     val responseDate = ZonedDateTime.parse("...")
     val now = ZonedDateTime.now()
     val duration = Duration.between(responseDate, now)
    
  3. Please separate two stratagies in your code: 1st is to parse as number; 2nd is to parse as timestamp

val now = Instant.now().epochSecond
(date - now).takeIf { it > 0 }
Copy link
Owner

Choose a reason for hiding this comment

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

It's great that you check it to be positive!

} catch (ex: ParseException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -91,12 +94,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 @@ -137,14 +141,35 @@ 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 `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 "Wed, 21 Oct 2115 07:28:00 GMT")))
assertNotNull(retryAfter)
}

@Test
fun `return null if cannot parse RetryAfter header`() {
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