On shutdown try to drain connections for 30s and close them#967
Merged
On shutdown try to drain connections for 30s and close them#967
Conversation
Shutdown can be aborted by timeout or signal.
Add flags for servers and proxy.
--shutdown-timeout <duration> (default 30s) (env FORWARDER_SHUTDOWN_TIMEOUT)
The maximum amount of time to wait for the server to drain connections before closing. Zero means no limit.
Listener is created in constructor, allow to destroy it. HTTP server is managed entirely by Run().
Shutdown() is aborted on context cancellation, it can be called multiple times.
Example: 2024/11/27 13:19:18.662313 [proxy] [DEBUG] [8-f0c13c00] switched protocols, proxying websocket traffic ^C2024/11/27 13:19:23.007224 [api] [DEBUG] server was shutdown gracefully 2024/11/27 13:19:23.007344 [proxy] [DEBUG] listener closed, returning 2024/11/27 13:19:23.007371 [proxy] [INFO] shutting down proxy, draining connections ^C2024/11/27 13:19:26.263814 [proxy] [DEBUG] failed to gracefully shutdown server error=context canceled 2024/11/27 13:19:26.263975 [proxy] [DEBUG] connection closed prematurely while reading request: read tcp 127.0.0.1:3128->127.0.0.1:54623: use of closed network connection 2024/11/27 13:19:26.263981 [proxy] [DEBUG] closing connection from 127.0.0.1:54623 duration=10.93750725s 2024/11/27 13:19:26.264084 [proxy] [DEBUG] [8-f0c13c00] upstream websocket tunnel finished copying
d2f08d4 to
dbf5077
Compare
Choraden
reviewed
Nov 28, 2024
Comment on lines
-631
to
-647
| // Close listeners first to prevent new connections. | ||
| var err error | ||
| for _, l := range hp.listeners { | ||
| if e := l.Close(); e != nil { | ||
| err = multierr.Append(err, e) | ||
| } | ||
| } | ||
|
|
||
| // Shutdown the proxy to stop serving requests. | ||
| if e := hp.proxy.Shutdown(context.Background()); e != nil { | ||
| err = multierr.Append(err, e) | ||
| } | ||
|
|
||
| if tr, ok := hp.transport.(*http.Transport); ok { | ||
| tr.CloseIdleConnections() | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
So now there is a Close that closes listeners, but user still needs to cancel ctx.
Let's hide Close method and use in it Run. This way all workflow will be in Run.
Contributor
Author
There was a problem hiding this comment.
After implementing it I think the Close method gives users possibility for cleanup without relying on GC. It's actually kind of sad that the variable is not freed when it goes out of scope i.e. test function ends but that's how it is.
I'm reverting the last commit.
Listeners are created in constructor and closed in finalizer, specified in constructor. This is the 2nd time they are closed, the first close is issued on Shutdown().
This reverts commit 05a1e14. See discussion #967 (comment)
d79700d to
0686629
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Shutdown tries to drain connections for 30s or until terminate signal is received, then they are closed.
Fixes #960