Improved handling of request cancellation
This commit is contained in:
parent
779bb8c53b
commit
4365c5242d
|
@ -27,8 +27,7 @@
|
||||||
package org.apache.http.client.methods;
|
package org.apache.http.client.methods;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicMarkableReference;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import org.apache.http.HttpRequest;
|
import org.apache.http.HttpRequest;
|
||||||
import org.apache.http.client.utils.CloneUtils;
|
import org.apache.http.client.utils.CloneUtils;
|
||||||
|
@ -41,13 +40,11 @@ import org.apache.http.message.AbstractHttpMessage;
|
||||||
public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
|
public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
|
||||||
HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
|
HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
|
||||||
|
|
||||||
private final AtomicBoolean aborted;
|
private final AtomicMarkableReference<Cancellable> cancellableRef;
|
||||||
private final AtomicReference<Cancellable> cancellableRef;
|
|
||||||
|
|
||||||
protected AbstractExecutionAwareRequest() {
|
protected AbstractExecutionAwareRequest() {
|
||||||
super();
|
super();
|
||||||
this.aborted = new AtomicBoolean(false);
|
this.cancellableRef = new AtomicMarkableReference<Cancellable>(null, false);
|
||||||
this.cancellableRef = new AtomicReference<Cancellable>(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,17 +87,19 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void abort() {
|
public void abort() {
|
||||||
if (this.aborted.compareAndSet(false, true)) {
|
while (!cancellableRef.isMarked()) {
|
||||||
final Cancellable cancellable = this.cancellableRef.getAndSet(null);
|
final Cancellable actualCancellable = cancellableRef.getReference();
|
||||||
if (cancellable != null) {
|
if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
|
||||||
cancellable.cancel();
|
if (actualCancellable != null) {
|
||||||
|
actualCancellable.cancel();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAborted() {
|
public boolean isAborted() {
|
||||||
return this.aborted.get();
|
return this.cancellableRef.isMarked();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,8 +107,9 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setCancellable(final Cancellable cancellable) {
|
public void setCancellable(final Cancellable cancellable) {
|
||||||
if (!this.aborted.get()) {
|
final Cancellable actualCancellable = cancellableRef.getReference();
|
||||||
this.cancellableRef.set(cancellable);
|
if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
|
||||||
|
cancellable.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,9 +123,12 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
|
*
|
||||||
|
* @deprecated Do not use.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public void completed() {
|
public void completed() {
|
||||||
this.cancellableRef.set(null);
|
this.cancellableRef.set(null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,11 +137,16 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public void reset() {
|
public void reset() {
|
||||||
final Cancellable cancellable = this.cancellableRef.getAndSet(null);
|
for (;;) {
|
||||||
if (cancellable != null) {
|
final boolean marked = cancellableRef.isMarked();
|
||||||
cancellable.cancel();
|
final Cancellable actualCancellable = cancellableRef.getReference();
|
||||||
|
if (actualCancellable != null) {
|
||||||
|
actualCancellable.cancel();
|
||||||
|
}
|
||||||
|
if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.aborted.set(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue