-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support FastThreadLocal mode. Resolves #94
1. Support FastThreadLocal mode when run with netty by config -Dttl.fastthreadlocal.enable=true, default is false 2. Caution: when FastThreadLocal mode is enabled, TransmittableThreadLocal can NEVER be inheritable. 3. Disable the tests which depends on the ability of InheritableThreadLocal when FastThreadLocal is enabled.
- Loading branch information
1 parent
3960572
commit 50a2b6d
Showing
9 changed files
with
186 additions
and
32 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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/alibaba/ttl/internal/FastThreadLocalValue.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,29 @@ | ||
package com.alibaba.ttl.internal; | ||
|
||
import io.netty.util.internal.FastThreadLocal; | ||
|
||
/** | ||
* {@link FastThreadLocal} implementation for {@link com.alibaba.ttl.TransmittableThreadLocal} value holder | ||
* | ||
* @author Yang Fang (snoop dot fy at gmail dot com) | ||
* @since 2.7.0 | ||
*/ | ||
public class FastThreadLocalValue<T> implements TtlValue<T> { | ||
|
||
private FastThreadLocal<T> holder = new FastThreadLocal<T>(); | ||
|
||
@Override | ||
public T get() { | ||
return holder.get(); | ||
} | ||
|
||
@Override | ||
public void set(T t) { | ||
holder.set(t); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
holder.remove(); | ||
} | ||
} |
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,17 @@ | ||
package com.alibaba.ttl.internal; | ||
|
||
/** | ||
* Hold {@link com.alibaba.ttl.TransmittableThreadLocal} value, can be implemented in varied ways depending on the runtime. | ||
* | ||
* @author Yang Fang (snoop dot fy at gmail dot com) | ||
* @since 2.7.0 | ||
*/ | ||
public interface TtlValue<T> { | ||
|
||
T get(); | ||
|
||
void set(T t); | ||
|
||
void remove(); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/alibaba/ttl/internal/TtlValueFactory.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,49 @@ | ||
package com.alibaba.ttl.internal; | ||
|
||
/** | ||
* {@link com.alibaba.ttl.TransmittableThreadLocal} value holder factory. | ||
* | ||
* <p> | ||
* If there is netty in the runtime and | ||
* {@link io.netty.util.internal.FastThreadLocal} is supported. You can just set | ||
* | ||
* <pre>-Dttl.fastthreadlocal.enable=true</pre> | ||
* | ||
* to enable FastThreadLocal mode for better performance. | ||
* </p> | ||
* | ||
* <p> | ||
* Caution: If FastThreadLocal mode is enabled, {@link com.alibaba.ttl.TransmittableThreadLocal} | ||
* will NEVER be inheritable! | ||
* </p> | ||
* | ||
* @author Yang Fang (snoop dot fy at gmail dot com) | ||
* @since 2.7.0 | ||
*/ | ||
public class TtlValueFactory { | ||
|
||
private static final String FAST_THREAD_LOCAL_ENABLE = "ttl.fastthreadlocal.enable"; | ||
|
||
private static final boolean IS_FAST_THREAD_LOCAL_SUPPORT = isClassPresent("io.netty.util.internal.FastThreadLocal"); | ||
|
||
public static <T> TtlValue<T> create() { | ||
if (isFastThreadLocalEnabled() && IS_FAST_THREAD_LOCAL_SUPPORT) { | ||
return new FastThreadLocalValue<T>(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public static boolean isFastThreadLocalEnabled() { | ||
return Boolean.parseBoolean(System.getProperty(FAST_THREAD_LOCAL_ENABLE, "false")); | ||
} | ||
|
||
private static boolean isClassPresent(String className) { | ||
try { | ||
Class.forName(className); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
} |
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