https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

diesieben07

01/09/2020, 9:36 AM
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

Zach Klippenstein (he/him) [MOD]

01/09/2020, 10:32 PM
receive needs to suspend because the buffer is empty
This is already how channels work, why do you want a custom implementation?
d

diesieben07

01/09/2020, 10:34 PM
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

Zach Klippenstein (he/him) [MOD]

01/09/2020, 10:35 PM
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

diesieben07

01/09/2020, 10:36 PM
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

Zach Klippenstein (he/him) [MOD]

01/09/2020, 10:37 PM
That’s a pretty common pattern in CSP programming, e.g. most Go programs.
d

diesieben07

01/09/2020, 10:39 PM
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

Zach Klippenstein (he/him) [MOD]

01/09/2020, 10:41 PM
I’m not a huge fan of the language either 😅 But yea, what you’re doing looks pretty idiomatic
d

diesieben07

01/09/2020, 10:42 PM
Thanks!
5 Views