How would you implement a timeout that I can kick ...
# coroutines
r
How would you implement a timeout that I can kick to start again each time something happens? Aka I'm looking for a heartbeat-like construct which shuts down an operation if it doesn't receive one every 2 minutes.
k
d
Maybe something like this?
Copy code
val channel = Channel<Unit>(Channel.CONFLATED)

launch {
  while (true) {
    val result = withTimeoutOrNull(2.minutes) {
      channel.receive()
    }
    if (result == null) {
      shutdown()
      break
    }
  }
}
We do use a construct similar to this internally in
kotlinx-coroutines-test
.
👍 1
r
🤔 ah, you reset the doomsday clock by sending something to the channel.