Search in sources :

Example 11 with ProxyRouterProcessingState

use of com.nike.riposte.server.http.ProxyRouterProcessingState in project riposte by Nike-Inc.

the class ExceptionHandlingHandlerTest method beforeMethod.

@Before
public void beforeMethod() {
    riposteErrorHandlerMock = mock(RiposteErrorHandler.class);
    riposteUnhandledErrorHandlerMock = mock(RiposteUnhandledErrorHandler.class);
    distributedTracingConfigMock = mock(DistributedTracingConfig.class);
    serverSpanNamingAndTaggingStrategyMock = mock(ServerSpanNamingAndTaggingStrategy.class);
    doReturn(serverSpanNamingAndTaggingStrategyMock).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
    handler = new ExceptionHandlingHandler(riposteErrorHandlerMock, riposteUnhandledErrorHandlerMock, distributedTracingConfigMock);
    channelMock = mock(Channel.class);
    ctxMock = mock(ChannelHandlerContext.class);
    stateAttributeMock = mock(Attribute.class);
    proxyRouterStateAttributeMock = mock(Attribute.class);
    state = new HttpProcessingState();
    proxyRouterStateSpy = spy(new ProxyRouterProcessingState());
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(state).when(stateAttributeMock).get();
    doReturn(proxyRouterStateAttributeMock).when(channelMock).attr(ChannelAttributes.PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(proxyRouterStateSpy).when(proxyRouterStateAttributeMock).get();
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) DistributedTracingConfig(com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig) Attribute(io.netty.util.Attribute) Channel(io.netty.channel.Channel) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ServerSpanNamingAndTaggingStrategy(com.nike.riposte.server.config.distributedtracing.ServerSpanNamingAndTaggingStrategy) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) Before(org.junit.Before)

Example 12 with ProxyRouterProcessingState

use of com.nike.riposte.server.http.ProxyRouterProcessingState in project riposte by Nike-Inc.

the class StreamingAsyncHttpClientTest method streamDownstreamCall_setsHostHeaderCorrectly.

@DataProvider(value = { "80   | false | localhost | localhost", "80   | true  | localhost | localhost:80", "8080 | false | localhost | localhost:8080", "443  | true  | localhost | localhost", "443  | false | localhost | localhost:443", "8080 | true  | localhost | localhost:8080" }, splitBy = "\\|")
@Test
public void streamDownstreamCall_setsHostHeaderCorrectly(int downstreamPort, boolean isSecure, String downstreamHost, String expectedHostHeader) {
    // given
    DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
    ChannelHandlerContext ctx = mockChannelHandlerContext();
    StreamingCallback streamingCallback = mock(StreamingCallback.class);
    ProxyRouterProcessingState proxyState = ChannelAttributes.getProxyRouterProcessingStateForChannel(ctx).get();
    RequestInfo<?> requestInfoMock = mock(RequestInfo.class);
    // when
    new StreamingAsyncHttpClient(200, 200, true, mock(DistributedTracingConfig.class)).streamDownstreamCall(downstreamHost, downstreamPort, request, isSecure, false, streamingCallback, 200, true, true, proxyState, requestInfoMock, ctx);
    // then
    assertThat(request.headers().get(HOST)).isEqualTo(expectedHostHeader);
}
Also used : StreamingCallback(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingCallback) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 13 with ProxyRouterProcessingState

use of com.nike.riposte.server.http.ProxyRouterProcessingState in project riposte by Nike-Inc.

the class ExceptionHandlingHandler method doExceptionCaught.

@Override
public PipelineContinuationBehavior doExceptionCaught(ChannelHandlerContext ctx, @NotNull Throwable cause) {
    // We expect to end up here when handlers previously in the pipeline throw an error, so do the normal
    // processError call.
    HttpProcessingState state = getStateAndCreateIfNeeded(ctx, cause);
    // Ensure that a RequestInfo is set on the state, no matter what.
    getRequestInfo(state, null);
    if (state.isResponseSendingStarted()) {
        String infoMessage = "A response has already been started. Ignoring this exception since it's secondary. NOTE: This often " + "occurs when an error happens repeatedly on multiple chunks of a request or response - only the " + "first one is processed into the error sent to the user. The original error is probably higher up in " + "the logs. ignored_secondary_exception=\"{}\"";
        if (cause instanceof NullPointerException)
            logger.info(infoMessage, cause.toString(), cause);
        else
            logger.info(infoMessage, cause.toString());
        return PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT;
    } else {
        ResponseInfo<ErrorResponseBody> responseInfo = processError(state, null, cause);
        if (shouldForceConnectionCloseAfterResponseSent(cause))
            responseInfo.setForceConnectionCloseAfterResponseSent(true);
        state.setResponseInfo(responseInfo, cause);
        // We're about to send a full error response back to the original caller, so any proxy/router streaming is
        // invalid. Cancel request and response streaming for proxy/router endpoints.
        Endpoint<?> endpoint = state.getEndpointForExecution();
        if (endpoint instanceof ProxyRouterEndpoint) {
            ProxyRouterProcessingState proxyRouterState = getProxyRouterProcessingStateForChannel(ctx).get();
            if (proxyRouterState != null) {
                proxyRouterState.cancelRequestStreaming(cause, ctx);
                proxyRouterState.cancelDownstreamRequest(cause);
            }
        }
        addErrorAnnotationToOverallRequestSpan(state, responseInfo, cause);
    }
    return PipelineContinuationBehavior.CONTINUE;
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint)

Aggregations

ProxyRouterProcessingState (com.nike.riposte.server.http.ProxyRouterProcessingState)13 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 Span (com.nike.wingtips.Span)5 DistributedTracingConfig (com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig)4 Channel (io.netty.channel.Channel)4 WrapperException (com.nike.backstopper.exception.WrapperException)3 Pair (com.nike.internal.util.Pair)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3 AsyncNettyHelper.runnableWithTracingAndMdc (com.nike.riposte.util.AsyncNettyHelper.runnableWithTracingAndMdc)3 ChannelFuture (io.netty.channel.ChannelFuture)3 Attribute (io.netty.util.Attribute)3 StringUtils (com.nike.internal.util.StringUtils)2 StreamingChannel (com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingChannel)2 DownstreamIdleChannelTimeoutHandler (com.nike.riposte.client.asynchttp.netty.downstreampipeline.DownstreamIdleChannelTimeoutHandler)2 ProxyRouterSpanNamingAndTaggingStrategy (com.nike.riposte.server.config.distributedtracing.ProxyRouterSpanNamingAndTaggingStrategy)2 DownstreamChannelClosedUnexpectedlyException (com.nike.riposte.server.error.exception.DownstreamChannelClosedUnexpectedlyException)2 DownstreamIdleChannelTimeoutException (com.nike.riposte.server.error.exception.DownstreamIdleChannelTimeoutException)2 HostnameResolutionException (com.nike.riposte.server.error.exception.HostnameResolutionException)2 NativeIoExceptionWrapper (com.nike.riposte.server.error.exception.NativeIoExceptionWrapper)2