Dias
10/17/2018, 3:49 PMcy
10/17/2018, 3:55 PMcy
10/17/2018, 3:56 PMdave08
10/17/2018, 3:59 PMuspend fun <T> retryIO(
times: Int = Int.MAX_VALUE,
initialDelay: Long = 100, // 0.1 second
maxDelay: Long = 1000, // 1 second
factor: Double = 2.0,
block: suspend () -> T): T
{
var currentDelay = initialDelay
repeat(times - 1) {
try {
return block()
} catch (e: IOException) {
// you can log an error here and/or make a more finer-grained
// analysis of the cause to see if retry is needed
}
delay(currentDelay)
currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay)
}
return block() // last attempt
}
Just for retry though...cy
10/17/2018, 4:33 PMcy
10/17/2018, 4:33 PMcy
10/17/2018, 4:33 PMcy
10/17/2018, 4:34 PMcoroutineScope { }
is required at least in this casedave08
10/17/2018, 4:52 PMasync
block...
This might be better @cy: suspend fun <T> retryIO(
times: Int = Int.MAX_VALUE,
initialDelay: Long = 100, // 0.1 second
maxDelay: Long = 1000, // 1 second
factor: Double = 2.0,
block: suspend CoroutineScope.() -> T): T {
var currentDelay = initialDelay
repeat(times - 1) {
try {
return coroutineScope { block() }
} catch (e: IOException) {
// you can log an error here and/or make a more finer-grained
// analysis of the cause to see if retry is needed
}
delay(currentDelay)
currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay)
}
return coroutineScope { block() } // last attempt
}
dave08
10/17/2018, 4:59 PMasync
run without a CoroutineScope will run on the GlobalScope...? So the changed version just allows to create new coroutines in the new scope inside the block
lambda...dave08
10/17/2018, 4:59 PMcy
10/17/2018, 5:10 PMdave08
10/17/2018, 5:16 PMIOException
then it would be better? I don't think I understand what you mean 100%...cy
10/17/2018, 5:18 PMcy
10/17/2018, 5:19 PMdave08
10/17/2018, 5:23 PM