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 2018/11/19 14:10:43 UTC

httpcomponents-core git commit: HTTP/1.1 protocol handlers to use I/O session lock for output synchronization

Repository: httpcomponents-core
Updated Branches:
  refs/heads/olegk_experimental 9f306716e -> faf372b37


HTTP/1.1 protocol handlers to use I/O session lock for output synchronization


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/faf372b3
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/faf372b3
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/faf372b3

Branch: refs/heads/olegk_experimental
Commit: faf372b37e4d58d1739cc25cdb491f3d30e08224
Parents: 9f30671
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Nov 19 15:10:21 2018 +0100
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Nov 19 15:10:21 2018 +0100

----------------------------------------------------------------------
 .../impl/nio/AbstractHttp1StreamDuplexer.java   | 40 ++++++++------------
 1 file changed, 15 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/faf372b3/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
index 5b549ba..585cbed 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
@@ -36,8 +36,6 @@ import java.nio.channels.SelectionKey;
 import java.nio.channels.WritableByteChannel;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 
 import javax.net.ssl.SSLSession;
 
@@ -97,7 +95,6 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
     private final ContentLengthStrategy outgoingContentStrategy;
     private final AtomicInteger inputWindow;
     private final ByteBuffer contentBuffer;
-    private final Lock outputLock;
     private final AtomicInteger outputRequests;
 
     private volatile Message<IncomingMessage, ContentDecoder> incomingMessage;
@@ -134,7 +131,6 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                 DefaultContentLengthStrategy.INSTANCE;
         this.inputWindow = new AtomicInteger(0);
         this.contentBuffer = ByteBuffer.allocate(this.h1Config.getBufferSize());
-        this.outputLock = new ReentrantLock();
         this.outputRequests = new AtomicInteger(0);
         this.connState = ConnectionState.READY;
     }
@@ -362,7 +358,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
     }
 
     public final void onOutput() throws IOException, HttpException {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             if (outbuf.hasData()) {
                 final int bytesWritten = outbuf.flush(ioSession.channel());
@@ -371,29 +367,23 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                 }
             }
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
         if (connState.compareTo(ConnectionState.SHUTDOWN) < 0) {
             produceOutput();
             final int pendingOutputRequests = outputRequests.get();
             final boolean outputPending = isOutputReady();
-            outputLock.lock();
+            final boolean outputEnd;
+            ioSession.lock().lock();
             try {
                 if (!outputPending && !outbuf.hasData() && outputRequests.compareAndSet(pendingOutputRequests, 0)) {
                     ioSession.clearEvent(SelectionKey.OP_WRITE);
                 } else {
                     outputRequests.addAndGet(-pendingOutputRequests);
                 }
-            } finally {
-                outputLock.unlock();
-            }
-
-            outputLock.lock();
-            final boolean outputEnd;
-            try {
                 outputEnd = outgoingMessage == null && !outbuf.hasData();
             } finally {
-                outputLock.unlock();
+                ioSession.lock().unlock();
             }
             if (outputEnd) {
                 outputEnd();
@@ -469,7 +459,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
             final OutgoingMessage messageHead,
             final boolean endStream,
             final FlushMode flushMode) throws HttpException, IOException {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             outgoingMessageWriter.write(messageHead, outbuf);
             updateOutputMetrics(messageHead, connMetrics);
@@ -491,7 +481,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
             }
             ioSession.setEvent(EventMask.WRITE);
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
     }
 
@@ -513,7 +503,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
     }
 
     void suspendSessionOutput() throws IOException {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             if (outbuf.hasData()) {
                 outbuf.flush(ioSession.channel());
@@ -521,12 +511,12 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                 ioSession.clearEvent(SelectionKey.OP_WRITE);
             }
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
     }
 
     int streamOutput(final ByteBuffer src) throws IOException {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             if (outgoingMessage == null) {
                 throw new ClosedChannelException();
@@ -538,14 +528,14 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
             }
             return bytesWritten;
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
     }
 
     enum MessageDelineation { NONE, CHUNK_CODED, MESSAGE_HEAD}
 
     MessageDelineation endOutputStream(final List<? extends Header> trailers) throws IOException {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             if (outgoingMessage == null) {
                 return MessageDelineation.NONE;
@@ -558,12 +548,12 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                             ? MessageDelineation.CHUNK_CODED
                             : MessageDelineation.MESSAGE_HEAD;
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
     }
 
     boolean isOutputCompleted() {
-        outputLock.lock();
+        ioSession.lock().lock();
         try {
             if (outgoingMessage == null) {
                 return true;
@@ -571,7 +561,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
             final ContentEncoder contentEncoder = outgoingMessage.getBody();
             return contentEncoder.isCompleted();
         } finally {
-            outputLock.unlock();
+            ioSession.lock().unlock();
         }
     }