Search in sources :

Example 1 with EntryType

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);
        }
    });
}
Also used : SphU(com.alibaba.csp.sentinel.SphU) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) Context(com.alibaba.csp.sentinel.context.Context) EntryType(com.alibaba.csp.sentinel.EntryType) Mono(reactor.core.publisher.Mono) Tracer(com.alibaba.csp.sentinel.Tracer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with EntryType

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());
        }
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Example 3 with EntryType

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());
        }
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) AroundInvoke(javax.interceptor.AroundInvoke)

Example 4 with EntryType

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);
}
Also used : EntryType(com.alibaba.csp.sentinel.EntryType) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 5 with EntryType

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());
        }
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) SentinelResource(com.alibaba.csp.sentinel.annotation.SentinelResource)

Aggregations

EntryType (com.alibaba.csp.sentinel.EntryType)6 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)5 Entry (com.alibaba.csp.sentinel.Entry)4 SentinelResource (com.alibaba.csp.sentinel.annotation.SentinelResource)2 Method (java.lang.reflect.Method)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 Around (org.aspectj.lang.annotation.Around)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 SphU (com.alibaba.csp.sentinel.SphU)1 Tracer (com.alibaba.csp.sentinel.Tracer)1 RequestOriginParser (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser)1 Context (com.alibaba.csp.sentinel.context.Context)1 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)1 CircuitBreakerMapping (com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AroundInvoke (javax.interceptor.AroundInvoke)1 Mono (reactor.core.publisher.Mono)1