Colton Idle
03/15/2023, 5:34 AMyschimke
03/15/2023, 6:20 AMresponse.priorResponse
efemoney
03/15/2023, 12:17 PMrepeat(5) {...}
your api call with delay until its complete or it passes the success criteriaColton Idle
03/15/2023, 1:04 PMWith 5 second retries, I'd be tempted to make those external to the call, do in app code. Mainly because the interceptors are synchronous.That's an interesting take. So using coroutines and retrofit, it'd just be like a try catch, where the catch just tries again. That's what I would have likely tried if I didn't know about interceptors. Interceptors still feel like the right spot to do them. Hm. "Interceptors are synchronous" but all of that happens on a background thread, so it's not like it affects the main thread or anything right?
, should be sufficient to justThat repeat method is on a Flowable? (sorry just trying to understand how that would plug into my code). Or actually, maybe repeat is just a plain ol kotlin funciton. let me try to look it up!your api call with delay until its complete or it passes the success criteriarepeat(5) {...}
yschimke
03/15/2023, 1:15 PMColton Idle
03/15/2023, 1:36 PMefemoney
03/15/2023, 9:51 PMinternal suspend inline fun <T> retry(
times: Int = Int.MAX_VALUE,
until: (T) -> Boolean,
delay: Duration = 1.seconds,
block: () -> T,
): T {
var currentDelay = delay
repeat(times - 1) {
val result = block()
if (until(result)) return result
delay(currentDelay)
currentDelay *= 2 // exponential delay of x, 2x, 4x, 8x, 16x ...
}
return block() // last attempt
}
which can be used like so:
val result = retry(
times = 7,
block = {
runCatching { myApi.call(query = param) }
// OR
// myApi.call(query = param) which returns retrofit2.Response
// and you check response.isSuccessful
},
until = { it.isSuccess },
)
if (it.isSuccess) doSuccess() else reportFailure()
Chris Fillmore
03/17/2023, 2:51 AM