// com.alibaba.csp.sentinel.context.ContextUtil /** * Store the context in ThreadLocal for easy access. */ privatestatic ThreadLocal<Context> contextHolder = new ThreadLocal<>();
/** * Holds all {@link EntranceNode}. Each {@link EntranceNode} is associated with a distinct context name. */ privatestaticvolatile Map<String, DefaultNode> contextNameNodeMap = new HashMap<>();
privatestaticfinal ReentrantLock LOCK = new ReentrantLock(); privatestaticfinal Context NULL_CONTEXT = new NullContext();
publicstatic Context enter(String name, String origin){ // 防止用户输入的 Context name 和默认 context name 冲突,创建默认 Context 时会走一个 internalEnter 函数,那个函数中没有下述检查 if (Constants.CONTEXT_DEFAULT_NAME.equals(name)) { thrownew ContextNameDefineException( "The " + Constants.CONTEXT_DEFAULT_NAME + " can't be permit to defined!"); } return trueEnter(name, origin); }
// com.alibaba.csp.sentinel.CtSph#entryWithPriority(com.alibaba.csp.sentinel.slotchain.ResourceWrapper, int, boolean, java.lang.Object...) private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) throws BlockException { Context context = ContextUtil.getContext(); if (context instanceof NullContext) { // The {@link NullContext} indicates that the amount of context has exceeded the threshold, // so here init the entry only. No rule checking will be done. returnnew CtEntry(resourceWrapper, null, context); }
if (context == null) { // Using default context. CONTEXT_DEFAULT_NAME = sentinel_default_context context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME); } // ... }