-
Notifications
You must be signed in to change notification settings - Fork 24
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
Close active connections while stopping an HTTPServer #218
Conversation
@pushkarnk are you using NIO's quiescing helper? |
here's a super primitive HTTP server that will correctly quiesce: https://github.com/apple/swift-nio-extras/blob/master/Sources/HTTPServerWithQuiescingDemo/main.swift When you tell it to stop (in the example that's triggered by Ctrl+C ( The bulk of the work is done in |
@weissi Thanks for the references. Quiescing seems like the right thing to do during an untimely I guess Kitura-net does not quiesce. It does an abrupt closure of all the connections resulting in the sending of RST packets amidst data transfers. |
@pushkarnk you can implement abrupt closure too. Key is to collect all channels. You could copy the code for the quiescing handler and just close stuff instead of sending the quiescing event. Would only need to replace this line : https://github.com/apple/swift-nio-extras/blob/master/Sources/NIOExtras/QuiescingHelper.swift#L114 With |
@weissi Does closing an |
@pushkarnk yes, it does, but generally you’re meant to able to share EventLoops. Shutting down EventLoops is a bit brutal as it kills the execution environment. If everybody implements shutdown by piggybacking on the EventLoop’s shutdown mechanism, bad things will happen. So it’s good style to be able to shut down without the EL going away. |
Also, an EventLoop that’s shutting down is kinda dangerous, if you |
I get your point. Thanks! |
Also eventually you’ll want to quiesce anyway, so why not implement something that’s pretty much quiescing already and when you’re ready you can switch to quiescing at any point in time. Do you need 100% bug for bug compat with kitura-net? |
In this case, we'd prefer to do a graceful shutdown in Kitura-net too, because that seems like the right thing to do. |
I think that's the right call. |
f9e32db
to
c72da0b
Compare
Fixes #216 |
c72da0b
to
18d43e8
Compare
Hi @weissi As suggested |
hey @pushkarnk is the code from NIO modified at all? |
Oh, no, I could actually add swift-nio-extras as a dependency |
If the HTTPServer is stopped while some connections are active, the connections need to be closed in the `stop()` method. Currently, the stop() method only closes the listening channel. The child channels, representing the active connections, also need to be closed. The right way to do this is to allow the ongoing transfers to complete and gracefully close these channels. To implement this, we have borrowed the `ServerQuiescingHelper` code from `NIOExtras`. On a server stop event, the server channel is closed. The open child channels that represent active connections are then collected, they are allowed to serve outstanding requests and then shutdown once they are all done. This also ensures a graceful connection shutdown on the clients. Initialize the quiescing helper in `listen()` An HTTPServer.stop() does not destroy the server. It only stops it. The server can be restarted by calling listen(). This means the right place to initialize a quiescing helper is in the beginning of the listen() implementation.
18d43e8
to
cf0f23e
Compare
If the HTTPServer is stopped while some connections are active, the connections
need to be closed in the
stop()
method. Currently, the stop() method onlycloses the listening channel. The child channels, representing the active
connections, also need to be closed. The right way to do this is to allow the
ongoing transfers to complete and gracefully close these channels. To implement
this, we have borrowed the
ServerQuiescingHelper
code fromNIOExtras
. On aserver stop event, the server channel is closed. The open child channels that
represent active connections are then collected, they are allowed to serve
outstanding requests and then shutdown once they are all done. This also
ensures a graceful connection shutdown on the clients.