Search in sources :

Example 1 with TimeoutsHolder

use of org.asynchttpclient.netty.timeout.TimeoutsHolder in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method sendRequestWithOpenChannel.

private <T> ListenableFuture<T> sendRequestWithOpenChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) {
    if (asyncHandler instanceof AsyncHandlerExtensions)
        AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel);
    TimeoutsHolder timeoutsHolder = scheduleRequestTimeout(future);
    timeoutsHolder.initRemoteAddress((InetSocketAddress) channel.remoteAddress());
    future.setChannelState(ChannelState.POOLED);
    future.attachChannel(channel, false);
    if (LOGGER.isDebugEnabled()) {
        HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
        LOGGER.debug("Using open Channel {} for {} '{}'", channel, httpRequest.method(), httpRequest.uri());
    }
    // channelInactive might be called between isChannelValid and writeRequest
    // so if we don't store the Future now, channelInactive won't perform handleUnexpectedClosedChannel
    Channels.setAttribute(channel, future);
    if (Channels.isChannelValid(channel)) {
        writeRequest(future, channel);
    } else {
        // bad luck, the channel was closed in-between
        // there's a very good chance onClose was already notified but the
        // future wasn't already registered
        handleUnexpectedClosedChannel(channel, future);
    }
    return future;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) AsyncHandlerExtensions(org.asynchttpclient.handler.AsyncHandlerExtensions) TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder)

Example 2 with TimeoutsHolder

use of org.asynchttpclient.netty.timeout.TimeoutsHolder in project async-http-client by AsyncHttpClient.

the class NettyConnectListener method onSuccess.

public void onSuccess(Channel channel, InetSocketAddress remoteAddress) {
    if (connectionSemaphore != null) {
        // transfer lock from future to channel
        Object partitionKeyLock = future.takePartitionKeyLock();
        if (partitionKeyLock != null) {
            channel.closeFuture().addListener(future -> connectionSemaphore.releaseChannelLock(partitionKeyLock));
        }
    }
    Channels.setActiveToken(channel);
    TimeoutsHolder timeoutsHolder = future.getTimeoutsHolder();
    if (futureIsAlreadyCancelled(channel)) {
        return;
    }
    Request request = future.getTargetRequest();
    Uri uri = request.getUri();
    timeoutsHolder.setResolvedRemoteAddress(remoteAddress);
    ProxyServer proxyServer = future.getProxyServer();
    // in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request
    if ((proxyServer == null || proxyServer.getProxyType().isSocks()) && uri.isSecured()) {
        SslHandler sslHandler;
        try {
            sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost(), proxyServer != null);
        } catch (Exception sslError) {
            onFailure(channel, sslError);
            return;
        }
        final AsyncHandler<?> asyncHandler = future.getAsyncHandler();
        try {
            asyncHandler.onTlsHandshakeAttempt();
        } catch (Exception e) {
            LOGGER.error("onTlsHandshakeAttempt crashed", e);
            onFailure(channel, e);
            return;
        }
        sslHandler.handshakeFuture().addListener(new SimpleFutureListener<Channel>() {

            @Override
            protected void onSuccess(Channel value) {
                try {
                    asyncHandler.onTlsHandshakeSuccess();
                } catch (Exception e) {
                    LOGGER.error("onTlsHandshakeSuccess crashed", e);
                    NettyConnectListener.this.onFailure(channel, e);
                    return;
                }
                writeRequest(channel);
            }

            @Override
            protected void onFailure(Throwable cause) {
                try {
                    asyncHandler.onTlsHandshakeFailure(cause);
                } catch (Exception e) {
                    LOGGER.error("onTlsHandshakeFailure crashed", e);
                    NettyConnectListener.this.onFailure(channel, e);
                    return;
                }
                NettyConnectListener.this.onFailure(channel, cause);
            }
        });
    } else {
        writeRequest(channel);
    }
}
Also used : Channel(io.netty.channel.Channel) HttpRequest(io.netty.handler.codec.http.HttpRequest) Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) SslHandler(io.netty.handler.ssl.SslHandler) ConnectException(java.net.ConnectException) TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder) ProxyServer(org.asynchttpclient.proxy.ProxyServer)

Example 3 with TimeoutsHolder

use of org.asynchttpclient.netty.timeout.TimeoutsHolder in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method scheduleRequestTimeout.

private TimeoutsHolder scheduleRequestTimeout(NettyResponseFuture<?> nettyResponseFuture) {
    nettyResponseFuture.touch();
    TimeoutsHolder timeoutsHolder = new TimeoutsHolder(nettyTimer, nettyResponseFuture, this, config);
    nettyResponseFuture.setTimeoutsHolder(timeoutsHolder);
    return timeoutsHolder;
}
Also used : TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder)

Example 4 with TimeoutsHolder

use of org.asynchttpclient.netty.timeout.TimeoutsHolder in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method scheduleRequestTimeout.

private void scheduleRequestTimeout(NettyResponseFuture<?> nettyResponseFuture, InetSocketAddress originalRemoteAddress) {
    nettyResponseFuture.touch();
    TimeoutsHolder timeoutsHolder = new TimeoutsHolder(nettyTimer, nettyResponseFuture, this, config, originalRemoteAddress);
    nettyResponseFuture.setTimeoutsHolder(timeoutsHolder);
}
Also used : TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder)

Example 5 with TimeoutsHolder

use of org.asynchttpclient.netty.timeout.TimeoutsHolder in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method scheduleReadTimeout.

private void scheduleReadTimeout(NettyResponseFuture<?> nettyResponseFuture) {
    TimeoutsHolder timeoutsHolder = nettyResponseFuture.getTimeoutsHolder();
    if (timeoutsHolder != null) {
        // on very fast requests, it's entirely possible that the response has already
        // been completed
        // by the time we try to schedule the read timeout
        nettyResponseFuture.touch();
        timeoutsHolder.startReadTimeout();
    }
}
Also used : TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder)

Aggregations

TimeoutsHolder (org.asynchttpclient.netty.timeout.TimeoutsHolder)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 Channel (io.netty.channel.Channel)1 SslHandler (io.netty.handler.ssl.SslHandler)1 ConnectException (java.net.ConnectException)1 Request (org.asynchttpclient.Request)1 AsyncHandlerExtensions (org.asynchttpclient.handler.AsyncHandlerExtensions)1 ProxyServer (org.asynchttpclient.proxy.ProxyServer)1 Uri (org.asynchttpclient.uri.Uri)1