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();
}
}
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();
}
}
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();
}
}
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);
}
});
}
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);
}
Aggregations