Search in sources :

Example 1 with AsyncEntry

use of com.alibaba.csp.sentinel.AsyncEntry in project Sentinel by alibaba.

the class AsyncEntryDemo method doAsyncThenSync.

private void doAsyncThenSync() {
    try {
        // First we call an asynchronous resource.
        final AsyncEntry entry = SphU.asyncEntry("test-async");
        this.invoke("abc", resp -> {
            // The thread is different from original caller thread for async entry.
            // So we need to wrap in the async context so that nested invocation entry
            // can be linked to the parent asynchronous entry.
            ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
                try {
                    // In the callback, we do another async invocation several times under the async context.
                    for (int i = 0; i < 7; i++) {
                        anotherAsync();
                    }
                    System.out.println(resp);
                    // Then we do a sync (normal) entry under current async context.
                    fetchSyncInAsync();
                } finally {
                    // Exit the async entry.
                    entry.exit();
                }
            });
        });
        // Then we call a sync resource.
        fetchSync();
    } catch (BlockException ex) {
        // Request blocked, handle the exception.
        ex.printStackTrace();
    }
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry)

Example 2 with AsyncEntry

use of com.alibaba.csp.sentinel.AsyncEntry in project Sentinel by alibaba.

the class AsyncEntryDemo method directlyAsync.

private void directlyAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-async-not-nested");
        this.invoke("abc", result -> {
            // If no nested entry later, we don't have to wrap in `ContextUtil.runOnContext()`.
            try {
            // Here to handle the async result (without other entry).
            } finally {
                // Exit the async entry.
                entry.exit();
            }
        });
    } catch (BlockException e) {
        // Request blocked, handle the exception.
        e.printStackTrace();
    }
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry)

Example 3 with AsyncEntry

use of com.alibaba.csp.sentinel.AsyncEntry in project Sentinel by alibaba.

the class AsyncEntryDemo method anotherAsync.

private void anotherAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-another-async");
        CompletableFuture.runAsync(() -> {
            ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    // Normal entry nested in asynchronous entry.
                    anotherSyncInAsync();
                    System.out.println("Async result: 666");
                } catch (InterruptedException e) {
                // Ignore.
                } finally {
                    entry.exit();
                }
            });
        });
    } catch (BlockException ex) {
        ex.printStackTrace();
    }
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry)

Example 4 with AsyncEntry

use of com.alibaba.csp.sentinel.AsyncEntry 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 5 with AsyncEntry

use of com.alibaba.csp.sentinel.AsyncEntry in project Sentinel by alibaba.

the class SentinelZuulPreFilter method doSentinelEntry.

private void doSentinelEntry(String resourceName, final int resType, RequestContext requestContext, Deque<EntryHolder> holders) throws BlockException {
    Object[] params = paramParser.parseParameterFor(resourceName, requestContext, new Predicate<GatewayFlowRule>() {

        @Override
        public boolean test(GatewayFlowRule r) {
            return r.getResourceMode() == resType;
        }
    });
    AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, params);
    EntryHolder holder = new EntryHolder(entry, params);
    holders.push(holder);
}
Also used : AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Aggregations

AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Entry (com.alibaba.csp.sentinel.Entry)1 EntryType (com.alibaba.csp.sentinel.EntryType)1 SphU (com.alibaba.csp.sentinel.SphU)1 Tracer (com.alibaba.csp.sentinel.Tracer)1 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)1 EntryHolder (com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.EntryHolder)1 Context (com.alibaba.csp.sentinel.context.Context)1 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 Mono (reactor.core.publisher.Mono)1