Search in sources :

Example 1 with BadHttpResponseException

use of com.hotels.styx.client.BadHttpResponseException in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagator method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    FlowControllingHttpContentProducer producer = getContentProducer(ctx);
    if (msg instanceof io.netty.handler.codec.http.HttpResponse) {
        io.netty.handler.codec.http.HttpResponse nettyResponse = (io.netty.handler.codec.http.HttpResponse) msg;
        if (!responseReceived.compareAndSet(false, true)) {
            LOGGER.warn("Unexpected additional response received: " + nettyResponse);
            ctx.channel().close();
            return;
        }
        if (nettyResponse.getDecoderResult().isFailure()) {
            emitResponseError(new BadHttpResponseException(origin, nettyResponse.getDecoderResult().cause()));
            return;
        }
        ctx.channel().config().setAutoRead(false);
        ctx.channel().read();
        // Can be started with flow controlling disabled
        EventLoop eventLoop = ctx.channel().eventLoop();
        Publisher<Buffer> contentPublisher = new ContentPublisher(eventLoop, producer);
        if ("close".equalsIgnoreCase(nettyResponse.headers().get(CONNECTION))) {
            toBeClosed = true;
        }
        LiveHttpResponse response = toStyxResponse(nettyResponse, contentPublisher, origin);
        this.sink.next(response);
    }
    if (msg instanceof HttpContent) {
        ByteBuf content = ((ByteBufHolder) msg).content();
        if (content.isReadable()) {
            producer.newChunk(retain(content));
        }
        if (msg instanceof LastHttpContent) {
            // Note: Netty may send a LastHttpContent as a response to TCP connection close.
            // In this case channelReadComplete event will _not_ follow the LastHttpContent.
            producer.lastHttpContent();
            if (toBeClosed) {
                ctx.channel().close();
            }
        }
    }
}
Also used : Buffer(com.hotels.styx.api.Buffer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) EventLoop(io.netty.channel.EventLoop) ByteBufHolder(io.netty.buffer.ByteBufHolder) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 2 with BadHttpResponseException

use of com.hotels.styx.client.BadHttpResponseException in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagatorTest method doesNotPropagateErrorsTwice.

@Test
public void doesNotPropagateErrorsTwice() throws Exception {
    NettyToStyxResponsePropagator handler = new NettyToStyxResponsePropagator(responseSubscriber, SOME_ORIGIN);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.writeInbound(httpResponseHeaders);
    LiveHttpResponse response = verifyNextCalledOnResponseSubscriber();
    StepVerifier.create(response.body()).then(// Execute onSubscribe in FSM
    channel::runPendingTasks).then(// Will emit BadHttpResponseException
    () -> channel.pipeline().fireExceptionCaught(new RuntimeException())).then(// Will emit TransportLostException
    () -> channel.pipeline().fireChannelInactive()).expectError(BadHttpResponseException.class).verify();
    verify(responseSubscriber, atMostOnce()).error(any());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with BadHttpResponseException

use of com.hotels.styx.client.BadHttpResponseException in project styx by ExpediaGroup.

the class HttpErrorStatusMetricsTest method backendFaultArgs.

private static Stream<Arguments> backendFaultArgs() {
    Id appId = Id.id("fakeApp");
    Id originId = Id.id("fakeApp1");
    Origin origin = Origin.newOriginBuilder("fakeHost", 9999).applicationId(appId).id(originId.toString()).build();
    Exception cause = new Exception();
    return Stream.of(Arguments.of(new NoAvailableHostsException(appId), "noHostsLiveForApplication"), Arguments.of(new OriginUnreachableException(origin, cause), "cannotConnect"), Arguments.of(new BadHttpResponseException(origin, cause), "badHttpResponse"), Arguments.of(new MaxPendingConnectionTimeoutException(origin, 1), "connectionsHeldTooLong"), Arguments.of(new MaxPendingConnectionsExceededException(origin, 1, 1), "tooManyConnections"), Arguments.of(new ResponseTimeoutException(origin, "test", 1, 1, 1, 1), "responseTooSlow"));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) ResponseTimeoutException(com.hotels.styx.api.exceptions.ResponseTimeoutException) NoAvailableHostsException(com.hotels.styx.api.exceptions.NoAvailableHostsException) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) MaxPendingConnectionTimeoutException(com.hotels.styx.client.connectionpool.MaxPendingConnectionTimeoutException) MaxPendingConnectionsExceededException(com.hotels.styx.client.connectionpool.MaxPendingConnectionsExceededException) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) Id(com.hotels.styx.api.Id) MaxPendingConnectionTimeoutException(com.hotels.styx.client.connectionpool.MaxPendingConnectionTimeoutException) NoAvailableHostsException(com.hotels.styx.api.exceptions.NoAvailableHostsException) PluginException(com.hotels.styx.api.plugins.spi.PluginException) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) ResponseTimeoutException(com.hotels.styx.api.exceptions.ResponseTimeoutException) MaxPendingConnectionsExceededException(com.hotels.styx.client.connectionpool.MaxPendingConnectionsExceededException) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException)

Aggregations

BadHttpResponseException (com.hotels.styx.client.BadHttpResponseException)3 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)2 Buffer (com.hotels.styx.api.Buffer)1 Id (com.hotels.styx.api.Id)1 NoAvailableHostsException (com.hotels.styx.api.exceptions.NoAvailableHostsException)1 OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)1 ResponseTimeoutException (com.hotels.styx.api.exceptions.ResponseTimeoutException)1 Origin (com.hotels.styx.api.extension.Origin)1 PluginException (com.hotels.styx.api.plugins.spi.PluginException)1 MaxPendingConnectionTimeoutException (com.hotels.styx.client.connectionpool.MaxPendingConnectionTimeoutException)1 MaxPendingConnectionsExceededException (com.hotels.styx.client.connectionpool.MaxPendingConnectionsExceededException)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufHolder (io.netty.buffer.ByteBufHolder)1 EventLoop (io.netty.channel.EventLoop)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 Test (org.junit.jupiter.api.Test)1