Search in sources :

Example 1 with ExecutionStatus

use of com.netflix.zuul.ExecutionStatus in project zuul by Netflix.

the class BaseZuulFilterRunner method filter.

protected final O filter(final ZuulFilter<I, O> filter, final I inMesg) {
    final long startTime = System.nanoTime();
    final ZuulMessage snapshot = inMesg.getContext().debugRouting() ? inMesg.clone() : null;
    FilterChainResumer resumer = null;
    try (TaskCloseable ignored = traceTask(filter, f -> f.filterName() + ".filter")) {
        addPerfMarkTags(inMesg);
        ExecutionStatus filterRunStatus = null;
        if (filter.filterType() == INBOUND && inMesg.getContext().shouldSendErrorResponse()) {
            // Pass request down the pipeline, all the way to error endpoint if error response needs to be generated
            filterRunStatus = SKIPPED;
        }
        ;
        try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".shouldSkipFilter")) {
            if (shouldSkipFilter(inMesg, filter)) {
                filterRunStatus = SKIPPED;
            }
        }
        if (filter.isDisabled()) {
            filterRunStatus = DISABLED;
        }
        if (filterRunStatus != null) {
            recordFilterCompletion(filterRunStatus, filter, startTime, inMesg, snapshot);
            return filter.getDefaultOutput(inMesg);
        }
        if (!isMessageBodyReadyForFilter(filter, inMesg)) {
            setFilterAwaitingBody(inMesg, true);
            logger.debug("Filter {} waiting for body, UUID {}", filter.filterName(), inMesg.getContext().getUUID());
            // wait for whole body to be buffered
            return null;
        }
        setFilterAwaitingBody(inMesg, false);
        if (snapshot != null) {
            Debug.addRoutingDebug(inMesg.getContext(), "Filter " + filter.filterType().toString() + " " + filter.filterOrder() + " " + filter.filterName());
        }
        // run body contents accumulated so far through this filter
        inMesg.runBufferedBodyContentThroughFilter(filter);
        if (filter.getSyncType() == FilterSyncType.SYNC) {
            final SyncZuulFilter<I, O> syncFilter = (SyncZuulFilter) filter;
            final O outMesg;
            try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".apply")) {
                addPerfMarkTags(inMesg);
                outMesg = syncFilter.apply(inMesg);
            }
            recordFilterCompletion(SUCCESS, filter, startTime, inMesg, snapshot);
            return (outMesg != null) ? outMesg : filter.getDefaultOutput(inMesg);
        }
        // async filter
        try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".applyAsync")) {
            final Link nettyToSchedulerLink = linkOut();
            filter.incrementConcurrency();
            resumer = new FilterChainResumer(inMesg, filter, snapshot, startTime);
            filter.applyAsync(inMesg).doOnSubscribe(() -> {
                try (TaskCloseable ignored3 = traceTask(filter, f -> f.filterName() + ".onSubscribeAsync")) {
                    linkIn(nettyToSchedulerLink);
                }
            }).doOnNext(resumer.onNextStarted(nettyToSchedulerLink)).doOnError(resumer.onErrorStarted(nettyToSchedulerLink)).doOnCompleted(resumer.onCompletedStarted(nettyToSchedulerLink)).observeOn(Schedulers.from(getChannelHandlerContext(inMesg).executor())).doOnUnsubscribe(resumer::decrementConcurrency).subscribe(resumer);
        }
        // wait for the async filter to finish
        return null;
    } catch (Throwable t) {
        if (resumer != null) {
            resumer.decrementConcurrency();
        }
        final O outMesg = handleFilterException(inMesg, filter, t);
        outMesg.finishBufferedBodyIfIncomplete();
        recordFilterCompletion(FAILED, filter, startTime, inMesg, snapshot);
        return outMesg;
    }
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) ExecutionStatus(com.netflix.zuul.ExecutionStatus) SyncZuulFilter(com.netflix.zuul.filters.SyncZuulFilter) Link(io.perfmark.Link) TaskCloseable(io.perfmark.TaskCloseable)

Aggregations

ExecutionStatus (com.netflix.zuul.ExecutionStatus)1 SyncZuulFilter (com.netflix.zuul.filters.SyncZuulFilter)1 ZuulMessage (com.netflix.zuul.message.ZuulMessage)1 Link (io.perfmark.Link)1 TaskCloseable (io.perfmark.TaskCloseable)1