"ratpack-blocking-*” threads

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

"ratpack-blocking-*” threads

stasmarkin
I was monitoring a web-app on ratpack and noticed big number of "ratpack-blocking-*” threads created. As I figured out,  that happened
due to I use Blocking API:  

   CompletableFuture<SomeResponse> cf = someService.doJob();
   Blocking.get(cf::get).then(context::render);

So there is a thread for each simultaneous request, that waits for some response and passed it to ratpack’s computeExecutor, right? If so, are there a way to avoid blockingExecutor? In my case that blocking thread do nothing, someResponse could be passed to  computeExecutor in someService’s executor, like:

   someService.doJob().thenApply(context::render)


I don't see significant performance degradation on creation and GC-ing that threads, but that behaviour it seems strange to me.

rus
Reply | Threaded
Open this post in threaded view
|

Re: "ratpack-blocking-*” threads

rus
Only blocking operations e.g. blocking I/O should be done using the Blocking API https://ratpack.io/manual/current/async.html

One option is to change your service to return Promise instead of CompletableFuture https://ratpack.io/manual/current/api/ratpack/exec/Promise.html

Then you can do someService.doJob().then(context::render)

HTH
Reply | Threaded
Open this post in threaded view
|

Re: "ratpack-blocking-*” threads

stasmarkin
aww, I didn't get an idea of Promise.of() at first time I read documentation, so I used Blocking API and that was a mistake.
Actually, Promise.of() is only a thing I was looking for. So it works pretty good:

    CompletableFuture<SomeResponse> cf = someService.doJob();
    Promise.<SomeResponse>of(downstream -> downstream.accept(cf))
        .then(ctx::render);

thanks!
Reply | Threaded
Open this post in threaded view
|

Re: "ratpack-blocking-*” threads

Luke Daley
Administrator
I recommend writing it like this:

    Promise.<SomeResponse>of(downstream -> downstream.accept(someService.doJob()))
        .then(ctx::render);