-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1412 Wait for async close tasks before close completes #1435
base: main
Are you sure you want to change the base?
Changes from 4 commits
c0af1eb
88ce96d
85f30e2
3bb5ae4
7026fc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,10 @@ | |
import static org.asynchttpclient.util.Assertions.assertNotNull; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.util.HashedWheelTimer; | ||
import io.netty.util.ThreadDeathWatcher; | ||
import io.netty.util.Timer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Predicate; | ||
|
||
|
@@ -42,6 +44,7 @@ public class DefaultAsyncHttpClient implements AsyncHttpClient { | |
|
||
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultAsyncHttpClient.class); | ||
private final AsyncHttpClientConfig config; | ||
private final AtomicBoolean closeTriggered = new AtomicBoolean(false); | ||
private final AtomicBoolean closed = new AtomicBoolean(false); | ||
private final ChannelManager channelManager; | ||
private final ConnectionSemaphore connectionSemaphore; | ||
|
@@ -87,7 +90,7 @@ public DefaultAsyncHttpClient(AsyncHttpClientConfig config) { | |
|
||
channelManager = new ChannelManager(config, nettyTimer); | ||
connectionSemaphore = new ConnectionSemaphore(config); | ||
requestSender = new NettyRequestSender(config, channelManager, connectionSemaphore, nettyTimer, new AsyncHttpClientState(closed)); | ||
requestSender = new NettyRequestSender(config, channelManager, connectionSemaphore, nettyTimer, new AsyncHttpClientState(closeTriggered)); | ||
channelManager.configureBootstraps(requestSender); | ||
} | ||
|
||
|
@@ -99,7 +102,7 @@ private Timer newNettyTimer() { | |
|
||
@Override | ||
public void close() { | ||
if (closed.compareAndSet(false, true)) { | ||
if (closeTriggered.compareAndSet(false, true)) { | ||
try { | ||
channelManager.close(); | ||
} catch (Throwable t) { | ||
|
@@ -112,6 +115,15 @@ public void close() { | |
LOGGER.warn("Unexpected error on HashedWheelTimer close", t); | ||
} | ||
} | ||
|
||
//see https://github.com/netty/netty/issues/2084#issuecomment-44822314 | ||
try { | ||
ThreadDeathWatcher.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); | ||
} catch(InterruptedException t) { | ||
// Ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-assert interrupted status |
||
} | ||
|
||
closed.compareAndSet(false, true); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
import java.net.InetSocketAddress; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
|
@@ -99,11 +100,15 @@ public class ChannelManager { | |
private final ChannelPool channelPool; | ||
private final ChannelGroup openChannels; | ||
|
||
private final CountDownLatch closeLatch; | ||
|
||
private AsyncHttpClientHandler wsHandler; | ||
|
||
public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) { | ||
|
||
this.config = config; | ||
|
||
closeLatch = new CountDownLatch(2); | ||
|
||
this.sslEngineFactory = config.getSslEngineFactory() != null ? config.getSslEngineFactory() : new DefaultSslEngineFactory(); | ||
try { | ||
|
@@ -300,16 +305,30 @@ public boolean removeAll(Channel connection) { | |
} | ||
|
||
private void doClose() { | ||
openChannels.close(); | ||
openChannels.close().addListener(future -> closeLatch.countDown()); | ||
channelPool.destroy(); | ||
|
||
//see https://github.com/netty/netty/issues/2084#issuecomment-44822314 | ||
try { | ||
GlobalEventExecutor.INSTANCE.awaitInactivity(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); | ||
} catch(InterruptedException t) { | ||
// Ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-assert interrupted status |
||
} finally { | ||
closeLatch.countDown(); | ||
} | ||
} | ||
|
||
public void close() { | ||
if (allowReleaseEventLoopGroup) { | ||
eventLoopGroup.shutdownGracefully(config.getShutdownQuietPeriod(), config.getShutdownTimeout(), TimeUnit.MILLISECONDS)// | ||
.addListener(future -> doClose()); | ||
} else { | ||
} else | ||
doClose(); | ||
|
||
try { | ||
closeLatch.await(config.getShutdownTimeout(), TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException e) { | ||
// Ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-assert interrupted status |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,7 +597,7 @@ public void replayRequest(final NettyResponseFuture<?> future, FilterContext fc, | |
} | ||
|
||
public boolean isClosed() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename method too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done and attached another commit to the pull request! |
||
return clientState.isClosed(); | ||
return clientState.isCloseTriggered(); | ||
} | ||
|
||
public void drainChannelAndExecuteNextRequest(final Channel channel, final NettyResponseFuture<?> future, Request nextRequest) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong with the old name?