//A container of some process and ways of notification when the process is finished. public interface ProcessorSlot<T> { //Entrance of this slot. //@param context current Context //@param resourceWrapper current resource //@param param generics parameter, usually is a com.alibaba.csp.sentinel.node.Node //@param count tokens needed //@param prioritized whether the entry is prioritized //@param args parameters of the original call //@throws Throwable blocked exception or unexpected error void entry(Context context, ResourceWrapper resourceWrapper, T param, int count, boolean prioritized, Object... args) throws Throwable; //Means finish of #entry(Context, ResourceWrapper, Object, int, boolean, Object...). //@param context current Context //@param resourceWrapper current resource //@param obj relevant object (e.g. Node) //@param count tokens needed //@param prioritized whether the entry is prioritized //@param args parameters of the original call //@throws Throwable blocked exception or unexpected error void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable; //Exit of this slot. //@param context current Context //@param resourceWrapper current resource //@param count tokens needed //@param args parameters of the original call void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args); //Means finish of #exit(Context, ResourceWrapper, int, Object...). //@param context current Context //@param resourceWrapper current resource //@param count tokens needed //@param args parameters of the original call void fireExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args); }
二.责任链接口的抽象实现类
public abstract class AbstractLinkedProcessorSlot<T> implements ProcessorSlot<T> { //下一个节点,这里的责任链是一个单向链表,因此next就是当前节点所指向的下一个节点 private AbstractLinkedProcessorSlot<?> next = null; //触发执行责任链下一个节点的entry()方法 @Override public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable { if (next != null) { next.transformEntry(context, resourceWrapper, obj, count, prioritized, args); } } @SuppressWarnings("unchecked") void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, boolean prioritized, Object... args) throws Throwable { T t = (T)o; entry(context, resourceWrapper, t, count, prioritized, args); } //触发执行责任链下一个节点的exit()方法 @Override public void fireExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) { if (next != null) { next.exit(context, resourceWrapper, count, args); } } public AbstractLinkedProcessorSlot<?> getNext() { returnnext; } public void setNext(AbstractLinkedProcessorSlot<?> next) { this.next = next; } }
publicclassSphU { private static final Object[] OBJECTS0 = new Object[0]; private SphU() { } //Record statistics and perform rule checking for the given resource. //@param name the unique name of the protected resource //@return the Entryof this invocation (used for mark the invocation complete and get context data) public static Entry entry(Stringname) throws BlockException { //调用CtSph.entry()方法创建一个Entry资源访问对象 returnEnv.sph.entry(name, EntryType.OUT, 1, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method public static Entry entry(Methodmethod) throws BlockException { returnEnv.sph.entry(method, EntryType.OUT, 1, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param batchCount the amount of calls within the invocation (e.g. batchCount=2 meansrequestfor 2 tokens) public static Entry entry(Methodmethod, intbatchCount) throws BlockException { returnEnv.sph.entry(method, EntryType.OUT, batchCount, OBJECTS0); } //Record statistics and perform rule checking for the given resource. //@param name the unique string for the resource //@param batchCount the amount of calls within the invocation (e.g. batchCount=2 meansrequestfor 2 tokens) public static Entry entry(Stringname, intbatchCount) throws BlockException { returnEnv.sph.entry(name, EntryType.OUT, batchCount, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param trafficType the traffic type (inbound, outboundorinternal). //This is used to mark whether it can be blocked when the system is unstable, only inbound traffic could be blocked by SystemRule. public static Entry entry(Methodmethod, EntryTypetrafficType) throws BlockException { returnEnv.sph.entry(method, trafficType, 1, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(Stringname, EntryTypetrafficType) throws BlockException { //调用CtSph.entry()方法创建一个Entry资源访问对象 returnEnv.sph.entry(name, trafficType, 1, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param trafficType the traffic type (inbound, outboundorinternal). //This is used to mark whether it can be blocked when the system is unstable, only inbound traffic could be blocked by SystemRule. //@param batchCount the amount of calls within the invocation (e.g. batchCount=2 meansrequestfor 2 tokens) public static Entry entry(Methodmethod, EntryTypetrafficType, intbatchCount) throws BlockException { returnEnv.sph.entry(method, trafficType, batchCount, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(Stringname, EntryTypetrafficType, intbatchCount) throws BlockException { returnEnv.sph.entry(name, trafficType, batchCount, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param trafficType the traffic type (inbound, outboundorinternal). //This is used to mark whether it can be blocked when the system is unstable, only inbound traffic could be blocked by SystemRule. //@param batchCount the amount of calls within the invocation (e.g. batchCount=2 meansrequestfor 2 tokens) //@param args args for parameter flow control or customized slots //@return the Entry of this invocation (usedformarktheinvocationcompleteandgetcontextdata) public static Entry entry(Methodmethod, EntryTypetrafficType, intbatchCount, Object... args) throws BlockException { returnEnv.sph.entry(method, trafficType, batchCount, args); } //Record statistics and perform rule checking for the given resource. public static Entry entry(Stringname, EntryTypetrafficType, intbatchCount, Object... args) throws BlockException { returnEnv.sph.entry(name, trafficType, batchCount, args); } //Record statistics and check all rules of the resource that indicates an async invocation. //@param name the unique name of the protected resource public static AsyncEntry asyncEntry(Stringname) throws BlockException { returnEnv.sph.asyncEntry(name, EntryType.OUT, 1, OBJECTS0); } //Record statistics and check all rules of the resource that indicates an async invocation. //@param name the unique name for the protected resource //@param trafficType the traffic type (inbound, outboundorinternal). //This is used to mark whether it can be blocked when the system is unstable, only inbound traffic could be blocked by SystemRule. //@return the Entry of this invocation (usedformarktheinvocationcompleteandgetcontextdata) public static AsyncEntry asyncEntry(Stringname, EntryTypetrafficType) throws BlockException { returnEnv.sph.asyncEntry(name, trafficType, 1, OBJECTS0); } public static AsyncEntry asyncEntry(Stringname, EntryTypetrafficType, intbatchCount, Object... args) throws BlockException { returnEnv.sph.asyncEntry(name, trafficType, batchCount, args); } //Record statistics and perform rule checking for the given resource. The entry is prioritized. public static Entry entryWithPriority(Stringname) throws BlockException { returnEnv.sph.entryWithPriority(name, EntryType.OUT, 1, true); } //Record statistics and perform rule checking for the given resource. The entry is prioritized. public static Entry entryWithPriority(Stringname, EntryTypetrafficType) throws BlockException { returnEnv.sph.entryWithPriority(name, trafficType, 1, true); } //Record statistics and perform rule checking for the given resource. public static Entry entry(Stringname, intresourceType, EntryTypetrafficType) throws BlockException { returnEnv.sph.entryWithType(name, resourceType, trafficType, 1, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(Stringname, intresourceType, EntryTypetrafficType, Object[] args) throws BlockException { returnEnv.sph.entryWithType(name, resourceType, trafficType, 1, args); } //Record statistics and perform rule checking for the given resource that indicates an async invocation. public static AsyncEntry asyncEntry(Stringname, intresourceType, EntryTypetrafficType) throws BlockException { returnEnv.sph.asyncEntryWithType(name, resourceType, trafficType, 1, false, OBJECTS0); } //Record statistics and perform rule checking for the given resource that indicates an async invocation. public static AsyncEntry asyncEntry(Stringname, intresourceType, EntryTypetrafficType, Object[] args) throws BlockException { returnEnv.sph.asyncEntryWithType(name, resourceType, trafficType, 1, false, args); } //Record statistics and perform rule checking for the given resource that indicates an async invocation. public static AsyncEntry asyncEntry(Stringname, intresourceType, EntryTypetrafficType, intbatchCount, Object[] args) throws BlockException { returnEnv.sph.asyncEntryWithType(name, resourceType, trafficType, batchCount, false, args); } }