Skip to content

Commit 5e08297

Browse files
author
Stephane Landelle
committed
Have one common way of closing channels: silentlyCloseChannel
1 parent 56813f7 commit 5e08297

File tree

8 files changed

+46
-73
lines changed

8 files changed

+46
-73
lines changed

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/ChannelManager.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,9 @@ public void close() {
309309
public void closeChannel(Channel channel) {
310310

311311
LOGGER.debug("Closing Channel {} ", channel);
312-
try {
313-
removeAll(channel);
314-
Channels.setDiscard(channel);
315-
channel.close();
316-
} catch (Throwable t) {
317-
LOGGER.debug("Error closing a connection", t);
318-
}
312+
removeAll(channel);
313+
Channels.setDiscard(channel);
314+
Channels.silentlyCloseChannel(channel);
319315
openChannels.remove(channel);
320316
}
321317

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import io.netty.util.AttributeKey;
1919

2020
import org.asynchttpclient.providers.netty.DiscardEvent;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

2224
public class Channels {
2325

26+
private static final Logger LOGGER = LoggerFactory.getLogger(Channels.class);
27+
2428
private static final AttributeKey<Object> DEFAULT_ATTRIBUTE = AttributeKey.valueOf("default");
2529

2630
public static Object getAttribute(Channel channel) {
@@ -37,6 +41,15 @@ public static void setDiscard(Channel channel) {
3741
}
3842

3943
public static boolean isChannelValid(Channel channel) {
40-
return channel != null && channel.isOpen() && channel.isActive();
44+
return channel != null && channel.isActive();
45+
}
46+
47+
public static void silentlyCloseChannel(Channel channel) {
48+
try {
49+
if (channel != null && channel.isActive())
50+
channel.close();
51+
} catch (Throwable t) {
52+
LOGGER.debug("Failed to close channel", t);
53+
}
4154
}
4255
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/CleanupChannelGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean add(Channel channel) {
8989
try {
9090
if (this.closed.get()) {
9191
// Immediately close channel, as close() was already called.
92-
channel.close();
92+
Channels.silentlyCloseChannel(channel);
9393
return false;
9494
}
9595

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/pool/DefaultChannelPool.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.ArrayList;
2323
import java.util.Collections;
2424
import java.util.List;
25-
import java.util.Map;
2625
import java.util.concurrent.ConcurrentHashMap;
2726
import java.util.concurrent.ConcurrentLinkedQueue;
2827
import java.util.concurrent.TimeUnit;
@@ -315,13 +314,9 @@ public void destroy() {
315314
}
316315

317316
private void close(Channel channel) {
318-
try {
319-
// FIXME pity to have to do this here
320-
Channels.setDiscard(channel);
321-
channel2Creation.remove(channel);
322-
channel.close();
323-
} catch (Throwable t) {
324-
// noop
325-
}
317+
// FIXME pity to have to do this here
318+
Channels.setDiscard(channel);
319+
channel2Creation.remove(channel);
320+
Channels.silentlyCloseChannel(channel);
326321
}
327-
}
322+
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/future/NettyResponseFuture.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ public boolean cancel(boolean force) {
132132
if (isCancelled.getAndSet(true))
133133
return false;
134134

135-
try {
136-
Channels.setDiscard(channel);
137-
channel.close();
138-
} catch (Throwable t) {
139-
// Ignore
140-
}
135+
Channels.setDiscard(channel);
136+
Channels.silentlyCloseChannel(channel);
137+
141138
if (!onThrowableCalled.getAndSet(true)) {
142139
try {
143140
asyncHandler.onThrowable(new CancellationException());

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/Processor.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce
7676
protocol.handle(channel, future, msg);
7777

7878
} else if (attribute != DiscardEvent.INSTANCE) {
79-
try {
80-
LOGGER.trace("Closing an orphan channel {}", channel);
81-
channel.close();
82-
} catch (Throwable t) {
83-
}
79+
LOGGER.trace("Closing an orphan channel {}", channel);
80+
Channels.silentlyCloseChannel(channel);
8481
}
8582
}
8683

@@ -146,14 +143,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Excep
146143

147144
// FIXME why drop the original exception and throw a new one?
148145
if (!config.getIOExceptionFilters().isEmpty()) {
149-
if (!requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel)) {
146+
if (!requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel))
150147
// Close the channel so the recovering can occurs.
151-
try {
152-
channel.close();
153-
} catch (Throwable t) {
154-
// Swallow.
155-
}
156-
}
148+
Channels.silentlyCloseChannel(channel);
157149
return;
158150
}
159151
}
@@ -181,6 +173,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Excep
181173
channelManager.closeChannel(channel);
182174
// FIXME not really sure
183175
// ctx.fireChannelRead(e);
184-
ctx.close();
176+
Channels.silentlyCloseChannel(channel);
185177
}
186178
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/NettyRequestSender.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,7 @@ public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
313313
} catch (Throwable cause) {
314314
// FIXME why not notify?
315315
LOGGER.debug(cause.getMessage(), cause);
316-
try {
317-
channel.close();
318-
} catch (RuntimeException ex) {
319-
LOGGER.debug(ex.getMessage(), ex);
320-
}
316+
Channels.silentlyCloseChannel(channel);
321317
return;
322318
}
323319
}
@@ -326,11 +322,7 @@ public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
326322
nettyRequest.getBody().write(channel, future, config);
327323

328324
} catch (Throwable ioe) {
329-
try {
330-
channel.close();
331-
} catch (RuntimeException ex) {
332-
LOGGER.debug(ex.getMessage(), ex);
333-
}
325+
Channels.silentlyCloseChannel(channel);
334326
}
335327

336328
scheduleTimeouts(future);

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/ProgressListener.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@
1313
*/
1414
package org.asynchttpclient.providers.netty.request;
1515

16+
import io.netty.channel.Channel;
17+
import io.netty.channel.ChannelProgressiveFuture;
18+
import io.netty.channel.ChannelProgressiveFutureListener;
19+
20+
import java.nio.channels.ClosedChannelException;
21+
1622
import org.asynchttpclient.AsyncHandler;
1723
import org.asynchttpclient.AsyncHttpClientConfig;
1824
import org.asynchttpclient.ProgressAsyncHandler;
1925
import org.asynchttpclient.Realm;
26+
import org.asynchttpclient.providers.netty.channel.Channels;
2027
import org.asynchttpclient.providers.netty.future.NettyResponseFuture;
2128
import org.asynchttpclient.providers.netty.future.StackTraceInspector;
2229
import org.slf4j.Logger;
2330
import org.slf4j.LoggerFactory;
2431

25-
import io.netty.channel.Channel;
26-
import io.netty.channel.ChannelProgressiveFuture;
27-
import io.netty.channel.ChannelProgressiveFutureListener;
28-
29-
import java.nio.channels.ClosedChannelException;
30-
3132
public class ProgressListener implements ChannelProgressiveFutureListener {
3233

3334
private static final Logger LOGGER = LoggerFactory.getLogger(ProgressListener.class);
@@ -54,28 +55,17 @@ public ProgressListener(AsyncHttpClientConfig config,//
5455
private boolean abortOnThrowable(Throwable cause, Channel channel) {
5556

5657
if (cause != null && future.getState() != NettyResponseFuture.STATE.NEW) {
57-
5858
if (cause instanceof IllegalStateException) {
5959
LOGGER.debug(cause.getMessage(), cause);
60-
try {
61-
channel.close();
62-
} catch (RuntimeException ex) {
63-
LOGGER.debug(ex.getMessage(), ex);
64-
}
65-
} else if (cause instanceof ClosedChannelException || StackTraceInspector.abortOnReadOrWriteException(cause)) {
66-
67-
if (LOGGER.isDebugEnabled())
68-
LOGGER.debug(cause.getMessage(), cause);
60+
Channels.silentlyCloseChannel(channel);
6961

70-
try {
71-
channel.close();
72-
} catch (RuntimeException ex) {
73-
LOGGER.debug(ex.getMessage(), ex);
74-
}
62+
} else if (cause instanceof ClosedChannelException || StackTraceInspector.abortOnReadOrWriteException(cause)) {
63+
LOGGER.debug(cause.getMessage(), cause);
64+
Channels.silentlyCloseChannel(channel);
65+
7566
} else {
7667
future.abort(cause);
7768
}
78-
7969
return true;
8070
}
8171

@@ -84,10 +74,8 @@ private boolean abortOnThrowable(Throwable cause, Channel channel) {
8474

8575
@Override
8676
public void operationComplete(ChannelProgressiveFuture cf) {
87-
// The write operation failed. If the channel was cached, it means it
88-
// got asynchronously closed.
77+
// The write operation failed. If the channel was cached, it means it got asynchronously closed.
8978
// Let's retry a second time.
90-
9179
if (!abortOnThrowable(cf.cause(), cf.channel())) {
9280

9381
future.touch();

0 commit comments

Comments
 (0)