I'm still beating my head against bringing a backp...
# coroutines
d
I'm still beating my head against bringing a backpressure-enabled callback API into the coroutines world. I wasn't happy with the
flow
solution (it doesn't work for bidirectional streaming), so I am back to fighting with channels. I need to explicitly request more elements from upstream when the channel runs empty (i.e.
receive
needs to suspend because the buffer is empty). I thought I could "just implement ReceiveChannel", but this proved difficult, because it's not just a simple interface with
suspend fun receive
...
Copy code
override suspend fun receive(): E {
  val element = delegate.poll()
  if (element != null) return element
  api.request(bufferSize)
  return delegate.receive()
}
This is not correct, because other ways to receive (such as
onReceive
) would have to all be implemented from scratch... This doesn't sound right... Is there really no simple way to do this?
z
receive needs to suspend because the buffer is empty
This is already how channels work, why do you want a custom implementation?
d
Because if the channel is empty I need to ask the server to produce more items using
request
.
The fun of callback-based APIs.
z
Isn’t your
launchReceive
function doing this already? It loops and sends things into the channel. It will keep doing that as long as the channel isn’t full, and then suspend until it has capacity again.
d
Yes, I have that solution now, after beating my head against it for 3 days 😄 I was just asking for comments on if that is the best way to do so.
And my channel is explicitly not limited in capacity, because the backpressure in gRPC is cooperative. Even if I tell the server to not send anymore it can still do so and I need to cope.
z
That’s a pretty common pattern in CSP programming, e.g. most Go programs.
d
I'm not experienced in Go (coworker keeps trying to push it on me, but I really do not find the language enjoyable). But good to hear it is at least somewhat reasonable, thanks.
z
I’m not a huge fan of the language either 😅 But yea, what you’re doing looks pretty idiomatic
d
Thanks!