Skip to content

Commit fefdc37

Browse files
authored
Merge pull request #1570 from mattrjacobs/only-wrap-timeout-callable-when-necessary
Only create the HystrixContextRunnable for running the timeout fallback...
2 parents f0d5482 + 951c6f9 commit fefdc37

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -1132,17 +1132,8 @@ public Subscriber<? super R> call(final Subscriber<? super R> child) {
11321132
// if the child unsubscribes we unsubscribe our parent as well
11331133
child.add(s);
11341134

1135-
/*
1136-
* Define the action to perform on timeout outside of the TimerListener to it can capture the HystrixRequestContext
1137-
* of the calling thread which doesn't exist on the Timer thread.
1138-
*/
1139-
final HystrixContextRunnable timeoutRunnable = new HystrixContextRunnable(originalCommand.concurrencyStrategy, new Runnable() {
1140-
1141-
@Override
1142-
public void run() {
1143-
child.onError(new HystrixTimeoutException());
1144-
}
1145-
});
1135+
//capture the HystrixRequestContext upfront so that we can use it in the timeout thread later
1136+
final HystrixRequestContext hystrixRequestContext = HystrixRequestContext.getContextForCurrentThread();
11461137

11471138
TimerListener listener = new TimerListener() {
11481139

@@ -1157,6 +1148,15 @@ public void tick() {
11571148
// shut down the original request
11581149
s.unsubscribe();
11591150

1151+
final HystrixContextRunnable timeoutRunnable = new HystrixContextRunnable(originalCommand.concurrencyStrategy, hystrixRequestContext, new Runnable() {
1152+
1153+
@Override
1154+
public void run() {
1155+
child.onError(new HystrixTimeoutException());
1156+
}
1157+
});
1158+
1159+
11601160
timeoutRunnable.run();
11611161
//if it did not start, then we need to mark a command start for concurrency metrics, and then issue the timeout
11621162
}

hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixContextRunnable.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public HystrixContextRunnable(Runnable actual) {
3434
}
3535

3636
public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) {
37+
this(concurrencyStrategy, HystrixRequestContext.getContextForCurrentThread(), actual);
38+
}
39+
40+
public HystrixContextRunnable(final HystrixConcurrencyStrategy concurrencyStrategy, final HystrixRequestContext hystrixRequestContext, final Runnable actual) {
3741
this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() {
3842

3943
@Override
@@ -43,7 +47,7 @@ public Void call() throws Exception {
4347
}
4448

4549
});
46-
this.parentThreadState = HystrixRequestContext.getContextForCurrentThread();
50+
this.parentThreadState = hystrixRequestContext;
4751
}
4852

4953
@Override

0 commit comments

Comments
 (0)