failsafe-lib / failsafe

Fault tolerance and resilience patterns for the JVM
https://failsafe.dev
Apache License 2.0
4.17k stars 296 forks source link

AsyncExecution improvements #290

Closed jhalterman closed 2 years ago

jhalterman commented 3 years ago

It would be nice to be able to register a cancellation handler, in order to help propagate cancellations through async executions:

failsafe.getAsyncExecution(exec -> {
  Future future = workInSomeOtherThread().whenDone(result -> {
    exec.recordResult(result);
  };
  exec.onCancel(shouldInterrupt -> future.cancel(shouldInterrupt));
});

A shortcut for this would be to recognize separately created futures, so that cancellations can be propagated to them:

failsafe.getFutureAsyncExecution(exec -> {
  Future future = workInSomeOtherThread().whenDone(result -> {
    exec.recordResult(result);
  };
  return future;
});

getStageAsyncExecution should maybe be deprecated in favor of getFutureAsyncExecution since the former already has a completion handler that Failsafe uses to record results, so the explicit AsyncExecution.record is not needed.

jhalterman commented 2 years ago

3.0 has been released, which includes this improvement.