Skip to content

Commit ec642b0

Browse files
committed
Merge pull request Netflix#1093 from caarlos0/request-cache
Fixed NPE on request cache when HystrixRequestContext is not initialized
2 parents 1b4c676 + 6363051 commit ec642b0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import rx.Observable;
2424

2525
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
26+
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
2627
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;
2728
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableHolder;
2829
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
@@ -100,6 +101,9 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon
100101
/* package */<T> Observable<T> get(String cacheKey) {
101102
ValueCacheKey key = getRequestCacheKey(cacheKey);
102103
if (key != null) {
104+
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
105+
throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
106+
}
103107
/* look for the stored value */
104108
return (Observable<T>) requestVariableForCache.get(concurrencyStrategy).get(key);
105109
}

hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public void testCache() {
6666
}
6767
}
6868

69+
@Test(expected = IllegalStateException.class)
70+
public void testCacheWithoutContext() {
71+
HystrixRequestCache.getInstance(
72+
HystrixCommandKey.Factory.asKey("command1"),
73+
HystrixConcurrencyStrategyDefault.getInstance()
74+
).get("any");
75+
}
76+
6977
@Test
7078
public void testClearCache() {
7179
HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();

0 commit comments

Comments
 (0)