MCPcopy Index your code
hub / github.com/PhantomThief/scope

github.com/PhantomThief/scope @1.0.23

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0.23 ↗ · + Follow
182 symbols 717 edges 19 files 35 documented · 19%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

scope

Build Status Coverage Status Total alerts Language grade: Java Maven Central

对ThreadLocal的高级封装

  • 显示的声明Scope的范围
  • 强类型
  • 可以在线程池中安全的使用,并防止泄露
  • 只支持jdk1.8

Usage


private static final ScopeKey<String> TEST_KEY = allocate();

public void basicUse() {
    runWithNewScope(() -> {
         TEST_KEY.set("abc");
         String result = TEST_KEY.get(); // get "abc"

         runAsyncWithCurrentScope(()-> {
             String resultInScope = TEST_KEY.get(); // get "abc"
         }, executor);
    });
}

// 或者声明一个Scope友好的ExecutorService,方法如下:
private static class ScopeThreadPoolExecutor extends ThreadPoolExecutor {

    ScopeThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
            TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    /**
     * same as {@link java.util.concurrent.Executors#newFixedThreadPool(int)}
     */ 
    static ScopeThreadPoolExecutor newFixedThreadPool(int nThreads) {
        return new ScopeThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    /**
     * 只要override这一个方法就可以
     * 所有submit, invokeAll等方法都会代理到这里来
     */
    @Override
    public void execute(Runnable command) {
        Scope scope = getCurrentScope();
        super.execute(() -> runWithExistScope(scope, command::run));
    }
}

private ExecutorService executor = ScopeThreadPoolExecutor.newFixedThreadPool(10);

public void executeTest() {
    runWithNewScope(() -> {
       TEST_KEY.set("abc");
       executor.submit(() -> {
           TEST_KEY.get(); // get abc
       });
    });
}

Extension points exported contracts — how you extend this code

LongCostTrack (Interface)
当请求结束时,调用#close() 关闭追踪 @author w.vela Created on 2019-10-22. [2 implementers]
src/main/java/com/github/phantomthief/scope/LongCostTrack.java
RetryPolicy (Interface)
@author w.vela Created on 2019-01-07.
src/main/java/com/github/phantomthief/scope/RetryPolicy.java

Core symbols most depended-on inside this repo

get
called by 117
src/main/java/com/github/phantomthief/scope/MyThreadLocal.java
set
called by 32
src/main/java/com/github/phantomthief/scope/MyThreadLocal.java
callWithRetry
called by 29
src/main/java/com/github/phantomthief/scope/ScopeAsyncRetry.java
retryNTimes
called by 24
src/main/java/com/github/phantomthief/scope/RetryPolicy.java
beginScope
called by 21
src/main/java/com/github/phantomthief/scope/Scope.java
endScope
called by 21
src/main/java/com/github/phantomthief/scope/Scope.java
runWithNewScope
called by 16
src/main/java/com/github/phantomthief/scope/Scope.java
getCurrentScope
called by 15
src/main/java/com/github/phantomthief/scope/Scope.java

Shape

Method 152
Class 26
Interface 3
Enum 1

Languages

Java100%

Modules by API surface

src/test/java/com/github/phantomthief/scope/ScopeAsyncRetryTest.java47 symbols
src/test/java/com/github/phantomthief/scope/ScopeTest.java20 symbols
src/main/java/com/github/phantomthief/scope/ScopeAsyncRetry.java18 symbols
src/main/java/com/github/phantomthief/scope/Scope.java17 symbols
src/main/java/com/github/phantomthief/scope/ScopeUtils.java14 symbols
src/test/java/com/github/phantomthief/scope/ScopeAsyncRetryBenchMark.java11 symbols
src/main/java/com/github/phantomthief/scope/ScopeKey.java11 symbols
src/main/java/com/github/phantomthief/scope/SubstituteThreadLocal.java7 symbols
src/main/java/com/github/phantomthief/scope/RetryPolicy.java7 symbols
src/test/java/com/github/phantomthief/scope/ScopeUtilsTest.java4 symbols
src/main/java/com/github/phantomthief/scope/NettyFastThreadLocal.java4 symbols
src/main/java/com/github/phantomthief/scope/MyThreadLocal.java4 symbols

For agents

$ claude mcp add scope \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact