Hystrix command requiring cleanup when timed-out

531 Views Asked by At

I have an Hystrix command that requires cleanup when timed-out. Our current approach to handle this is as follows:

public class MyCommand extends HystrixCommand<MyResponse> {

    @Override
    public MyResponse run() {
        // do stuff

        // Cleanup if timed out
        if( this.isResponseTimedOut() ) {
           // perform cleanup
        }

        return myresponse;
    }
}

Does the Hystrix framework provide another way for that?

1

There are 1 best solutions below

0
On

As far as I know you can't determine reliably inside the run() method if a timeout has occured: The run() method is running in a separated thread while the caller is waiting. While you are checking in the run() method if a timeout has occured, the calling thread might time out and you will not know about it. The code you suggest will work most of the time, but there is a small time window on each call where it will not work.

A save place to check if a timeout has occured would be the fallback() method. Please note that the original "do stuff" as described in your method might still be running at this point: Hystrix will send the original run() method a java.lang.Thread.interrupt(). Depending on your "do stuff" the results may vary.

I hope this is an answer to your question. If you could give more information in your question, I can update my answer to include it.