use of com.alibaba.csp.sentinel.EntryType in project Sentinel by alibaba.
the class ReactorSphU method entryWith.
public static <R> Mono<R> entryWith(String resourceName, EntryType entryType, Mono<R> actual) {
final AtomicReference<AsyncEntry> entryWrapper = new AtomicReference<>(null);
return Mono.defer(() -> {
try {
AsyncEntry entry = SphU.asyncEntry(resourceName, entryType);
entryWrapper.set(entry);
return actual.subscriberContext(context -> {
if (entry == null) {
return context;
}
Context sentinelContext = entry.getAsyncContext();
if (sentinelContext == null) {
return context;
}
// TODO: check GC friendly?
return context.put(SentinelReactorConstants.SENTINEL_CONTEXT_KEY, sentinelContext);
}).doOnSuccessOrError((o, t) -> {
if (entry != null && entryWrapper.compareAndSet(entry, null)) {
if (t != null) {
Tracer.traceContext(t, 1, entry.getAsyncContext());
}
entry.exit();
}
});
} catch (BlockException ex) {
return Mono.error(ex);
}
});
}
use of com.alibaba.csp.sentinel.EntryType in project Sentinel by alibaba.
the class SentinelResourceAspect method invokeResourceWithSentinel.
@Around("sentinelResourceAnnotationPointcut()")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {
Method originMethod = resolveMethod(pjp);
SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
if (annotation == null) {
// Should not go through here.
throw new IllegalStateException("Wrong state for SentinelResource annotation");
}
String resourceName = getResourceName(annotation.value(), originMethod);
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
return pjp.proceed();
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
return handleFallback(pjp, annotation, ex);
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, pjp.getArgs());
}
}
}
use of com.alibaba.csp.sentinel.EntryType in project Sentinel by alibaba.
the class SentinelResourceInterceptor method aroundInvoke.
@AroundInvoke
Object aroundInvoke(InvocationContext ctx) throws Throwable {
SentinelResourceBinding annotation = ctx.getMethod().getAnnotation(SentinelResourceBinding.class);
if (annotation == null) {
// Should not go through here.
throw new IllegalStateException("Wrong state for SentinelResource annotation");
}
String resourceName = getResourceName(annotation.value(), ctx.getMethod());
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, ctx.getParameters());
Object result = ctx.proceed();
return result;
} catch (BlockException ex) {
return handleBlockException(ctx, annotation, ex);
} catch (Throwable ex) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
return handleFallback(ctx, annotation, ex);
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, ctx.getParameters());
}
}
}
use of com.alibaba.csp.sentinel.EntryType in project spring-cloud-alibaba by alibaba.
the class SentinelConfigBuilder method build.
@Override
public SentinelCircuitBreakerConfiguration build() {
Assert.hasText(resourceName, "resourceName cannot be empty");
List<DegradeRule> rules = Optional.ofNullable(this.rules).orElse(new ArrayList<>());
EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT);
return new SentinelCircuitBreakerConfiguration().setResourceName(this.resourceName).setEntryType(entryType).setRules(rules);
}
use of com.alibaba.csp.sentinel.EntryType in project jboot by yangfuhai.
the class SentinelInterceptor method intercept.
@Override
public void intercept(Invocation inv) {
SentinelResource annotation = inv.getMethod().getAnnotation(SentinelResource.class);
if (annotation == null) {
inv.invoke();
return;
}
String resourceName = getResourceName(annotation.value(), inv.getMethod());
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, inv.getArgs());
inv.invoke();
} catch (BlockException ex) {
try {
inv.setReturnValue(handleBlockException(inv, annotation, ex));
} catch (Throwable throwable) {
if (inv.isActionInvocation()) {
inv.getController().renderText("Blocked by Sentinel " + ex.getRule());
} else {
throwable.printStackTrace();
}
}
return;
} catch (Throwable ex) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
try {
inv.setReturnValue(handleFallback(inv, annotation, ex));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return;
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, inv.getArgs());
}
}
}
Aggregations