|
21 | 21 | import io.netty.util.HashedWheelTimer;
|
22 | 22 | import io.netty.util.ThreadDeathWatcher;
|
23 | 23 | import io.netty.util.Timer;
|
| 24 | +import io.netty.util.concurrent.GlobalEventExecutor; |
24 | 25 |
|
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ExecutionException; |
25 | 28 | import java.util.concurrent.TimeUnit;
|
| 29 | +import java.util.concurrent.TimeoutException; |
26 | 30 | import java.util.concurrent.atomic.AtomicBoolean;
|
27 | 31 | import java.util.function.Predicate;
|
28 | 32 |
|
@@ -102,31 +106,60 @@ private Timer newNettyTimer() {
|
102 | 106 |
|
103 | 107 | @Override
|
104 | 108 | public void close() {
|
| 109 | + closeInternal(false); |
| 110 | + } |
| 111 | + |
| 112 | + public void closeAndAwaitInactivity() { |
| 113 | + closeInternal(true); |
| 114 | + } |
| 115 | + |
| 116 | + private void closeInternal(boolean awaitInactivity) { |
105 | 117 | if (closeTriggered.compareAndSet(false, true)) {
|
106 |
| - try { |
107 |
| - channelManager.close(); |
108 |
| - } catch (Throwable t) { |
109 |
| - LOGGER.warn("Unexpected error on ChannelManager close", t); |
110 |
| - } |
111 |
| - if (allowStopNettyTimer) { |
112 |
| - try { |
113 |
| - nettyTimer.stop(); |
114 |
| - } catch (Throwable t) { |
115 |
| - LOGGER.warn("Unexpected error on HashedWheelTimer close", t); |
| 118 | + CompletableFuture<Void> handledCloseFuture = channelManager.close().whenComplete((v, t) -> { |
| 119 | + if(t != null) { |
| 120 | + LOGGER.warn("Unexpected error on ChannelManager close", t); |
| 121 | + } |
| 122 | + if (allowStopNettyTimer) { |
| 123 | + try { |
| 124 | + nettyTimer.stop(); |
| 125 | + } catch (Throwable th) { |
| 126 | + LOGGER.warn("Unexpected error on HashedWheelTimer close", th); |
| 127 | + } |
116 | 128 | }
|
| 129 | + }); |
| 130 | + |
| 131 | + if(awaitInactivity) { |
| 132 | + handledCloseFuture = handledCloseFuture.thenCombine(awaitInactivity(), (v1,v2) -> null) ; |
117 | 133 | }
|
118 |
| - |
119 |
| - //see https://github.com/netty/netty/issues/2084#issuecomment-44822314 |
| 134 | + |
120 | 135 | try {
|
121 |
| - ThreadDeathWatcher.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); |
122 |
| - } catch(InterruptedException t) { |
123 |
| - // Ignore |
| 136 | + handledCloseFuture.get(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); |
| 137 | + } catch (InterruptedException | TimeoutException t) { |
| 138 | + LOGGER.warn("Unexpected error on AsyncHttpClient close", t); |
| 139 | + } catch (ExecutionException e) { |
| 140 | + // already handled and could be ignored |
124 | 141 | }
|
125 |
| - |
126 | 142 | closed.compareAndSet(false, true);
|
127 | 143 | }
|
128 | 144 | }
|
129 | 145 |
|
| 146 | + private CompletableFuture<Void> awaitInactivity() { |
| 147 | + //see https://github.com/netty/netty/issues/2084#issuecomment-44822314 |
| 148 | + CompletableFuture<Void> wait1 = CompletableFuture.runAsync(() -> { |
| 149 | + try { |
| 150 | + GlobalEventExecutor.INSTANCE.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); |
| 151 | + } catch(InterruptedException t) { |
| 152 | + // Ignore |
| 153 | + }}); |
| 154 | + CompletableFuture<Void> wait2 = CompletableFuture.runAsync(() -> { |
| 155 | + try { |
| 156 | + ThreadDeathWatcher.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); |
| 157 | + } catch(InterruptedException t) { |
| 158 | + // Ignore |
| 159 | + }}); |
| 160 | + return wait1.thenCombine(wait2, (v1,v2) -> null); |
| 161 | + } |
| 162 | + |
130 | 163 | @Override
|
131 | 164 | public boolean isClosed() {
|
132 | 165 | return closed.get();
|
|
0 commit comments