btlines / grpcakkastream

Use GRPC services with the Akka-stream API
MIT License
69 stars 12 forks source link

Implement backpressure using the grpc-java API #17

Closed btlines closed 6 years ago

btlines commented 6 years ago

grpc-java provides a way to manage back pressure by manually controlling the flow. This is done by disabling the automatic flow control and then checking that the observer can accept more elements by calling isReady before calling onNext. If the observer is not ready the registered onReady handler will be invoke when the observer is able to accept more elements. On the other hand request allows to ask the producer to send more elements.

This PR is a first stab at wiring the grpc-java back-pressure mechanism into akka-streams.

btlines commented 6 years ago

I had a go with the async handlers and it doesn't play nice with the graph stage initialisation.

The grpc callbacks(beforeStart or onReadyHandler) seem to be called before the stage is fully initialised which gives the following error:

java.lang.IllegalStateException: not yet initialized: only setHandler is allowed in GraphStageLogic constructor
btlines commented 6 years ago

grpc-java seems to go with request(1) when auto flow control is on:

https://github.com/grpc/grpc-java/blob/c9b02db276403db4794c6e5ffc78b46889cd4ce8/stub/src/main/java/io/grpc/stub/ClientCalls.java#L410

ktoso commented 6 years ago

Regarding: "java.lang.IllegalStateException: not yet initialized: only setHandler ..."

You have to call things in preStart() of the GraphStage, then things are ready