-
Notifications
You must be signed in to change notification settings - Fork 13
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
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
http_proxy.go
Outdated
// 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() | ||
} | ||
|
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.
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
.
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.
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.
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.
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().
This reverts commit 05a1e14. See discussion #967 (comment)
d79700d
to
0686629
Compare
Shutdown tries to drain connections for 30s or until terminate signal is received, then they are closed.
Fixes #960