20
20
import java .util .concurrent .CancellationException ;
21
21
import java .util .concurrent .CompletableFuture ;
22
22
import java .util .concurrent .Future ;
23
+ import java .util .function .BiConsumer ;
23
24
24
25
/**
25
26
* A CompletableFuture implementation that propagates cancellations and calls completion handlers.
@@ -34,9 +35,8 @@ public class FailsafeFuture<T> extends CompletableFuture<T> {
34
35
private AbstractExecution execution ;
35
36
36
37
// Mutable state, guarded by "this"
37
- private Future <T > policyExecFuture ;
38
38
private Future <?> dependentStageFuture ;
39
- private Runnable cancelFn ;
39
+ private BiConsumer < Boolean , ExecutionResult > cancelFn ;
40
40
private List <Future <T >> timeoutFutures ;
41
41
private boolean cancelWithInterrupt ;
42
42
@@ -72,7 +72,7 @@ public synchronized boolean cancel(boolean mayInterruptIfRunning) {
72
72
this .cancelWithInterrupt = mayInterruptIfRunning ;
73
73
execution .cancelledIndex = Integer .MAX_VALUE ;
74
74
boolean cancelResult = super .cancel (mayInterruptIfRunning );
75
- cancelResult = cancelDependencies (mayInterruptIfRunning , cancelResult );
75
+ cancelDependencies (mayInterruptIfRunning , null );
76
76
ExecutionResult result = ExecutionResult .failure (new CancellationException ());
77
77
super .completeExceptionally (result .getFailure ());
78
78
executor .handleComplete (result , execution );
@@ -98,46 +98,31 @@ synchronized boolean completeResult(ExecutionResult result) {
98
98
return completed ;
99
99
}
100
100
101
- synchronized Future <T > getDependency () {
102
- return policyExecFuture ;
103
- }
104
-
105
101
synchronized List <Future <T >> getTimeoutDelegates () {
106
102
return timeoutFutures ;
107
103
}
108
104
109
105
/**
110
- * Cancels the dependency passing in the {@code interruptDelegate } flag, applies the retry cancel fn, and cancels all
106
+ * Cancels the dependency passing in the {@code mayInterrupt } flag, applies the retry cancel fn, and cancels all
111
107
* timeout dependencies.
112
108
*/
113
- synchronized boolean cancelDependencies (boolean interruptDelegate , boolean result ) {
114
- execution .interrupted = interruptDelegate ;
115
- if (policyExecFuture != null )
116
- result = policyExecFuture .cancel (interruptDelegate );
109
+ synchronized void cancelDependencies (boolean mayInterrupt , ExecutionResult cancelResult ) {
110
+ execution .interrupted = mayInterrupt ;
117
111
if (dependentStageFuture != null )
118
- dependentStageFuture .cancel (interruptDelegate );
119
- if (cancelFn != null )
120
- cancelFn .run ();
112
+ dependentStageFuture .cancel (mayInterrupt );
121
113
if (timeoutFutures != null ) {
122
114
for (Future <T > timeoutDelegate : timeoutFutures )
123
115
timeoutDelegate .cancel (false );
124
116
timeoutFutures .clear ();
125
117
}
126
- return result ;
118
+ if (cancelFn != null )
119
+ cancelFn .accept (mayInterrupt , cancelResult );
127
120
}
128
121
129
122
void inject (AbstractExecution execution ) {
130
123
this .execution = execution ;
131
124
}
132
125
133
- /**
134
- * Injects a {@code policyExecFuture} to be cancelled when this future is cancelled.
135
- */
136
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
137
- synchronized void injectPolicy (Future <?> policyExecFuture ) {
138
- this .policyExecFuture = (Future ) policyExecFuture ;
139
- }
140
-
141
126
/**
142
127
* Injects a {@code dependentStageFuture} to be cancelled when this future is cancelled.
143
128
*/
@@ -152,7 +137,7 @@ synchronized void injectStage(Future<?> dependentStageFuture) {
152
137
/**
153
138
* Injects a {@code cancelFn} to be called when this future is cancelled.
154
139
*/
155
- synchronized void injectCancelFn (Runnable cancelFn ) {
140
+ synchronized void injectCancelFn (BiConsumer < Boolean , ExecutionResult > cancelFn ) {
156
141
this .cancelFn = cancelFn ;
157
142
}
158
143
0 commit comments