Skip to content

Commit 4ae31e9

Browse files
committed
Merge pull request #1102 from mattrjacobs/fix-request-cache-null-check
Delegate HystrixRequestCache check through concurrency strategy
2 parents 32dee9d + 5c9601e commit 4ae31e9

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
*/
1616
package com.netflix.hystrix;
1717

18-
import java.util.concurrent.ConcurrentHashMap;
19-
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import rx.Observable;
24-
2518
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
26-
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
2719
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;
2820
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableHolder;
2921
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import rx.Observable;
25+
26+
import java.util.concurrent.ConcurrentHashMap;
3027

3128
/**
3229
* Cache that is scoped to the current request as managed by {@link HystrixRequestVariableDefault}.
@@ -101,11 +98,12 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon
10198
/* package */<T> Observable<T> get(String cacheKey) {
10299
ValueCacheKey key = getRequestCacheKey(cacheKey);
103100
if (key != null) {
104-
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
101+
ConcurrentHashMap<ValueCacheKey, Observable<?>> cacheInstance = requestVariableForCache.get(concurrencyStrategy);
102+
if (cacheInstance == null) {
105103
throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
106104
}
107105
/* look for the stored value */
108-
return (Observable<T>) requestVariableForCache.get(concurrencyStrategy).get(key);
106+
return (Observable<T>) cacheInstance.get(key);
109107
}
110108
return null;
111109
}
@@ -128,7 +126,11 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon
128126
ValueCacheKey key = getRequestCacheKey(cacheKey);
129127
if (key != null) {
130128
/* look for the stored value */
131-
Observable<T> alreadySet = (Observable<T>) requestVariableForCache.get(concurrencyStrategy).putIfAbsent(key, f);
129+
ConcurrentHashMap<ValueCacheKey, Observable<?>> cacheInstance = requestVariableForCache.get(concurrencyStrategy);
130+
if (cacheInstance == null) {
131+
throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
132+
}
133+
Observable<T> alreadySet = (Observable<T>) cacheInstance.putIfAbsent(key, f);
132134
if (alreadySet != null) {
133135
// someone beat us so we didn't cache this
134136
return alreadySet;

0 commit comments

Comments
 (0)