Skip to content
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

On shutdown try to drain connections for 30s and close them #967

Merged
merged 10 commits into from
Nov 28, 2024

Conversation

mmatczuk
Copy link
Contributor

Shutdown tries to drain connections for 30s or until terminate signal is received, then they are closed.

Fixes #960

@mmatczuk mmatczuk requested a review from Choraden as a code owner November 27, 2024 13:50
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
http_proxy.go Outdated
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()
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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().
@mmatczuk mmatczuk merged commit 90c9f5b into main Nov 28, 2024
6 checks passed
@mmatczuk mmatczuk deleted the mmt/shutdown branch November 28, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

systemctl restart and systemctl stop seems to hang on ubuntu 24.04 if websockets are in use
2 participants