forked from Netflix/Hystrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Netflix#1271 from daniilguit/master
Fix race condition in HystrixThreadPoolKey.asKey method
- Loading branch information
Showing
6 changed files
with
128 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
hystrix-core/src/main/java/com/netflix/hystrix/HystrixKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.netflix.hystrix; | ||
|
||
/** | ||
* Basic class for hystrix keys | ||
*/ | ||
public interface HystrixKey { | ||
/** | ||
* The word 'name' is used instead of 'key' so that Enums can implement this interface and it work natively. | ||
* | ||
* @return String | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* Default implementation of the interface | ||
*/ | ||
abstract class HystrixKeyDefault implements HystrixKey { | ||
private final String name; | ||
|
||
public HystrixKeyDefault(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
hystrix-core/src/main/java/com/netflix/hystrix/util/InternMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.netflix.hystrix.util; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* Utility to have 'intern' - like functionality, which holds single instance of wrapper for a given key | ||
*/ | ||
public class InternMap<K, V> { | ||
private final ConcurrentMap<K, V> storage = new ConcurrentHashMap<K, V>(); | ||
private final ValueConstructor<K, V> valueConstructor; | ||
|
||
public interface ValueConstructor<K, V> { | ||
V create(K key); | ||
} | ||
|
||
public InternMap(ValueConstructor<K, V> valueConstructor) { | ||
this.valueConstructor = valueConstructor; | ||
} | ||
|
||
public V interned(K key) { | ||
V existingKey = storage.get(key); | ||
V newKey = null; | ||
if (existingKey == null) { | ||
newKey = valueConstructor.create(key); | ||
existingKey = storage.putIfAbsent(key, newKey); | ||
} | ||
return existingKey != null ? existingKey : newKey; | ||
} | ||
|
||
public int size() { | ||
return storage.size(); | ||
} | ||
} |