hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [5/5] httpcomponents-core git commit: Fixed regression in non-blocking session request state management that could lead to incorrect session cancellation
Date Sun, 18 Jun 2017 19:17:28 GMT
Fixed regression in non-blocking session request state management that could lead to incorrect
session cancellation


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

Branch: refs/heads/master
Commit: 876fa863649211ea6d45b3c3e09667fa5b9e2a41
Parents: 42294a3
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Sun Jun 18 20:23:56 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Sun Jun 18 21:14:46 2017 +0200

----------------------------------------------------------------------
 .../hc/core5/reactor/IOSessionRequest.java      | 60 +++++++++++++-------
 .../hc/core5/reactor/SingleCoreIOReactor.java   |  6 +-
 2 files changed, 41 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/876fa863/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionRequest.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionRequest.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionRequest.java
index 3e27232..b30f50d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionRequest.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionRequest.java
@@ -28,23 +28,27 @@
 package org.apache.hc.core5.reactor;
 
 import java.net.SocketAddress;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicReference;
 
-import org.apache.hc.core5.concurrent.Cancellable;
-import org.apache.hc.core5.concurrent.ComplexFuture;
+import org.apache.hc.core5.concurrent.BasicFuture;
+import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.io.GracefullyCloseable;
 import org.apache.hc.core5.io.ShutdownType;
 import org.apache.hc.core5.net.NamedEndpoint;
 import org.apache.hc.core5.util.TimeValue;
 
-final class IOSessionRequest implements Cancellable {
+final class IOSessionRequest implements Future<IOSession> {
 
     final NamedEndpoint remoteEndpoint;
     final SocketAddress remoteAddress;
     final SocketAddress localAddress;
     final TimeValue timeout;
     final Object attachment;
-    final ComplexFuture<IOSession> future;
+    final BasicFuture<IOSession> future;
 
     private final AtomicReference<GracefullyCloseable> closeableRef;
 
@@ -54,49 +58,63 @@ final class IOSessionRequest implements Cancellable {
             final SocketAddress localAddress,
             final TimeValue timeout,
             final Object attachment,
-            final ComplexFuture<IOSession> future) {
+            final FutureCallback<IOSession> callback) {
         super();
         this.remoteEndpoint = remoteEndpoint;
         this.remoteAddress = remoteAddress;
         this.localAddress = localAddress;
         this.timeout = timeout;
         this.attachment = attachment;
-        this.future = future;
+        this.future = new BasicFuture<>(callback);
         this.closeableRef = new AtomicReference<>(null);
     }
 
     public void completed(final TlsCapableIOSession ioSession) {
         future.completed(ioSession);
+        closeableRef.set(null);
     }
 
     public void failed(final Exception cause) {
         future.failed(cause);
+        closeableRef.set(null);
     }
 
     public boolean cancel() {
-        return future.cancel();
+        final boolean cancelled = future.cancel();
+        final GracefullyCloseable closeable = closeableRef.getAndSet(null);
+        if (cancelled && closeable != null) {
+            closeable.shutdown(ShutdownType.IMMEDIATE);
+        }
+        return cancelled;
     }
 
+    @Override
+    public boolean cancel(final boolean mayInterruptIfRunning) {
+        return cancel();
+    }
+
+    @Override
     public boolean isCancelled() {
         return future.isCancelled();
     }
 
     public void assign(final GracefullyCloseable closeable) {
         closeableRef.set(closeable);
-        future.setDependency(new Cancellable() {
-
-            @Override
-            public boolean cancel() {
-                final GracefullyCloseable closeable = closeableRef.getAndSet(null);
-                if (closeable != null) {
-                    closeable.shutdown(ShutdownType.IMMEDIATE);
-                    return true;
-                } else {
-                    return false;
-                }
-            }
-
-        });
+    }
+
+    @Override
+    public boolean isDone() {
+        return future.isDone();
+    }
+
+    @Override
+    public IOSession get() throws InterruptedException, ExecutionException {
+        return future.get();
+    }
+
+    @Override
+    public IOSession get(final long timeout, final TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException {
+        return future.get(timeout, unit);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/876fa863/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
----------------------------------------------------------------------
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 0793ca7..56dec9b 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
@@ -42,7 +42,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.apache.hc.core5.concurrent.ComplexFuture;
 import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.function.Callback;
 import org.apache.hc.core5.function.Decorator;
@@ -237,19 +236,18 @@ class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements
Connect
             final Object attachment,
             final FutureCallback<IOSession> callback) throws IOReactorShutdownException
{
         Args.notNull(remoteEndpoint, "Remote endpoint");
-        final ComplexFuture<IOSession> future = new ComplexFuture<>(callback);
         final IOSessionRequest sessionRequest = new IOSessionRequest(
                 remoteEndpoint,
                 remoteAddress != null ? remoteAddress : new InetSocketAddress(remoteEndpoint.getHostName(),
remoteEndpoint.getPort()),
                 localAddress,
                 timeout,
                 attachment,
-                future);
+                callback);
 
         this.requestQueue.add(sessionRequest);
         this.selector.wakeup();
 
-        return future;
+        return sessionRequest;
     }
 
     private void prepareSocket(final Socket socket) throws IOException {


Mime
View raw message