diff --git a/CHANGELOG.md b/CHANGELOG.md index d4b5959b..1dc2ab20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,7 +78,7 @@ - Trigger the `app.OnTerminate()` hook on `app.Restart()` call. _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 diff --git a/apis/serve.go b/apis/serve.go index c4c17290..1984c131 100644 --- a/apis/serve.go +++ b/apis/serve.go @@ -140,6 +140,11 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) { 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{ TLSConfig: &tls.Config{ MinVersion: tls.VersionTLS12, @@ -151,6 +156,9 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) { // WriteTimeout: 60 * time.Second, // breaks sse! Handler: router, Addr: mainAddr, + BaseContext: func(l net.Listener) context.Context { + return baseCtx + }, } 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 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() wg.Add(1) server.Shutdown(ctx) if e.IsRestart { - // wait for execve up to 3 seconds before exit - time.AfterFunc(3*time.Second, func() { + // wait for execve and other handlers up to 5 seconds before exit + time.AfterFunc(5*time.Second, func() { wg.Done() }) } else {