|
65 | 65 | abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel { |
66 | 66 | private static final ChannelMetadata METADATA = new ChannelMetadata(false); |
67 | 67 | protected final LinuxSocket socket; |
68 | | - /** |
69 | | - * The future of the current connection attempt. If not null, subsequent |
70 | | - * connection attempts will fail. |
71 | | - */ |
72 | 68 | private ChannelPromise connectPromise; |
73 | | - private Future<?> connectTimeoutFuture; |
74 | 69 | private SocketAddress requestedRemoteAddress; |
75 | 70 | private volatile SocketAddress local; |
76 | 71 | private volatile SocketAddress remote; |
@@ -175,12 +170,6 @@ protected void doClose(ChannelPromise promise) { |
175 | 170 | this.connectPromise = null; |
176 | 171 | } |
177 | 172 |
|
178 | | - Future<?> future = connectTimeoutFuture; |
179 | | - if (future != null) { |
180 | | - future.cancel(false); |
181 | | - connectTimeoutFuture = null; |
182 | | - } |
183 | | - |
184 | 173 | if (isRegistered()) { |
185 | 174 | // Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor |
186 | 175 | // if SO_LINGER is used. |
@@ -440,6 +429,24 @@ final long doWriteOrSendBytes(ByteBuf data, InetSocketAddress remoteAddress, boo |
440 | 429 | remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen); |
441 | 430 | } |
442 | 431 |
|
| 432 | + @Override |
| 433 | + protected void doConnect( |
| 434 | + final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { |
| 435 | + final boolean connected; |
| 436 | + requestedRemoteAddress = remoteAddress; |
| 437 | + try { |
| 438 | + connected = doConnect(remoteAddress, localAddress); |
| 439 | + } catch (Throwable cause) { |
| 440 | + promise.setFailure(cause); |
| 441 | + return; |
| 442 | + } |
| 443 | + if (connected) { |
| 444 | + promise.setSuccess(); |
| 445 | + } else { |
| 446 | + connectPromise = promise; |
| 447 | + } |
| 448 | + } |
| 449 | + |
443 | 450 | protected abstract class AbstractEpollUnsafe extends AbstractUnsafe implements EpollIoHandle { |
444 | 451 | boolean readPending; |
445 | 452 | private EpollRecvByteAllocatorHandle allocHandle; |
@@ -642,127 +649,24 @@ protected final void clearEpollIn0() { |
642 | 649 | } |
643 | 650 | } |
644 | 651 |
|
645 | | - @Override |
646 | | - public void connect( |
647 | | - final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { |
648 | | - // Don't mark the connect promise as uncancellable as in fact we can cancel it as it is using |
649 | | - // non-blocking io. |
650 | | - if (promise.isDone() || !ensureOpen(promise)) { |
651 | | - return; |
652 | | - } |
653 | | - |
654 | | - try { |
655 | | - if (connectPromise != null) { |
656 | | - throw new ConnectionPendingException(); |
657 | | - } |
658 | | - |
659 | | - boolean wasActive = isActive(); |
660 | | - if (doConnect(remoteAddress, localAddress)) { |
661 | | - fulfillConnectPromise(promise, wasActive); |
662 | | - } else { |
663 | | - connectPromise = promise; |
664 | | - requestedRemoteAddress = remoteAddress; |
665 | | - |
666 | | - // Schedule connect timeout. |
667 | | - final int connectTimeoutMillis = config().getConnectTimeoutMillis(); |
668 | | - if (connectTimeoutMillis > 0) { |
669 | | - connectTimeoutFuture = executor().schedule(new Runnable() { |
670 | | - @Override |
671 | | - public void run() { |
672 | | - ChannelPromise connectPromise = AbstractEpollChannel.this.connectPromise; |
673 | | - if (connectPromise != null && !connectPromise.isDone() |
674 | | - && connectPromise.tryFailure(new ConnectTimeoutException( |
675 | | - "connection timed out after " + connectTimeoutMillis + " ms: " + |
676 | | - remoteAddress))) { |
677 | | - close(newPromise()); |
678 | | - } |
679 | | - } |
680 | | - }, connectTimeoutMillis, TimeUnit.MILLISECONDS); |
681 | | - } |
682 | | - |
683 | | - promise.addListener(new ChannelFutureListener() { |
684 | | - @Override |
685 | | - public void operationComplete(ChannelFuture future) { |
686 | | - // If the connect future is cancelled we also cancel the timeout and close the |
687 | | - // underlying socket. |
688 | | - if (future.isCancelled()) { |
689 | | - if (connectTimeoutFuture != null) { |
690 | | - connectTimeoutFuture.cancel(false); |
691 | | - } |
692 | | - connectPromise = null; |
693 | | - close(newPromise()); |
694 | | - } |
695 | | - } |
696 | | - }); |
697 | | - } |
698 | | - } catch (Throwable t) { |
699 | | - closeIfClosed(); |
700 | | - promise.tryFailure(annotateConnectException(t, remoteAddress)); |
701 | | - } |
702 | | - } |
703 | | - |
704 | | - private void fulfillConnectPromise(ChannelPromise promise, boolean wasActive) { |
705 | | - if (promise == null) { |
706 | | - // Closed via cancellation and the promise has been notified already. |
707 | | - return; |
708 | | - } |
709 | | - active = true; |
710 | | - |
711 | | - // Get the state as trySuccess() may trigger an ChannelFutureListener that will close the Channel. |
712 | | - // We still need to ensure we call fireChannelActive() in this case. |
713 | | - boolean active = isActive(); |
714 | | - |
715 | | - // trySuccess() will return false if a user cancelled the connection attempt. |
716 | | - boolean promiseSet = promise.trySuccess(); |
717 | | - |
718 | | - // Regardless if the connection attempt was cancelled, channelActive() event should be triggered, |
719 | | - // because what happened is what happened. |
720 | | - if (!wasActive && active) { |
721 | | - pipeline().fireChannelActive(); |
722 | | - } |
723 | | - |
724 | | - // If a user cancelled the connection attempt, close the channel, which is followed by channelInactive(). |
725 | | - if (!promiseSet) { |
726 | | - close(newPromise()); |
727 | | - } |
728 | | - } |
729 | | - |
730 | | - private void fulfillConnectPromise(ChannelPromise promise, Throwable cause) { |
731 | | - if (promise == null) { |
732 | | - // Closed via cancellation and the promise has been notified already. |
733 | | - return; |
734 | | - } |
735 | | - |
736 | | - // Use tryFailure() instead of setFailure() to avoid the race against cancel(). |
737 | | - promise.tryFailure(cause); |
738 | | - closeIfClosed(); |
739 | | - } |
740 | | - |
741 | 652 | private void finishConnect() { |
742 | 653 | // Note this method is invoked by the event loop only if the connection attempt was |
743 | 654 | // neither cancelled nor timed out. |
744 | 655 |
|
745 | 656 | assert executor().inEventLoop(); |
746 | | - |
747 | | - boolean connectStillInProgress = false; |
| 657 | + assert connectPromise != null; |
| 658 | + ChannelPromise promise = connectPromise; |
| 659 | + final boolean connected; |
748 | 660 | try { |
749 | | - boolean wasActive = isActive(); |
750 | | - if (!doFinishConnect()) { |
751 | | - connectStillInProgress = true; |
752 | | - return; |
753 | | - } |
754 | | - fulfillConnectPromise(connectPromise, wasActive); |
755 | | - } catch (Throwable t) { |
756 | | - fulfillConnectPromise(connectPromise, annotateConnectException(t, requestedRemoteAddress)); |
757 | | - } finally { |
758 | | - if (!connectStillInProgress) { |
759 | | - // Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used |
760 | | - // See https://github.com/netty/netty/issues/1770 |
761 | | - if (connectTimeoutFuture != null) { |
762 | | - connectTimeoutFuture.cancel(false); |
763 | | - } |
764 | | - connectPromise = null; |
765 | | - } |
| 661 | + connected = doFinishConnect(); |
| 662 | + } catch (Throwable cause) { |
| 663 | + connectPromise = null; |
| 664 | + promise.setFailure(cause); |
| 665 | + return; |
| 666 | + } |
| 667 | + if (connected) { |
| 668 | + connectPromise = null; |
| 669 | + promise.setSuccess(); |
766 | 670 | } |
767 | 671 | } |
768 | 672 |
|
@@ -776,6 +680,7 @@ private boolean doFinishConnect() throws Exception { |
776 | 680 | remote = computeRemoteAddr((InetSocketAddress) requestedRemoteAddress, socket.remoteAddress()); |
777 | 681 | } |
778 | 682 | requestedRemoteAddress = null; |
| 683 | + active = true; |
779 | 684 |
|
780 | 685 | return true; |
781 | 686 | } |
@@ -828,6 +733,7 @@ protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddr |
828 | 733 | if (connected) { |
829 | 734 | remote = remoteSocketAddr == null ? |
830 | 735 | remoteAddress : computeRemoteAddr(remoteSocketAddr, socket.remoteAddress()); |
| 736 | + active = true; |
831 | 737 | } |
832 | 738 | // We always need to set the localAddress even if not connected yet as the bind already took place. |
833 | 739 | // |
|