2

I am using enumerators in a Play Framework application. When a request is sent to a specific URI, I call a MongoService (using reactivemongo) that returns an enumerator and this result is then sent through an HTTP stream using Ok.chunked(stream), the problem is that if I close the browser while receiving this stream response, Play Framework keeps receiving data from the reactivemongo enumerator. How can I cancel the mongo enumerator when the client closes the browser?

EDIT: Code example

The enumerator I get from reactivemongo:

def findStreamByCriteria(criteria: JsObject, limit: Int): Enumerator[E] = {
    collection.find(criteria).cursor[E](readPreference = ReadPreference.primary).enumerate(limit)
}

Then, in my controller I have:

  val stream = service.findStreamByCriteria(query)
  Future {
    Ok.chunked(stream)
  }

The problem is that when the chunked request is cancelled, reactivemongo keeps sending data to the HTTP server.

vicaba
  • 2,836
  • 1
  • 27
  • 45
  • Sample code? It may depend on how the `Enumerator` is created. – Michael Zajac Oct 28 '15 at 22:58
  • @m-z I have added a code example, hope this helps understanding the problem. – vicaba Oct 29 '15 at 13:25
  • I've got a similar problem: I've got a function that calculates a bit amount of data and uses an Enumerator to pass the results to the controller, but keeps processing until finished even when the client cancels the request. – Nick Jan 12 '16 at 10:25
  • @Nick I haven't tried it yet, but take a look at section "Custom Streaming" (at the end of the document). Maybe we can manually control the cursor if we can know somehow how to catch the "request status" (if the client have cancelled the request). Report the feedback to me if you try it please! – vicaba Jan 12 '16 at 14:07

0 Answers0