Backpressure with http

streams

(K River) #1

I see that there are so many ways to handle a Route Directive, say return a Route/onSuccess/complete/completeOKWithSource etc. To me it looks like for a REST endpoint to have backpressure, I have to use completeOKWithSource like below:

get(() -> 
        parameter(StringUnmarshallers.INTEGER, "n", n -> {
          final Source<JavaTweet, NotUsed> tws = Source.repeat(new JavaTweet("Hello World!")).take(n);
          return completeOKWithSource(tws, Jackson.marshaller(), EntityStreamingSupport.json());
        })
  1. In above, I’m returning a Source, to which akka http server can apply backpressure, right?

But, if I actually return something like a CompletableFuture, then there won’t be any backpressure support, right? Like in below example, it’s returning all or nothing with CompletableFuture.

  1. So even though akka http is fully async, there is no backpressure. Is my understanding correct?
get(() -> {
                    CompletionStage<UserRegistryActor.Users> futureUsers = PatternsCS
                        .ask(userRegistryActor, new UserRegistryMessages.GetUsers(), timeout)
                        .thenApply(obj ->(UserRegistryActor.Users) obj);
                    return onSuccess(() -> futureUsers,
                        users -> complete(StatusCodes.OK, users, Jackson.marshaller()));
                })
      )

Thanks!


(Johannes Rudolph) #2

Yes, in that case, your UserRegistryActor.Users already contains all the data in memory anyways which in case of the Jackson.marshaller is converted to JSON in one go (so that it then has the full json in memory). If you want to use the streaming JSON support, you need to provide the data in a stream in the first place.

Johannes


(K River) #3

Thank you @jrudolph . So is it fair to say that completeOKWithSource and completeWithSource are the only main ones that support backpressure?
Essentially if you use anything like complete or completeWithFuture then there is no backpressure applied, correct?
I feel like this is not stressed or highlighted anywhere in the documentation…