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/08/24 09:51:39 UTC

[httpcomponents-client] branch master updated (08b35c7 -> 8f6f6a5)

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

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


    from 08b35c7  Move javadocs to Builder classes
     new a478ed1  Fixed concurrent use of threading unsafe ClassicHttpRequest messages
     new 8f6f6a5  Improved handling of request cancellation (classic API)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../testing/sync/TestIdleConnectionEviction.java   | 27 ++++++------
 .../http/classic/methods/HttpExecutionAware.java   | 50 ----------------------
 .../http/classic/methods/HttpUriRequestBase.java   | 41 ++++++++++--------
 3 files changed, 38 insertions(+), 80 deletions(-)
 delete mode 100644 httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpExecutionAware.java


[httpcomponents-client] 02/02: Improved handling of request cancellation (classic API)

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8f6f6a5357d1aacdc9306666a71d1c9c822a27f2
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Aug 23 10:55:44 2019 +0200

    Improved handling of request cancellation (classic API)
---
 .../http/classic/methods/HttpExecutionAware.java   | 50 ----------------------
 .../http/classic/methods/HttpUriRequestBase.java   | 41 ++++++++++--------
 2 files changed, 23 insertions(+), 68 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpExecutionAware.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpExecutionAware.java
deleted file mode 100644
index ab4bb90..0000000
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpExecutionAware.java
+++ /dev/null
@@ -1,50 +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.client5.http.classic.methods;
-
-import org.apache.hc.core5.concurrent.Cancellable;
-
-/**
- * Interface to be implemented by any object that wishes to be notified of blocking I/O operations
- * that could be cancelled.
- *
- * @since 4.3
- */
-public interface HttpExecutionAware {
-
-    boolean isAborted();
-
-    /**
-     * Sets {@link Cancellable} for the ongoing operation.
-     *
-     * @param cancellable
-     *            {@link Cancellable} for the ongoing operation.
-     */
-    void setCancellable(Cancellable cancellable);
-
-}
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java
index d16716a..d6ea8b6 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java
@@ -27,8 +27,7 @@
 package org.apache.hc.client5.http.classic.methods;
 
 import java.net.URI;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.atomic.AtomicMarkableReference;
 
 import org.apache.hc.client5.http.config.RequestConfig;
 import org.apache.hc.core5.concurrent.Cancellable;
@@ -39,31 +38,31 @@ public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpU
 
     private static final long serialVersionUID = 1L;
 
+    private final AtomicMarkableReference<Cancellable> cancellableRef;
     private RequestConfig requestConfig;
-    private final AtomicBoolean cancelled;
-    private final AtomicReference<Cancellable> cancellableRef;
 
     public HttpUriRequestBase(final String method, final URI requestUri) {
         super(method, requestUri);
-        this.cancelled = new AtomicBoolean(false);
-        this.cancellableRef = new AtomicReference<>(null);
+        this.cancellableRef = new AtomicMarkableReference<>(null, false);
     }
 
     @Override
     public boolean cancel() {
-        if (this.cancelled.compareAndSet(false, true)) {
-            final Cancellable cancellable = this.cancellableRef.getAndSet(null);
-            if (cancellable != null) {
-                cancellable.cancel();
+        while (!cancellableRef.isMarked()) {
+            final Cancellable actualCancellable = cancellableRef.getReference();
+            if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
+                if (actualCancellable != null) {
+                    actualCancellable.cancel();
+                }
+                return true;
             }
-            return true;
         }
         return false;
     }
 
     @Override
     public boolean isCancelled() {
-        return cancelled.get();
+        return cancellableRef.isMarked();
     }
 
     /**
@@ -71,8 +70,9 @@ public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpU
      */
     @Override
     public void setDependency(final Cancellable cancellable) {
-        if (!this.cancelled.get()) {
-            this.cancellableRef.set(cancellable);
+        final Cancellable actualCancellable = cancellableRef.getReference();
+        if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
+            cancellable.cancel();
         }
     }
 
@@ -82,11 +82,16 @@ public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpU
      * @since 4.2
      */
     public void reset() {
-        final Cancellable cancellable = this.cancellableRef.getAndSet(null);
-        if (cancellable != null) {
-            cancellable.cancel();
+        for (;;) {
+            final boolean marked = cancellableRef.isMarked();
+            final Cancellable actualCancellable = cancellableRef.getReference();
+            if (actualCancellable != null) {
+                actualCancellable.cancel();
+            }
+            if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
+                break;
+            }
         }
-        this.cancelled.set(false);
     }
 
     @Override


[httpcomponents-client] 01/02: Fixed concurrent use of threading unsafe ClassicHttpRequest messages

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a478ed1bb0eb2bd5b9e8ea0fefc015b37dc63bbf
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Aug 22 16:08:12 2019 +0200

    Fixed concurrent use of threading unsafe ClassicHttpRequest messages
---
 .../testing/sync/TestIdleConnectionEviction.java   | 27 ++++++++++++----------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestIdleConnectionEviction.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestIdleConnectionEviction.java
index c66b395..46cecd1 100644
--- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestIdleConnectionEviction.java
+++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestIdleConnectionEviction.java
@@ -27,11 +27,12 @@
 
 package org.apache.hc.client5.testing.sync;
 
+import java.net.URI;
+
 import org.apache.hc.client5.http.ClientProtocolException;
 import org.apache.hc.client5.http.classic.methods.HttpGet;
 import org.apache.hc.client5.http.impl.IdleConnectionEvictor;
 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -50,10 +51,10 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
         final IdleConnectionEvictor idleConnectionMonitor = new IdleConnectionEvictor(this.connManager, TimeValue.ofMilliseconds(50));
         idleConnectionMonitor.start();
 
-        final HttpGet httpget = new HttpGet("/random/1024");
+        final URI requestUri = new URI("/random/1024");
         final WorkerThread[] workers = new WorkerThread[5];
         for (int i = 0; i < workers.length; i++) {
-            workers[i] = new WorkerThread(httpclient, target, httpget, 200);
+            workers[i] = new WorkerThread(httpclient, target, requestUri, 200);
         }
         for (final WorkerThread worker : workers) {
             worker.start();
@@ -72,7 +73,7 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
 
         private final CloseableHttpClient httpclient;
         private final HttpHost target;
-        private final ClassicHttpRequest request;
+        private final URI requestUri;
         private final int count;
 
         private volatile Exception ex;
@@ -80,12 +81,12 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
         public WorkerThread(
                 final CloseableHttpClient httpclient,
                 final HttpHost target,
-                final ClassicHttpRequest request,
+                final URI requestUri,
                 final int count) {
             super();
             this.httpclient = httpclient;
             this.target = target;
-            this.request = request;
+            this.requestUri = requestUri;
             this.count = count;
         }
 
@@ -93,13 +94,15 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
         public void run() {
             try {
                 for (int i = 0; i < this.count; i++) {
-                    final ClassicHttpResponse response = this.httpclient.execute(this.target, this.request);
-                    final int status = response.getCode();
-                    if (status != 200) {
-                        throw new ClientProtocolException("Unexpected status code: " + status);
+                    final HttpGet httpget = new HttpGet(this.requestUri);
+                    try (final ClassicHttpResponse response = this.httpclient.execute(this.target, httpget)) {
+                        final int status = response.getCode();
+                        if (status != 200) {
+                            throw new ClientProtocolException("Unexpected status code: " + status);
+                        }
+                        EntityUtils.consume(response.getEntity());
+                        Thread.sleep(10);
                     }
-                    EntityUtils.consume(response.getEntity());
-                    Thread.sleep(10);
                 }
             } catch (final Exception ex) {
                 this.ex = ex;