Skip to content

Commit

Permalink
fix: improve remote evaluation fetch retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jan 22, 2024
1 parent 46745cc commit 88338fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.amplitude.experiment.storage.LoadStoreCache
import com.amplitude.experiment.storage.Storage
import com.amplitude.experiment.storage.getFlagStorage
import com.amplitude.experiment.storage.getVariantStorage
import com.amplitude.experiment.util.FetchException
import com.amplitude.experiment.util.AsyncFuture
import com.amplitude.experiment.util.Backoff
import com.amplitude.experiment.util.BackoffConfig
Expand Down Expand Up @@ -36,6 +37,7 @@ import org.json.JSONObject
import java.io.IOException
import java.lang.IllegalStateException
import java.util.concurrent.Callable
import java.util.concurrent.ExecutionException
import java.util.concurrent.Future
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -293,7 +295,7 @@ internal class DefaultExperimentClient internal constructor(
val variants = doFetch(user, timeoutMillis, options).get()
storeVariants(variants, options)
} catch (e: Exception) {
if (retry) {
if (retry && shouldRetryFetch(e)) {
startRetries(user, options)
}
throw e
Expand Down Expand Up @@ -339,6 +341,9 @@ internal class DefaultExperimentClient internal constructor(
override fun onResponse(call: Call, response: Response) {
try {
Logger.d("Received fetch variants response: $response")
if (!response.isSuccessful) {
throw FetchException(response.code, "fetch error response: $response")
}
val variants = parseResponse(response)
future.complete(variants)
} catch (e: Exception) {
Expand Down Expand Up @@ -664,6 +669,15 @@ internal class DefaultExperimentClient internal constructor(
}
}
}

private fun shouldRetryFetch(e: Exception): Boolean {
println(e.toString())
if (e is ExecutionException && e.cause is FetchException) {
val fetchException = e.cause as FetchException
return fetchException.statusCode < 400 || fetchException.statusCode >= 500 || fetchException.statusCode == 429
}
return true
}
}

data class VariantAndSource(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.amplitude.experiment.util

import okio.IOException

class FetchException internal constructor(
val statusCode: Int,
message: String
) : IOException(message)

0 comments on commit 88338fb

Please sign in to comment.