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.