diff options
author | 2022-09-19 13:43:22 +0200 | |
---|---|---|
committer | 2022-09-19 12:43:22 +0100 | |
commit | 3777f5c68448992a6ed8230f40713d3b31da0413 (patch) | |
tree | 3b56e932503b2dec6ca613e5d8abcfe074b4dfc2 /internal/router | |
parent | [bugfix] Fix domains not being unblockable, log internal server errors from A... (diff) | |
download | gotosocial-3777f5c68448992a6ed8230f40713d3b31da0413.tar.xz |
[bugfix] Server and closer bugfixes (#839)
* defer streaming from storage more forcefully
* shut down Server more gracefully
* use command context as server BaseContext
Diffstat (limited to 'internal/router')
-rw-r--r-- | internal/router/router.go | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/internal/router/router.go b/internal/router/router.go index da00b685d..d66e5f9cd 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -21,6 +21,7 @@ package router import ( "context" "fmt" + "net" "net/http" "time" @@ -37,6 +38,7 @@ const ( writeTimeout = 30 * time.Second idleTimeout = 30 * time.Second readHeaderTimeout = 30 * time.Second + shutdownTimeout = 30 * time.Second ) // Router provides the REST interface for gotosocial, using gin. @@ -128,7 +130,16 @@ func (r *router) Start() { // Stop shuts down the router nicely func (r *router) Stop(ctx context.Context) error { - return r.srv.Shutdown(ctx) + log.Infof("shutting down http router with %s grace period", shutdownTimeout) + timeout, cancel := context.WithTimeout(ctx, shutdownTimeout) + defer cancel() + + if err := r.srv.Shutdown(timeout); err != nil { + return fmt.Errorf("error shutting down http router: %s", err) + } + + log.Info("http router closed connections and shut down gracefully") + return nil } // New returns a new Router with the specified configuration. @@ -174,17 +185,24 @@ func New(ctx context.Context, db db.DB) (Router, error) { return nil, err } - // create the http server here, passing the gin engine as handler + // use the passed-in command context as the base context for the server, + // since we'll never want the server to live past the command anyway + baseCtx := func(_ net.Listener) context.Context { + return ctx + } + bindAddress := config.GetBindAddress() port := config.GetPort() - listen := fmt.Sprintf("%s:%d", bindAddress, port) + addr := fmt.Sprintf("%s:%d", bindAddress, port) + s := &http.Server{ - Addr: listen, - Handler: engine, + Addr: addr, + Handler: engine, // use gin engine as handler ReadTimeout: readTimeout, + ReadHeaderTimeout: readHeaderTimeout, WriteTimeout: writeTimeout, IdleTimeout: idleTimeout, - ReadHeaderTimeout: readHeaderTimeout, + BaseContext: baseCtx, } // We need to spawn the underlying server slightly differently depending on whether lets encrypt is enabled or not. |