Search in sources :

Example 1 with TransportLostException

use of com.hotels.styx.api.exceptions.TransportLostException in project styx by ExpediaGroup.

the class HttpRequestOperation method execute.

public Flux<LiveHttpResponse> execute(NettyConnection nettyConnection) {
    AtomicReference<RequestBodyChunkSubscriber> requestRequestBodyChunkSubscriber = new AtomicReference<>();
    requestTime = System.currentTimeMillis();
    executeCount.incrementAndGet();
    Flux<LiveHttpResponse> responseFlux = Flux.create(sink -> {
        if (nettyConnection.isConnected()) {
            RequestBodyChunkSubscriber bodyChunkSubscriber = new RequestBodyChunkSubscriber(request, nettyConnection);
            requestRequestBodyChunkSubscriber.set(bodyChunkSubscriber);
            addProxyBridgeHandlers(nettyConnection, sink);
            new WriteRequestToOrigin(sink, nettyConnection, request, bodyChunkSubscriber).write();
            if (requestLoggingEnabled) {
                httpRequestMessageLogger.logRequest(request, nettyConnection.getOrigin());
            }
        } else {
            sink.error(new TransportLostException(nettyConnection.channel(), nettyConnection.getOrigin()));
        }
    });
    if (requestLoggingEnabled) {
        responseFlux = responseFlux.doOnNext(response -> {
            httpRequestMessageLogger.logResponse(request, response);
        });
    }
    return responseFlux.map(response -> Requests.doFinally(response, cause -> {
        if (nettyConnection.isConnected()) {
            removeProxyBridgeHandlers(nettyConnection);
            if (requestIsOngoing(requestRequestBodyChunkSubscriber.get())) {
                LOGGER.warn("Origin responded too quickly to an ongoing request, or it was cancelled. Connection={}, Request={}.", new Object[] { nettyConnection.channel(), this.request });
                nettyConnection.close();
            }
        }
    }));
}
Also used : HttpVersion(com.hotels.styx.api.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) HttpObject(io.netty.handler.codec.http.HttpObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Buffers(com.hotels.styx.api.Buffers) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Objects.requireNonNull(java.util.Objects.requireNonNull) Requests(com.hotels.styx.api.Requests) NoSuchElementException(java.util.NoSuchElementException) HttpMethod(com.hotels.styx.api.HttpMethod) HttpRequestMessageLogger(com.hotels.styx.common.logging.HttpRequestMessageLogger) Logger(org.slf4j.Logger) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ChannelPipeline(io.netty.channel.ChannelPipeline) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) EMPTY_LAST_CONTENT(io.netty.handler.codec.http.LastHttpContent.EMPTY_LAST_CONTENT) String.format(java.lang.String.format) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) ChannelFuture(io.netty.channel.ChannelFuture) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Objects(java.util.Objects) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Flux(reactor.core.publisher.Flux) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) HttpMessageFormatter(com.hotels.styx.common.format.HttpMessageFormatter) Origin(com.hotels.styx.api.extension.Origin) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) HOST(com.hotels.styx.api.HttpHeaderNames.HOST) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpObject(io.netty.handler.codec.http.HttpObject) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse)

Example 2 with TransportLostException

use of com.hotels.styx.api.exceptions.TransportLostException in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagator method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) {
    TransportLostException cause = new TransportLostException(ctx.channel().remoteAddress(), origin);
    getContentProducer(ctx).channelInactive(cause);
}
Also used : TransportLostException(com.hotels.styx.api.exceptions.TransportLostException)

Example 3 with TransportLostException

use of com.hotels.styx.api.exceptions.TransportLostException in project styx by ExpediaGroup.

the class HttpResponseWriterTest method logsSentAndAcknowledgedBytes.

@Test
public void logsSentAndAcknowledgedBytes() {
    EmbeddedChannel ch = new EmbeddedChannel(new SimpleChannelInboundHandler<LiveHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
            HttpResponseWriter writer = new HttpResponseWriter(ctx);
            CompletableFuture<Void> future = writer.write(response);
            assertThat(future.isDone(), is(false));
            contentObservable.onNext(new Buffer("aaa", UTF_8));
            assertThat(future.isDone(), is(false));
            contentObservable.onNext(new Buffer("bbbb", UTF_8));
            assertThat(future.isDone(), is(false));
            contentObservable.onError(new TransportLostException(new InetSocketAddress(getLoopbackAddress(), 5050), newOriginBuilder("localhost", 5050).build()));
            assertThat(future.isDone(), is(true));
            channelRead.set(true);
        }
    });
    ch.writeInbound(response(OK).body(new ByteStream(contentObservable)).build());
    assertThat(LOGGER.lastMessage(), is(loggingEvent(Level.WARN, "Content observable error. Written content bytes 7/7 \\(ackd/sent\\). Write events 3/3 \\(ackd/writes\\).*", TransportLostException.class, "Connection to origin lost. origin=\"generic-app:anonymous-origin:localhost:5050\", remoteAddress=\"localhost/127.0.0.1:5050.*")));
}
Also used : Buffer(com.hotels.styx.api.Buffer) CompletableFuture(java.util.concurrent.CompletableFuture) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) InetSocketAddress(java.net.InetSocketAddress) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)3 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)2 Buffer (com.hotels.styx.api.Buffer)1 Buffers (com.hotels.styx.api.Buffers)1 ByteStream (com.hotels.styx.api.ByteStream)1 HOST (com.hotels.styx.api.HttpHeaderNames.HOST)1 HttpMethod (com.hotels.styx.api.HttpMethod)1 HttpVersion (com.hotels.styx.api.HttpVersion)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 Requests (com.hotels.styx.api.Requests)1 Origin (com.hotels.styx.api.extension.Origin)1 OriginStatsFactory (com.hotels.styx.client.OriginStatsFactory)1 HttpMessageFormatter (com.hotels.styx.common.format.HttpMessageFormatter)1 HttpRequestMessageLogger (com.hotels.styx.common.logging.HttpRequestMessageLogger)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1