Search code examples
scalafuturefinagle

Infinite Sequence of requests in Finagle with Future


I have an HTTP API endpoint that I need constantly check for new values. Luckily, it's supports long polling. So the idea is that I need to implement 'an infinite loop' where I do a request, wait for a response (at most 10mins), get some value from response and produce a side-effect by storing them somewhere, make another request.

Given that I have some function the call to which will start this 'infinite loop' I also need to return a Closable to satisfy Finagle API I'm integrating with so the process can be interrupted. If HTTP request fails I need to re-try immediately.

Now I need to figure out how to implement this with Futures in Finagle. I wonder whether I can user a recursion by applying transform to response Future?.. Or am I missing something and there is a more straightforward way to do it in Finagle?

Thanks!


Solution

  • I am not sure I can imagine how it (what you described) can be made any more straightforward than recursive:

      def keepCalling: Future[Unit] = makeRequest
        .flatMap { response => 
           processResponse(response)
           if(cancelled) Future.Unit else keepCalling
         }
    

    Note, that this is actually not recursive in the traditional sense, as we should normally expect (with some reservations) only one instance of keepCalling to be on stack at any given time, since the "recursive" invocation happens on a different thread.