Multiple HttpClient requests in parallel [0.9.15]

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple HttpClient requests in parallel [0.9.15]

danthegoodman
I have a handler which needs to make calls to an external service. I found that even though I attempted to make my requests execute in parallel, they are still only happening one at a time.

To demonstrate my problem, I created a simple app using ratpack 0.9.15: https://github.com/danthegoodman/test-ratpack-http-client/blob/master/src/ratpack/Ratpack.groovy

With the app, I make a call to '/hello?times=5'. That request in turn makes five requests to '/block' in parallel using HttpClient. The 'block' handler uses Thread.sleep inside of a blocking section, but I've tested it using apache benchmark (ab) to make sure it can handle multiple concurrent requests.

So when I call this handler, I expect to see this output in the logs:

  /hello: Making 5 requests
  /block: Recieved wait request for 500 ms
  /block: Recieved wait request for 500 ms
  /block: Recieved wait request for 500 ms
  /block: Recieved wait request for 500 ms
  /block: Recieved wait request for 500 ms
  /block: done in 502 ms
  /block: done in 502 ms
  /block: done in 503 ms
  /block: done in 506 ms
  /block: done in 513 ms
  /hello: made 5 requests in 574 ms

However, instead I see this output:

  /hello: Making 5 requests
  /block: Recieved wait request for 500 ms
  /block: done in 513 ms
  /block: Recieved wait request for 500 ms
  /block: done in 502 ms
  /block: Recieved wait request for 500 ms
  /block: done in 506 ms
  /block: Recieved wait request for 500 ms
  /block: done in 502 ms
  /block: Recieved wait request for 500 ms
  /block: done in 503 ms
  /hello: made 5 requests in 2694 ms
Reply | Threaded
Open this post in threaded view
|

Re: Multiple HttpClient requests in parallel [0.9.15]

Luke Daley
Administrator
This is intentional. Ratpack prevents implicit parallelism. 



As the comment says, this isn’t something we’ve started to sugar up yet.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple HttpClient requests in parallel [0.9.15]

danthegoodman
Ahh... Everything I've been seeing makes much more sense now. Thank you for the link to the blog posts, they made it much more clear.

And with the help of your explicit parallelism example, I was able to fix my little app to be  parallel. I pushed it out for others to see, in case someone else runs into this issue.

Thank you Luke!
Reply | Threaded
Open this post in threaded view
|

Re: Multiple HttpClient requests in parallel [0.9.15]

danthegoodman
If this is the case, what is the point of Promise.throttled()? The javadocs state:
Throttling can be used to limit concurrency. Typically to limit concurrent use of an external resource, such as a HTTP API.
I see that Throttle was added back in September of last year. Were the execution semantics different then?
Reply | Threaded
Open this post in threaded view
|

Re: Multiple HttpClient requests in parallel [0.9.15]

Luke Daley
Administrator
They were the same. The point of the throttle is to limit concurrency across executions.

Say you had a flakey external web service that can only handle 10 connections at a time, but every single request to a particular URL of your app made a request to this service. You could use a size 10 throttle to make sure you never have more than 10 requests out to the service.