diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java
index e8750710..fcd8df6d 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java
@@ -61,7 +61,8 @@ public class CtSph implements Sph {
return entry;
}
- private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
+ private AsyncEntry asyncEntryWithPriorityInternal(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,
@@ -87,7 +88,7 @@ public class CtSph implements Sph {
AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context);
try {
- chain.entry(context, resourceWrapper, null, count, args);
+ chain.entry(context, resourceWrapper, null, count, prioritized, args);
// Initiate the async context only when the entry successfully passed the slot chain.
asyncEntry.initAsyncContext();
// The asynchronous call may take time in background, and current context should not be hanged on it.
@@ -108,23 +109,12 @@ public class CtSph implements Sph {
return asyncEntry;
}
- /**
- * Do all {@link Rule}s checking about the resource.
- *
- *
Each distinct resource will use a {@link ProcessorSlot} to do rules checking. Same resource will use
- * same {@link ProcessorSlot} globally.
- *
- *
Note that total {@link ProcessorSlot} count must not exceed {@link Constants#MAX_SLOT_CHAIN_SIZE},
- * otherwise no rules checking will do. In this condition, all requests will pass directly, with no checking
- * or exception.
- *
- * @param resourceWrapper resource name
- * @param count tokens needed
- * @param args arguments of user method call
- * @return {@link Entry} represents this call
- * @throws BlockException if any rule's threshold is exceeded
- */
- public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
+ private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
+ return asyncEntryWithPriorityInternal(resourceWrapper, count, false, args);
+ }
+
+ 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,
@@ -154,7 +144,7 @@ public class CtSph implements Sph {
Entry e = new CtEntry(resourceWrapper, chain, context);
try {
- chain.entry(context, resourceWrapper, null, count, args);
+ chain.entry(context, resourceWrapper, null, count, prioritized, args);
} catch (BlockException e1) {
e.exit(count, args);
throw e1;
@@ -165,6 +155,26 @@ public class CtSph implements Sph {
return e;
}
+ /**
+ * Do all {@link Rule}s checking about the resource.
+ *
+ *
Each distinct resource will use a {@link ProcessorSlot} to do rules checking. Same resource will use
+ * same {@link ProcessorSlot} globally.
+ *
+ *
Note that total {@link ProcessorSlot} count must not exceed {@link Constants#MAX_SLOT_CHAIN_SIZE},
+ * otherwise no rules checking will do. In this condition, all requests will pass directly, with no checking
+ * or exception.
+ *
+ * @param resourceWrapper resource name
+ * @param count tokens needed
+ * @param args arguments of user method call
+ * @return {@link Entry} represents this call
+ * @throws BlockException if any rule's threshold is exceeded
+ */
+ public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
+ return entryWithPriority(resourceWrapper, count, false, args);
+ }
+
/**
* Get {@link ProcessorSlotChain} of the resource. new {@link ProcessorSlotChain} will
* be created if the resource doesn't relate one.
@@ -305,4 +315,10 @@ public class CtSph implements Sph {
StringResourceWrapper resource = new StringResourceWrapper(name, type);
return asyncEntryInternal(resource, count, args);
}
+
+ @Override
+ public Entry entryWithPriority(String name, EntryType type, int count, boolean prioritized) throws BlockException {
+ StringResourceWrapper resource = new StringResourceWrapper(name, type);
+ return entryWithPriority(resource, count, prioritized);
+ }
}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java
index 8564db56..7e1f2a90 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java
@@ -155,4 +155,18 @@ public interface Sph {
* @since 0.2.0
*/
AsyncEntry asyncEntry(String name, EntryType type, int count, Object... args) throws BlockException;
+
+ /**
+ * Create a protected resource with priority.
+ *
+ * @param name the unique name for the protected resource
+ * @param type the resource is an inbound or an outbound method. This is used
+ * to mark whether it can be blocked when the system is unstable
+ * @param count the count that the resource requires
+ * @param prioritized whether the entry is prioritized
+ * @return entry get
+ * @throws BlockException if the block criteria is met
+ * @since 1.4.0
+ */
+ Entry entryWithPriority(String name, EntryType type, int count, boolean prioritized) throws BlockException;
}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java
index ee3f769d..696b686f 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java
@@ -242,4 +242,29 @@ public class SphU {
public static AsyncEntry asyncEntry(String name, EntryType type, int count, Object... args) throws BlockException {
return Env.sph.asyncEntry(name, type, count, args);
}
+
+ /**
+ * Checking all {@link Rule}s related the resource. The entry is prioritized.
+ *
+ * @param name the unique name for the protected resource
+ * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded.
+ * @since 1.4.0
+ */
+ public static Entry entryWithPriority(String name) throws BlockException {
+ return Env.sph.entryWithPriority(name, EntryType.OUT, 1, true);
+ }
+
+ /**
+ * Checking all {@link Rule}s related the resource. The entry is prioritized.
+ *
+ * @param name the unique name for the protected resource
+ * @param type the resource is an inbound or an outbound method. This is used
+ * to mark whether it can be blocked when the system is unstable,
+ * only inbound traffic could be blocked by {@link SystemRule}
+ * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded.
+ * @since 1.4.0
+ */
+ public static Entry entryWithPriority(String name, EntryType type) throws BlockException {
+ return Env.sph.entryWithPriority(name, type, 1, true);
+ }
}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java
index 3d10186f..2da213a2 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java
@@ -26,18 +26,18 @@ public abstract class AbstractLinkedProcessorSlot implements ProcessorSlot
private AbstractLinkedProcessorSlot> next = null;
@Override
- public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args)
+ 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, args);
+ next.transformEntry(context, resourceWrapper, obj, count, prioritized, args);
}
}
@SuppressWarnings("unchecked")
- void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, Object... args)
+ 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, args);
+ entry(context, resourceWrapper, t, count, prioritized, args);
}
@Override
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java
index b48301e2..5906d179 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java
@@ -26,9 +26,9 @@ public class DefaultProcessorSlotChain extends ProcessorSlotChain {
AbstractLinkedProcessorSlot> first = new AbstractLinkedProcessorSlot