//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() { return next; } public void setNext(AbstractLinkedProcessorSlot<?> next) { this.next = next; } }
public class SphU { 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 Entry of this invocation (used for mark the invocation complete and get context data) public static Entry entry(String name) throws BlockException { //调用CtSph.entry()方法创建一个Entry资源访问对象 return Env.sph.entry(name, EntryType.OUT, 1, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method public static Entry entry(Method method) throws BlockException { return Env.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 means request for 2 tokens) public static Entry entry(Method method, int batchCount) throws BlockException { return Env.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 means request for 2 tokens) public static Entry entry(String name, int batchCount) throws BlockException { return Env.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, outbound or internal). //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(Method method, EntryType trafficType) throws BlockException { return Env.sph.entry(method, trafficType, 1, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(String name, EntryType trafficType) throws BlockException { //调用CtSph.entry()方法创建一个Entry资源访问对象 return Env.sph.entry(name, trafficType, 1, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param trafficType the traffic type (inbound, outbound or internal). //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 means request for 2 tokens) public static Entry entry(Method method, EntryType trafficType, int batchCount) throws BlockException { return Env.sph.entry(method, trafficType, batchCount, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(String name, EntryType trafficType, int batchCount) throws BlockException { return Env.sph.entry(name, trafficType, batchCount, OBJECTS0); } //Checking all Rules about the protected method. //@param method the protected method //@param trafficType the traffic type (inbound, outbound or internal). //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 means request for 2 tokens) //@param args args for parameter flow control or customized slots //@return the Entry of this invocation (used for mark the invocation complete and get context data) public static Entry entry(Method method, EntryType trafficType, int batchCount, Object... args) throws BlockException { return Env.sph.entry(method, trafficType, batchCount, args); } //Record statistics and perform rule checking for the given resource. public static Entry entry(String name, EntryType trafficType, int batchCount, Object... args) throws BlockException { return Env.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(String name) throws BlockException { return Env.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, outbound or internal). //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 (used for mark the invocation complete and get context data) public static AsyncEntry asyncEntry(String name, EntryType trafficType) throws BlockException { return Env.sph.asyncEntry(name, trafficType, 1, OBJECTS0); } public static AsyncEntry asyncEntry(String name, EntryType trafficType, int batchCount, Object... args) throws BlockException { return Env.sph.asyncEntry(name, trafficType, batchCount, args); } //Record statistics and perform rule checking for the given resource. The entry is prioritized. public static Entry entryWithPriority(String name) throws BlockException { return Env.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(String name, EntryType trafficType) throws BlockException { return Env.sph.entryWithPriority(name, trafficType, 1, true); } //Record statistics and perform rule checking for the given resource. public static Entry entry(String name, int resourceType, EntryType trafficType) throws BlockException { return Env.sph.entryWithType(name, resourceType, trafficType, 1, OBJECTS0); } //Record statistics and perform rule checking for the given resource. public static Entry entry(String name, int resourceType, EntryType trafficType, Object[] args) throws BlockException { return Env.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(String name, int resourceType, EntryType trafficType) throws BlockException { return Env.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(String name, int resourceType, EntryType trafficType, Object[] args) throws BlockException { return Env.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(String name, int resourceType, EntryType trafficType, int batchCount, Object[] args) throws BlockException { return Env.sph.asyncEntryWithType(name, resourceType, trafficType, batchCount, false, args); } }