use of com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.EntryHolder in project Sentinel by alibaba.
the class SentinelZuulInboundFilter method doSentinelEntry.
private void doSentinelEntry(String resourceName, final int resType, HttpRequestMessage input, Deque<EntryHolder> holders) throws BlockException {
Object[] params = paramParser.parseParameterFor(resourceName, input, r -> r.getResourceMode() == resType);
AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, params);
holders.push(new EntryHolder(entry, params));
}
use of com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.EntryHolder in project Sentinel by alibaba.
the class SentinelZuulInboundFilter method apply.
private Observable<HttpRequestMessage> apply(HttpRequestMessage request) {
SessionContext context = request.getContext();
Deque<EntryHolder> holders = new ArrayDeque<>();
String routeId = routeExtractor.apply(request);
String fallBackRoute = routeId;
try {
if (StringUtil.isNotBlank(routeId)) {
ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId);
doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, request, holders);
}
Set<String> matchingApis = pickMatchingApiDefinitions(request);
if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) {
ContextUtil.enter(SentinelZuul2Constants.ZUUL_DEFAULT_CONTEXT);
}
for (String apiName : matchingApis) {
fallBackRoute = apiName;
doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, request, holders);
}
return Observable.just(request);
} catch (BlockException t) {
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_BLOCKED_FLAG, Boolean.TRUE);
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_FALLBACK_ROUTE, fallBackRoute);
if (fastError) {
context.setShouldSendErrorResponse(true);
context.setErrorEndpoint(blockedEndpointName);
} else {
context.setEndpoint(blockedEndpointName);
}
return Observable.error(t);
} finally {
if (!holders.isEmpty()) {
context.put(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY, holders);
}
// clear context to avoid another request use incorrect context
ContextUtil.exit();
}
}
use of com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.EntryHolder in project Sentinel by alibaba.
the class SentinelZuulOutboundFilter method apply.
public HttpResponseMessage apply(HttpResponseMessage response) {
SessionContext context = response.getContext();
if (context.get(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY) == null) {
return response;
}
boolean previousBlocked = context.getFilterErrors().stream().anyMatch(e -> BlockException.isBlockException(e.getException()));
Deque<EntryHolder> holders = (Deque<EntryHolder>) context.get(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_ENTRIES_KEY);
while (!holders.isEmpty()) {
EntryHolder holder = holders.pop();
if (!previousBlocked) {
Tracer.traceEntry(context.getError(), holder.getEntry());
holder.getEntry().exit(1, holder.getParams());
}
}
return response;
}
Aggregations