Dealing with blocking code in handler

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

Dealing with blocking code in handler

Neeraj
Hi All,

   Would the following pseudo code be a valid pattern when dealing with potentially blocking code in a handler ? If the 'stopAndPublish' method were to take a long time to return due to the Metrics Service being slow/unreachable, at least the result of the handler chain is not delayed.

void handle(Context context) {
      MetricsMeasurement.start()

      handler.handle()

      Thread.start {
           //Blocking call to external metrics service
           MetricsMeasurement.stopAndPublish()
      }
}
Reply | Threaded
Open this post in threaded view
|

Re: Dealing with blocking code in handler

Luke Daley
Administrator
You should prefer using the blocking API for this.

http://www.ratpack-framework.org/manual/current/api/org/ratpackframework/block/Blocking.html

It has the benefit of reusing threads from the worker thread pool and not creating more overhead due to extra thread creation.

However, if performance/throughput is not really a concern then the code you have will be fine.
Reply | Threaded
Open this post in threaded view
|

Re: Dealing with blocking code in handler

Neeraj
Thanks, that sounds right.