I have data being pulled from Reactive Mongo that I need to push through a Spray Rest API. I had hoped to do this with Chunked Responses. However I have discovered that the Enumerator that comes back from Reactive Mongo is capable of pushing through Spray faster than the network connection can handle. The result is that the connection is terminated.
I was able to resolve this problem using the Spray Ack feature in an intermediate actor. This along with a Blocking Await allowed me to create backpressure on the Enumerator. However I don't really want the Await. I would like to figure out a way to stream the data through Spray in a non-blocking fashion.
Is this possible? I have few ideas that might work if I can fill in the missing pieces.
1) Create back pressure on the Enumerator in a non-blocking fashion (no idea how to do this. Suggestions?)
2) Break the enumerator into smaller enumerators. Start consuming each enumerator only once the previous one has completed. I can do this using an actor. What I lack here is a way to break the larger enumerator into smaller enumerators.
3) Use something like the "Enumeratee.take" method. Where I would take some number of records from the Enumerator, then when I am ready, take some more. This is really just the same solution as 2) but from a slightly different perspective. This would require the enumerator to maintain state however. Is there a way to use the Enumeratee.take multiple times against the same enumerator without restarting from the beginning each time?
Can anyone offer any alternate suggestions that might work? Or if it is not possible please let me know.
I am using Play Enumerators 2.3.5