Comcast / jrugged

A Java libary of robustness design patterns
Apache License 2.0
266 stars 93 forks source link

Timeboxer Implementation #14

Open cobyyoung opened 11 years ago

cobyyoung commented 11 years ago

Timeouts are a useful stability pattern, however many high-level APIs omit the ability to set a timeout.

It would be nice if JRugged could provide a solution that allows existing services to be timeboxed.

If the wrapped service takes longer than a predefined timeLimit, the wrapper would throw some sort TimeoutException.

Also, after a timeLimit has been exceed, there should be some (optional) mechanism that attempts to cancel the wrapped service call (perhaps via sending an Interrupt, or a more explicit cancel() method defined by a JRugged Cancellable.cancel() interface).

It would be very nice to include some documentation that illustrates an example of how a Timeboxer could be used to wrap HttpClient calls, such that HttpRequests are nicely aborted after the timeLimit is exceeded.

The wrapper probably needs to expose mechanisms to control the threading model used to execute the Futures that the wrapper service will use internally. Perhaps this is as simple as allowing a custom ExecutorService to be injected into the wrapper.

An optional, but potentially useful, feature could be to allow several services to be simultaneously wrapped in the same timebox. For example: i have a composite service that assembles data from multiple different backends, if any of them haven't finished by the end of the timeLimit, i want to grab the results from the services that did finish and cancel the calls that weren't fast enough.

cobyyoung commented 11 years ago

An relevant read regarding cleanly shutting down java threads: http://www.javaspecialists.eu/archive/Issue056.html