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 2019/12/05 14:33:16 UTC

[httpcomponents-client] 01/01: Fixed session i/o and wire logging in async clients

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

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

commit 070f30fdc4506694515c826b4163d6aee487c867
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Dec 5 15:27:27 2019 +0100

    Fixed session i/o and wire logging in async clients
---
 .../http/impl/async/H2AsyncClientBuilder.java      |   2 +-
 .../async/H2AsyncClientEventHandlerFactory.java    |  12 +-
 .../http/impl/async/HttpAsyncClientBuilder.java    |   2 +-
 .../async/HttpAsyncClientEventHandlerFactory.java  |  12 +-
 .../client5/http/impl/async/LoggingIOSession.java  | 129 ++++++---------------
 .../http/impl/async/LoggingIOSessionDecorator.java |  53 +++++++++
 .../http/impl/async/MinimalH2AsyncClient.java      |   2 +-
 .../http/impl/async/MinimalHttpAsyncClient.java    |   2 +-
 .../nio/DefaultManagedAsyncClientConnection.java   |   3 +-
 9 files changed, 100 insertions(+), 117 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java
index 6bd7c48..02e6f10 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java
@@ -720,7 +720,7 @@ public class H2AsyncClientBuilder {
                 ioEventHandlerFactory,
                 ioReactorConfig != null ? ioReactorConfig : IOReactorConfig.DEFAULT,
                 threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-dispatch", true),
-                null,
+                LoggingIOSessionDecorator.INSTANCE,
                 LoggingExceptionCallback.INSTANCE,
                 null,
                 new Callback<IOSession>() {
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java
index 6e3799b..e7a40c6 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java
@@ -30,7 +30,6 @@ package org.apache.hc.client5.http.impl.async;
 import java.io.IOException;
 import java.util.List;
 
-import org.apache.hc.client5.http.impl.ConnPoolSupport;
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpConnection;
 import org.apache.hc.core5.http.config.CharCodingConfig;
@@ -52,7 +51,6 @@ import org.slf4j.LoggerFactory;
 
 class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory {
 
-    private final Logger wireLog = LoggerFactory.getLogger("org.apache.hc.client5.http.wire");
     private final Logger headerLog = LoggerFactory.getLogger("org.apache.hc.client5.http.headers");
     private final Logger frameLog = LoggerFactory.getLogger("org.apache.hc.client5.http2.frame");
     private final Logger framePayloadLog = LoggerFactory.getLogger("org.apache.hc.client5.http2.frame.payload");
@@ -76,14 +74,11 @@ class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory {
 
     @Override
     public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
-        final Logger sessionLog = LoggerFactory.getLogger(ioSession.getClass());
-        if (sessionLog.isDebugEnabled()
-                || wireLog.isDebugEnabled()
-                || headerLog.isDebugEnabled()
+        if (headerLog.isDebugEnabled()
                 || frameLog.isDebugEnabled()
                 || framePayloadLog.isDebugEnabled()
                 || flowCtrlLog.isDebugEnabled()) {
-            final String id = ConnPoolSupport.getId(ioSession);
+            final String id = ioSession.getId();
             final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(
                     httpProcessor,
                     exchangeHandlerFactory,
@@ -172,8 +167,7 @@ class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory {
                         }
 
                     });
-            final LoggingIOSession loggingIOSession = new LoggingIOSession(ioSession, id, sessionLog, wireLog);
-            return new H2OnlyClientProtocolNegotiator(loggingIOSession, http2StreamHandlerFactory, false);
+            return new H2OnlyClientProtocolNegotiator(ioSession, http2StreamHandlerFactory, false);
         }
         final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(
                 httpProcessor,
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
index 67c0f32..75eb512 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java
@@ -927,7 +927,7 @@ public class HttpAsyncClientBuilder {
                 ioEventHandlerFactory,
                 ioReactorConfig != null ? ioReactorConfig : IOReactorConfig.DEFAULT,
                 threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-dispatch", true),
-                null,
+                LoggingIOSessionDecorator.INSTANCE,
                 LoggingExceptionCallback.INSTANCE,
                 null,
                 new Callback<IOSession>() {
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java
index c89db2a..3b36bf6 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java
@@ -31,7 +31,6 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.hc.client5.http.impl.ConnPoolSupport;
 import org.apache.hc.core5.http.ConnectionReuseStrategy;
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpConnection;
@@ -68,7 +67,6 @@ import org.slf4j.LoggerFactory;
 class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory {
 
     private final Logger streamLog = LoggerFactory.getLogger(InternalHttpAsyncClient.class);
-    private final Logger wireLog = LoggerFactory.getLogger("org.apache.hc.client5.http.wire");
     private final Logger headerLog = LoggerFactory.getLogger("org.apache.hc.client5.http.headers");
     private final Logger frameLog = LoggerFactory.getLogger("org.apache.hc.client5.http2.frame");
     private final Logger framePayloadLog = LoggerFactory.getLogger("org.apache.hc.client5.http2.frame.payload");
@@ -105,15 +103,12 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory {
 
     @Override
     public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
-        final Logger sessionLog = LoggerFactory.getLogger(ioSession.getClass());
-        if (sessionLog.isDebugEnabled()
-                || streamLog.isDebugEnabled()
-                || wireLog.isDebugEnabled()
+        if (streamLog.isDebugEnabled()
                 || headerLog.isDebugEnabled()
                 || frameLog.isDebugEnabled()
                 || framePayloadLog.isDebugEnabled()
                 || flowCtrlLog.isDebugEnabled()) {
-            final String id = ConnPoolSupport.getId(ioSession);
+            final String id = ioSession.getId();
             final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(
                     httpProcessor,
                     h1Config,
@@ -243,9 +238,8 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory {
                         }
 
                     });
-            final LoggingIOSession loggingIOSession = new LoggingIOSession(ioSession, id, sessionLog, wireLog);
             return new ClientHttpProtocolNegotiator(
-                            loggingIOSession,
+                            ioSession,
                             http1StreamHandlerFactory,
                             http2StreamHandlerFactory,
                             attachment instanceof HttpVersionPolicy ? (HttpVersionPolicy) attachment : versionPolicy);
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSession.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSession.java
index 9156915..7d411ed 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSession.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSession.java
@@ -34,40 +34,27 @@ import java.nio.channels.ByteChannel;
 import java.nio.channels.SelectionKey;
 import java.util.concurrent.locks.Lock;
 
-import javax.net.ssl.SSLContext;
-
 import org.apache.hc.client5.http.impl.Wire;
 import org.apache.hc.core5.io.CloseMode;
-import org.apache.hc.core5.net.NamedEndpoint;
 import org.apache.hc.core5.reactor.Command;
 import org.apache.hc.core5.reactor.IOEventHandler;
-import org.apache.hc.core5.reactor.ProtocolIOSession;
-import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
-import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
-import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
-import org.apache.hc.core5.reactor.ssl.TlsDetails;
+import org.apache.hc.core5.reactor.IOSession;
 import org.apache.hc.core5.util.Timeout;
 import org.slf4j.Logger;
 
-class LoggingIOSession implements ProtocolIOSession {
+class LoggingIOSession implements IOSession {
 
     private final Logger log;
     private final Wire wireLog;
     private final String id;
-    private final ProtocolIOSession session;
-    private final ByteChannel channel;
+    private final IOSession session;
 
-    public LoggingIOSession(final ProtocolIOSession session, final String id, final Logger log, final Logger wireLog) {
+    public LoggingIOSession(final IOSession session, final Logger log, final Logger wireLog) {
         super();
         this.session = session;
-        this.id = id;
+        this.id = session.getId();
         this.log = log;
         this.wireLog = new Wire(wireLog, this.id);
-        this.channel = new LoggingByteChannel();
-    }
-
-    public LoggingIOSession(final ProtocolIOSession session, final String id, final Logger log) {
-        this(session, id, log, null);
     }
 
     @Override
@@ -93,11 +80,14 @@ class LoggingIOSession implements ProtocolIOSession {
     @Override
     public void enqueue(final Command command, final Command.Priority priority) {
         this.session.enqueue(command, priority);
+        if (this.log.isDebugEnabled()) {
+            this.log.debug(this.session + " Enqueued " + command.getClass().getSimpleName() + " with priority " + priority);
+        }
     }
 
     @Override
     public ByteChannel channel() {
-        return this.channel;
+        return this.session.channel();
     }
 
     @Override
@@ -160,7 +150,7 @@ class LoggingIOSession implements ProtocolIOSession {
 
     @Override
     public boolean isOpen() {
-        return channel.isOpen();
+        return session.isOpen();
     }
 
     @Override
@@ -233,24 +223,36 @@ class LoggingIOSession implements ProtocolIOSession {
     }
 
     @Override
-    public void startTls(
-            final SSLContext sslContext,
-            final NamedEndpoint endpoint,
-            final SSLBufferMode sslBufferMode,
-            final SSLSessionInitializer initializer,
-            final SSLSessionVerifier verifier,
-            final Timeout handshakeTimeout) throws UnsupportedOperationException {
-        session.startTls(sslContext, endpoint, sslBufferMode, initializer, verifier, handshakeTimeout);
+    public int read(final ByteBuffer dst) throws IOException {
+        final int bytesRead = this.session.channel().read(dst);
+        if (this.log.isDebugEnabled()) {
+            this.log.debug(this.id + " " + this.session + ": " + bytesRead + " bytes read");
+        }
+        if (bytesRead > 0 && this.wireLog.isEnabled()) {
+            final ByteBuffer b = dst.duplicate();
+            final int p = b.position();
+            b.limit(p);
+            b.position(p - bytesRead);
+            this.wireLog.input(b);
+        }
+        return bytesRead;
     }
 
-    @Override
-    public TlsDetails getTlsDetails() {
-        return session.getTlsDetails();
-    }
 
     @Override
-    public NamedEndpoint getInitialEndpoint() {
-        return session.getInitialEndpoint();
+    public int write(final ByteBuffer src) throws IOException {
+        final int byteWritten = session.channel().write(src);
+        if (this.log.isDebugEnabled()) {
+            this.log.debug(this.id + " " + this.session + ": " + byteWritten + " bytes written");
+        }
+        if (byteWritten > 0 && this.wireLog.isEnabled()) {
+            final ByteBuffer b = src.duplicate();
+            final int p = b.position();
+            b.limit(p);
+            b.position(p - byteWritten);
+            this.wireLog.output(b);
+        }
+        return byteWritten;
     }
 
     @Override
@@ -258,63 +260,4 @@ class LoggingIOSession implements ProtocolIOSession {
         return this.id + " " + this.session.toString();
     }
 
-    @Override
-    public int read(final ByteBuffer dst) throws IOException {
-        return channel.read(dst);
-    }
-
-    @Override
-    public int write(final ByteBuffer src) throws IOException {
-        return channel.write(src);
-    }
-
-    class LoggingByteChannel implements ByteChannel {
-
-        @Override
-        public int read(final ByteBuffer dst) throws IOException {
-            final int bytesRead = session.channel().read(dst);
-            if (log.isDebugEnabled()) {
-                log.debug(id + " " + session + ": " + bytesRead + " bytes read");
-            }
-            if (bytesRead > 0 && wireLog.isEnabled()) {
-                final ByteBuffer b = dst.duplicate();
-                final int p = b.position();
-                b.limit(p);
-                b.position(p - bytesRead);
-                wireLog.input(b);
-            }
-            return bytesRead;
-        }
-
-        @Override
-        public int write(final ByteBuffer src) throws IOException {
-            final int byteWritten = session.channel().write(src);
-            if (log.isDebugEnabled()) {
-                log.debug(id + " " + session + ": " + byteWritten + " bytes written");
-            }
-            if (byteWritten > 0 && wireLog.isEnabled()) {
-                final ByteBuffer b = src.duplicate();
-                final int p = b.position();
-                b.limit(p);
-                b.position(p - byteWritten);
-                wireLog.output(b);
-            }
-            return byteWritten;
-        }
-
-        @Override
-        public void close() throws IOException {
-            if (log.isDebugEnabled()) {
-                log.debug(id + " " + session + ": Channel close");
-            }
-            session.channel().close();
-        }
-
-        @Override
-        public boolean isOpen() {
-            return session.channel().isOpen();
-        }
-
-    }
-
 }
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSessionDecorator.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSessionDecorator.java
new file mode 100644
index 0000000..571d6f4
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/LoggingIOSessionDecorator.java
@@ -0,0 +1,53 @@
+/*
+ * ====================================================================
+ * 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.client5.http.impl.async;
+
+import org.apache.hc.core5.function.Decorator;
+import org.apache.hc.core5.reactor.IOSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class LoggingIOSessionDecorator implements Decorator<IOSession> {
+
+    public final static LoggingIOSessionDecorator INSTANCE = new LoggingIOSessionDecorator();
+
+    private final Logger wireLog = LoggerFactory.getLogger("org.apache.hc.client5.http.wire");
+
+    private LoggingIOSessionDecorator() {
+    }
+
+    @Override
+    public IOSession decorate(final IOSession ioSession) {
+        final Logger sessionLog = LoggerFactory.getLogger(ioSession.getClass());
+        if (sessionLog.isDebugEnabled() || wireLog.isDebugEnabled()) {
+            return new LoggingIOSession(ioSession, sessionLog, wireLog);
+        } else {
+            return ioSession;
+        }
+    }
+}
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java
index cad86c1..5147426 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java
@@ -105,7 +105,7 @@ public final class MinimalH2AsyncClient extends AbstractMinimalHttpAsyncClientBa
                 eventHandlerFactory,
                 reactorConfig,
                 workerThreadFactory,
-                null,
+                LoggingIOSessionDecorator.INSTANCE,
                 LoggingExceptionCallback.INSTANCE,
                 null,
                 new Callback<IOSession>() {
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java
index 53df7a0..fc93999 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java
@@ -115,7 +115,7 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
                 eventHandlerFactory,
                 reactorConfig,
                 workerThreadFactory,
-                null,
+                LoggingIOSessionDecorator.INSTANCE,
                 LoggingExceptionCallback.INSTANCE,
                 null,
                 new Callback<IOSession>() {
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/DefaultManagedAsyncClientConnection.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/DefaultManagedAsyncClientConnection.java
index a2bda68..5026135 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/DefaultManagedAsyncClientConnection.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/DefaultManagedAsyncClientConnection.java
@@ -34,7 +34,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 
-import org.apache.hc.client5.http.impl.ConnPoolSupport;
 import org.apache.hc.client5.http.nio.ManagedAsyncClientConnection;
 import org.apache.hc.core5.http.EndpointDetails;
 import org.apache.hc.core5.http.HttpConnection;
@@ -72,7 +71,7 @@ final class DefaultManagedAsyncClientConnection implements ManagedAsyncClientCon
 
     @Override
     public String getId() {
-        return ConnPoolSupport.getId(ioSession);
+        return ioSession.getId();
     }
 
     @Override