You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2022/11/16 22:53:04 UTC

[httpcomponents-core] branch socks5 updated: I/O reactor connect process redesign

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch socks5
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/socks5 by this push:
     new ebc1d6a56 I/O reactor connect process redesign
ebc1d6a56 is described below

commit ebc1d6a560f9980ecff9ea3e4b1263eedd846528
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Wed Nov 16 16:05:38 2022 +0100

    I/O reactor connect process redesign
---
 .../hc/core5/reactor/InternalConnectChannel.java   | 29 ++++++++----
 .../core5/reactor/InternalDataChannelFactory.java  | 43 ------------------
 .../hc/core5/reactor/SingleCoreIOReactor.java      | 52 +++++++++-------------
 3 files changed, 43 insertions(+), 81 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalConnectChannel.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalConnectChannel.java
index aa597621b..700ae0c1e 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalConnectChannel.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalConnectChannel.java
@@ -41,20 +41,26 @@ final class InternalConnectChannel extends InternalChannel {
     private final SelectionKey key;
     private final SocketChannel socketChannel;
     private final IOSessionRequest sessionRequest;
+    private final InternalDataChannel dataChannel;
+    private final IOEventHandlerFactory eventHandlerFactory;
+    private final IOReactorConfig reactorConfig;
     private final long creationTimeMillis;
-    private final InternalDataChannelFactory dataChannelFactory;
 
     InternalConnectChannel(
             final SelectionKey key,
             final SocketChannel socketChannel,
             final IOSessionRequest sessionRequest,
-            final InternalDataChannelFactory dataChannelFactory) {
+            final InternalDataChannel dataChannel,
+            final IOEventHandlerFactory eventHandlerFactory,
+            final IOReactorConfig reactorConfig) {
         super();
         this.key = key;
         this.socketChannel = socketChannel;
         this.sessionRequest = sessionRequest;
+        this.dataChannel = dataChannel;
+        this.eventHandlerFactory = eventHandlerFactory;
+        this.reactorConfig = reactorConfig;
         this.creationTimeMillis = System.currentTimeMillis();
-        this.dataChannelFactory = dataChannelFactory;
     }
 
     @Override
@@ -66,12 +72,19 @@ final class InternalConnectChannel extends InternalChannel {
             //check out connectTimeout
             final long now = System.currentTimeMillis();
             if (checkTimeout(now)) {
-                final InternalDataChannel dataChannel = dataChannelFactory.create(
-                        key,
-                        socketChannel,
-                        sessionRequest.remoteEndpoint,
-                        sessionRequest.attachment);
                 key.attach(dataChannel);
+                final IOEventHandler ioEventHandler;
+                if (reactorConfig.getSocksProxyAddress() == null) {
+                    ioEventHandler = eventHandlerFactory.createHandler(dataChannel, sessionRequest.attachment);
+                } else {
+                    final SocksProxyProtocolHandlerFactory eventHandlerFactory = new SocksProxyProtocolHandlerFactory(
+                            sessionRequest.remoteAddress,
+                            this.reactorConfig.getSocksProxyUsername(),
+                            this.reactorConfig.getSocksProxyPassword(),
+                            this.eventHandlerFactory);
+                    ioEventHandler = eventHandlerFactory.createHandler(dataChannel, sessionRequest.attachment);
+                }
+                dataChannel.upgrade(ioEventHandler);
                 sessionRequest.completed(dataChannel);
                 dataChannel.handleIOEvent(SelectionKey.OP_CONNECT);
             }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannelFactory.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannelFactory.java
deleted file mode 100644
index 5f008aa7a..000000000
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannelFactory.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.reactor;
-
-import java.nio.channels.SelectionKey;
-import java.nio.channels.SocketChannel;
-
-import org.apache.hc.core5.net.NamedEndpoint;
-
-interface InternalDataChannelFactory {
-
-    InternalDataChannel create(
-            SelectionKey key,
-            SocketChannel socketChannel,
-            NamedEndpoint namedEndpoint,
-            Object attachment);
-
-}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
index 6a465321f..248ce1049 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
@@ -324,27 +324,16 @@ class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements Connect
             sock.bind(sessionRequest.localAddress);
         }
 
-        final SocketAddress targetAddress;
-        final IOEventHandlerFactory eventHandlerFactory;
-        if (this.reactorConfig.getSocksProxyAddress() != null) {
-            targetAddress = this.reactorConfig.getSocksProxyAddress();
-            eventHandlerFactory = new SocksProxyProtocolHandlerFactory(
-                    sessionRequest.remoteAddress,
-                    this.reactorConfig.getSocksProxyUsername(),
-                    this.reactorConfig.getSocksProxyPassword(),
-                    this.eventHandlerFactory);
-        } else {
-            targetAddress = sessionRequest.remoteAddress;
-            eventHandlerFactory = this.eventHandlerFactory;
-        }
+        final SocketAddress socksProxyAddress = reactorConfig.getSocksProxyAddress();
+        final SocketAddress remoteAddress = socksProxyAddress != null ? socksProxyAddress : sessionRequest.remoteAddress;
 
         // Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
         // only to this library
-        validateAddress(targetAddress);
+        validateAddress(remoteAddress);
         final boolean connected;
         try {
             connected = AccessController.doPrivileged(
-                    (PrivilegedExceptionAction<Boolean>) () -> socketChannel.connect(targetAddress));
+                    (PrivilegedExceptionAction<Boolean>) () -> socketChannel.connect(remoteAddress));
         } catch (final PrivilegedActionException e) {
             Asserts.check(e.getCause() instanceof  IOException,
                     "method contract violation only checked exceptions are wrapped: " + e.getCause());
@@ -353,23 +342,26 @@ class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements Connect
         }
 
         final SelectionKey key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
-        final InternalChannel channel = new InternalConnectChannel(key, socketChannel, sessionRequest, (k, sc, namedEndpoint, attachment) -> {
-            final IOSession ioSession = new IOSessionImpl("c", k, sc);
-            final InternalDataChannel dataChannel = new InternalDataChannel(
-                    ioSession,
-                    namedEndpoint,
-                    ioSessionDecorator,
-                    sessionListener,
-                    closedSessions);
-            dataChannel.upgrade(eventHandlerFactory.createHandler(dataChannel, attachment));
-            dataChannel.setSocketTimeout(reactorConfig.getSoTimeout());
-            return dataChannel;
-        });
+        final IOSession ioSession = new IOSessionImpl("c", key, socketChannel);
+        final InternalDataChannel dataChannel = new InternalDataChannel(
+                ioSession,
+                sessionRequest.remoteEndpoint,
+                ioSessionDecorator,
+                sessionListener,
+                closedSessions);
+        dataChannel.setSocketTimeout(reactorConfig.getSoTimeout());
+        final InternalChannel connectChannel = new InternalConnectChannel(
+                key,
+                socketChannel,
+                sessionRequest,
+                dataChannel,
+                eventHandlerFactory,
+                reactorConfig);
         if (connected) {
-            channel.handleIOEvent(SelectionKey.OP_CONNECT);
+            connectChannel.handleIOEvent(SelectionKey.OP_CONNECT);
         } else {
-            key.attach(channel);
-            sessionRequest.assign(channel);
+            key.attach(connectChannel);
+            sessionRequest.assign(connectChannel);
         }
     }