define Server.BaseContext to cancel globally the SSE connections on server shutdown
This commit is contained in:
parent
506b759560
commit
35fc6d0734
|
@ -78,7 +78,7 @@
|
||||||
- Trigger the `app.OnTerminate()` hook on `app.Restart()` call.
|
- Trigger the `app.OnTerminate()` hook on `app.Restart()` call.
|
||||||
_A new bool `IsRestart` field was also added to the `core.TerminateEvent` event._
|
_A new bool `IsRestart` field was also added to the `core.TerminateEvent` event._
|
||||||
|
|
||||||
- Fixed the graceful shutdown handling.
|
- Fixed graceful shutdown handling and speed up a little the app termination time.
|
||||||
|
|
||||||
|
|
||||||
## v0.20.0-rc3
|
## v0.20.0-rc3
|
||||||
|
|
|
@ -140,6 +140,11 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) {
|
||||||
HostPolicy: autocert.HostWhitelist(hostNames...),
|
HostPolicy: autocert.HostWhitelist(hostNames...),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// base request context used for cancelling long running requests
|
||||||
|
// like the SSE connections
|
||||||
|
baseCtx, cancelBaseCtx := context.WithCancel(context.Background())
|
||||||
|
defer cancelBaseCtx()
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
TLSConfig: &tls.Config{
|
TLSConfig: &tls.Config{
|
||||||
MinVersion: tls.VersionTLS12,
|
MinVersion: tls.VersionTLS12,
|
||||||
|
@ -151,6 +156,9 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) {
|
||||||
// WriteTimeout: 60 * time.Second, // breaks sse!
|
// WriteTimeout: 60 * time.Second, // breaks sse!
|
||||||
Handler: router,
|
Handler: router,
|
||||||
Addr: mainAddr,
|
Addr: mainAddr,
|
||||||
|
BaseContext: func(l net.Listener) context.Context {
|
||||||
|
return baseCtx
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
serveEvent := &core.ServeEvent{
|
serveEvent := &core.ServeEvent{
|
||||||
|
@ -196,14 +204,16 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) {
|
||||||
|
|
||||||
// try to gracefully shutdown the server on app termination
|
// try to gracefully shutdown the server on app termination
|
||||||
app.OnTerminate().Add(func(e *core.TerminateEvent) error {
|
app.OnTerminate().Add(func(e *core.TerminateEvent) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
cancelBaseCtx()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
server.Shutdown(ctx)
|
server.Shutdown(ctx)
|
||||||
if e.IsRestart {
|
if e.IsRestart {
|
||||||
// wait for execve up to 3 seconds before exit
|
// wait for execve and other handlers up to 5 seconds before exit
|
||||||
time.AfterFunc(3*time.Second, func() {
|
time.AfterFunc(5*time.Second, func() {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue