Skip to content

Commit 38877ae

Browse files
committed
nit
1 parent 8a15b62 commit 38877ae

File tree

4 files changed

+163
-121
lines changed

4 files changed

+163
-121
lines changed

client/src/main/java/org/asynchttpclient/netty/NettyResponseFuture.java

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,46 @@
4343
import org.slf4j.LoggerFactory;
4444

4545
/**
46-
* A {@link Future} that can be used to track when an asynchronous HTTP request has been fully processed.
46+
* A {@link Future} that can be used to track when an asynchronous HTTP request
47+
* has been fully processed.
4748
*
48-
* @param <V> the result type
49+
* @param <V>
50+
* the result type
4951
*/
5052
public final class NettyResponseFuture<V> implements ListenableFuture<V> {
5153

5254
private static final Logger LOGGER = LoggerFactory.getLogger(NettyResponseFuture.class);
5355

5456
@SuppressWarnings("rawtypes")
55-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> REDIRECT_COUNT_UPDATER = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "redirectCount");
57+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> REDIRECT_COUNT_UPDATER = AtomicIntegerFieldUpdater
58+
.newUpdater(NettyResponseFuture.class, "redirectCount");
5659
@SuppressWarnings("rawtypes")
57-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> CURRENT_RETRY_UPDATER = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "currentRetry");
60+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> CURRENT_RETRY_UPDATER = AtomicIntegerFieldUpdater
61+
.newUpdater(NettyResponseFuture.class, "currentRetry");
62+
@SuppressWarnings("rawtypes")
63+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> IS_DONE_FIELD = AtomicIntegerFieldUpdater
64+
.newUpdater(NettyResponseFuture.class, "isDone");
65+
@SuppressWarnings("rawtypes")
66+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> IS_CANCELLED_FIELD = AtomicIntegerFieldUpdater
67+
.newUpdater(NettyResponseFuture.class, "isCancelled");
68+
@SuppressWarnings("rawtypes")
69+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> IN_AUTH_FIELD = AtomicIntegerFieldUpdater
70+
.newUpdater(NettyResponseFuture.class, "inAuth");
71+
@SuppressWarnings("rawtypes")
72+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> IN_PROXY_AUTH_FIELD = AtomicIntegerFieldUpdater
73+
.newUpdater(NettyResponseFuture.class, "inProxyAuth");
74+
@SuppressWarnings("rawtypes")
75+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> CONTENT_PROCESSED_FIELD = AtomicIntegerFieldUpdater
76+
.newUpdater(NettyResponseFuture.class, "contentProcessed");
77+
@SuppressWarnings("rawtypes")
78+
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> ON_THROWABLE_CALLED_FIELD = AtomicIntegerFieldUpdater
79+
.newUpdater(NettyResponseFuture.class, "onThrowableCalled");
80+
@SuppressWarnings("rawtypes")
81+
private static final AtomicReferenceFieldUpdater<NettyResponseFuture, TimeoutsHolder> TIMEOUTS_HOLDER_FIELD = AtomicReferenceFieldUpdater
82+
.newUpdater(NettyResponseFuture.class, TimeoutsHolder.class, "timeoutsHolder");
83+
@SuppressWarnings("rawtypes")
84+
private static final AtomicReferenceFieldUpdater<NettyResponseFuture, Object> PARTITION_KEY_LOCK_FIELD = AtomicReferenceFieldUpdater
85+
.newUpdater(NettyResponseFuture.class, Object.class, "partitionKeyLock");
5886

5987
private final long start = unpreciseMillisTime();
6088
private final ChannelPoolPartitioning connectionPoolPartitioning;
@@ -79,26 +107,6 @@ public final class NettyResponseFuture<V> implements ListenableFuture<V> {
79107
// partition key, when != null used to release lock in ChannelManager
80108
private volatile Object partitionKeyLock;
81109

82-
@SuppressWarnings("rawtypes")
83-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> isDoneField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "isDone");
84-
@SuppressWarnings("rawtypes")
85-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> isCancelledField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "isCancelled");
86-
@SuppressWarnings("rawtypes")
87-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> inAuthField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "inAuth");
88-
@SuppressWarnings("rawtypes")
89-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> inProxyAuthField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "inProxyAuth");
90-
@SuppressWarnings("rawtypes")
91-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> contentProcessedField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class, "contentProcessed");
92-
@SuppressWarnings("rawtypes")
93-
private static final AtomicIntegerFieldUpdater<NettyResponseFuture> onThrowableCalledField = AtomicIntegerFieldUpdater.newUpdater(NettyResponseFuture.class,
94-
"onThrowableCalled");
95-
@SuppressWarnings("rawtypes")
96-
private static final AtomicReferenceFieldUpdater<NettyResponseFuture, TimeoutsHolder> timeoutsHolderField = AtomicReferenceFieldUpdater.newUpdater(NettyResponseFuture.class,
97-
TimeoutsHolder.class, "timeoutsHolder");
98-
@SuppressWarnings("rawtypes")
99-
private static final AtomicReferenceFieldUpdater<NettyResponseFuture, Object> partitionKeyLockField = AtomicReferenceFieldUpdater.newUpdater(NettyResponseFuture.class,
100-
Object.class, "partitionKeyLock");
101-
102110
// volatile where we need CAS ops
103111
private volatile int redirectCount = 0;
104112
private volatile int currentRetry = 0;
@@ -159,7 +167,7 @@ public Object takePartitionKeyLock() {
159167
return null;
160168
}
161169

162-
return partitionKeyLockField.getAndSet(this, null);
170+
return PARTITION_KEY_LOCK_FIELD.getAndSet(this, null);
163171
}
164172

165173
// java.util.concurrent.Future
@@ -179,7 +187,7 @@ public boolean cancel(boolean force) {
179187
releasePartitionKeyLock();
180188
cancelTimeouts();
181189

182-
if (isCancelledField.getAndSet(this, 1) != 0)
190+
if (IS_CANCELLED_FIELD.getAndSet(this, 1) != 0)
183191
return false;
184192

185193
// cancel could happen before channel was attached
@@ -188,7 +196,7 @@ public boolean cancel(boolean force) {
188196
Channels.silentlyCloseChannel(channel);
189197
}
190198

191-
if (onThrowableCalledField.getAndSet(this, 1) == 0) {
199+
if (ON_THROWABLE_CALLED_FIELD.getAndSet(this, 1) == 0) {
192200
try {
193201
asyncHandler.onThrowable(new CancellationException());
194202
} catch (Throwable t) {
@@ -221,11 +229,11 @@ private V getContent() throws ExecutionException {
221229

222230
// No more retry
223231
CURRENT_RETRY_UPDATER.set(this, maxRetry);
224-
if (contentProcessedField.getAndSet(this, 1) == 0) {
232+
if (CONTENT_PROCESSED_FIELD.getAndSet(this, 1) == 0) {
225233
try {
226234
future.complete(asyncHandler.onCompleted());
227235
} catch (Throwable ex) {
228-
if (onThrowableCalledField.getAndSet(this, 1) == 0) {
236+
if (ON_THROWABLE_CALLED_FIELD.getAndSet(this, 1) == 0) {
229237
try {
230238
try {
231239
asyncHandler.onThrowable(ex);
@@ -249,7 +257,7 @@ private boolean terminateAndExit() {
249257
cancelTimeouts();
250258
this.channel = null;
251259
this.reuseChannel = false;
252-
return isDoneField.getAndSet(this, 1) != 0 || isCancelled != 0;
260+
return IS_DONE_FIELD.getAndSet(this, 1) != 0 || isCancelled != 0;
253261
}
254262

255263
public final void done() {
@@ -276,7 +284,7 @@ public final void abort(final Throwable t) {
276284

277285
future.completeExceptionally(t);
278286

279-
if (onThrowableCalledField.compareAndSet(this, 0, 1)) {
287+
if (ON_THROWABLE_CALLED_FIELD.compareAndSet(this, 0, 1)) {
280288
try {
281289
asyncHandler.onThrowable(t);
282290
} catch (Throwable te) {
@@ -323,7 +331,7 @@ public void setAsyncHandler(AsyncHandler<V> asyncHandler) {
323331
}
324332

325333
public void cancelTimeouts() {
326-
TimeoutsHolder ref = timeoutsHolderField.getAndSet(this, null);
334+
TimeoutsHolder ref = TIMEOUTS_HOLDER_FIELD.getAndSet(this, null);
327335
if (ref != null) {
328336
ref.cancel();
329337
}
@@ -362,11 +370,11 @@ public int incrementAndGetCurrentRedirectCount() {
362370
}
363371

364372
public void setTimeoutsHolder(TimeoutsHolder timeoutsHolder) {
365-
timeoutsHolderField.set(this, timeoutsHolder);
373+
TIMEOUTS_HOLDER_FIELD.set(this, timeoutsHolder);
366374
}
367375

368376
public TimeoutsHolder getTimeoutsHolder() {
369-
return timeoutsHolderField.get(this);
377+
return TIMEOUTS_HOLDER_FIELD.get(this);
370378
}
371379

372380
public boolean isInAuth() {
@@ -378,7 +386,7 @@ public void setInAuth(boolean inAuth) {
378386
}
379387

380388
public boolean isAndSetInAuth(boolean set) {
381-
return inAuthField.getAndSet(this, set ? 1 : 0) != 0;
389+
return IN_AUTH_FIELD.getAndSet(this, set ? 1 : 0) != 0;
382390
}
383391

384392
public boolean isInProxyAuth() {
@@ -390,7 +398,7 @@ public void setInProxyAuth(boolean inProxyAuth) {
390398
}
391399

392400
public boolean isAndSetInProxyAuth(boolean inProxyAuth) {
393-
return inProxyAuthField.getAndSet(this, inProxyAuth ? 1 : 0) != 0;
401+
return IN_PROXY_AUTH_FIELD.getAndSet(this, inProxyAuth ? 1 : 0) != 0;
394402
}
395403

396404
public ChannelState getChannelState() {
@@ -473,21 +481,24 @@ public void setCurrentRequest(Request currentRequest) {
473481
}
474482

475483
/**
476-
* Return true if the {@link Future} can be recovered. There is some scenario where a connection can be closed by an unexpected IOException, and in some situation we can
477-
* recover from that exception.
484+
* Return true if the {@link Future} can be recovered. There is some scenario
485+
* where a connection can be closed by an unexpected IOException, and in some
486+
* situation we can recover from that exception.
478487
*
479488
* @return true if that {@link Future} cannot be recovered.
480489
*/
481490
public boolean isReplayPossible() {
482-
return !isDone() && !(Channels.isChannelValid(channel) && !getUri().getScheme().equalsIgnoreCase("https")) && inAuth == 0 && inProxyAuth == 0;
491+
return !isDone() && !(Channels.isChannelActive(channel) && !getUri().getScheme().equalsIgnoreCase("https"))
492+
&& inAuth == 0 && inProxyAuth == 0;
483493
}
484494

485495
public long getStart() {
486496
return start;
487497
}
488498

489499
public Object getPartitionKey() {
490-
return connectionPoolPartitioning.getPartitionKey(targetRequest.getUri(), targetRequest.getVirtualHost(), proxyServer);
500+
return connectionPoolPartitioning.getPartitionKey(targetRequest.getUri(), targetRequest.getVirtualHost(),
501+
proxyServer);
491502
}
492503

493504
public void acquirePartitionLockLazily() throws IOException {
@@ -497,7 +508,7 @@ public void acquirePartitionLockLazily() throws IOException {
497508

498509
Object partitionKey = getPartitionKey();
499510
connectionSemaphore.acquireChannelLock(partitionKey);
500-
Object prevKey = partitionKeyLockField.getAndSet(this, partitionKey);
511+
Object prevKey = PARTITION_KEY_LOCK_FIELD.getAndSet(this, partitionKey);
501512
if (prevKey != null) {
502513
// self-check
503514

@@ -541,7 +552,7 @@ public String toString() {
541552
",\n\turi=" + getUri() + //
542553
",\n\tkeepAlive=" + keepAlive + //
543554
",\n\tredirectCount=" + redirectCount + //
544-
",\n\ttimeoutsHolder=" + timeoutsHolderField.get(this) + //
555+
",\n\ttimeoutsHolder=" + TIMEOUTS_HOLDER_FIELD.get(this) + //
545556
",\n\tinAuth=" + inAuth + //
546557
",\n\tstatusReceived=" + statusReceived + //
547558
",\n\ttouch=" + touch + //

client/src/main/java/org/asynchttpclient/netty/channel/Channels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static void setDiscard(Channel channel) {
4343
setAttribute(channel, DiscardEvent.DISCARD);
4444
}
4545

46-
public static boolean isChannelValid(Channel channel) {
46+
public static boolean isChannelActive(Channel channel) {
4747
return channel != null && channel.isActive();
4848
}
4949

client/src/main/java/org/asynchttpclient/netty/handler/intercept/Redirect30xInterceptor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ else if (request.getBodyGenerator() != null)
146146
LOGGER.debug("Sending redirect to {}", newUri);
147147

148148
if (future.isKeepAlive() && !HttpUtil.isTransferEncodingChunked(response)) {
149-
150149
if (sameBase) {
151150
future.setReuseChannel(true);
152151
// we can't directly send the next request because we still have to received LastContent

0 commit comments

Comments
 (0)