Jorge Viana
07/28/2023, 2:14 PMWesley Hartford
07/28/2023, 4:04 PMJorge Viana
07/28/2023, 6:14 PMWesley Hartford
07/28/2023, 7:38 PMfun <T> Flow<T>.withElementDeadline(
deadline: Duration
): Flow<T> = channelFlow {
var cancelJob: Job? = null
onEach {
cancelJob?.cancel()
cancelJob = launch {
delay(deadline)
this@channelFlow.cancel("More than $deadline between elements")
}
}
.collect { send(it) }
}
This has nothing to do with gRPC, but would allow your client to cancel the flow (which should cancel the call on the server as well) and retry if there is a long gap between elements.Wesley Hartford
07/28/2023, 7:41 PMsuspend fun main() {
flow {
while (true) {
val delay = Random.nextLong(100, 550).milliseconds
println("Dealy: $delay")
delay(delay)
emit(delay)
}
}.withElementDeadline(500.milliseconds)
.collectIndexed { index, value -> println("RX: $index: $value") }
}
Jorge Viana
07/28/2023, 8:25 PM