diff options
author | Runxi Yu <me@runxiyu.org> | 2025-08-17 04:34:16 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-08-17 04:35:17 +0800 |
commit | 308ce6c10ce77835a9b4d2ca9a17d449260c6adb (patch) | |
tree | ead254909f04c22973536db7b4a0bb5da10917e3 /forged/internal/incoming/web/web.go | |
parent | Fix a few other goroutine leaks with contexts (diff) | |
download | forge-308ce6c10ce77835a9b4d2ca9a17d449260c6adb.tar.gz forge-308ce6c10ce77835a9b4d2ca9a17d449260c6adb.tar.zst forge-308ce6c10ce77835a9b4d2ca9a17d449260c6adb.zip |
Add shutdown timeouts
Diffstat (limited to 'forged/internal/incoming/web/web.go')
-rw-r--r-- | forged/internal/incoming/web/web.go | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/forged/internal/incoming/web/web.go b/forged/internal/incoming/web/web.go index f66ad64..391f6ff 100644 --- a/forged/internal/incoming/web/web.go +++ b/forged/internal/incoming/web/web.go @@ -10,10 +10,11 @@ import ( ) type Server struct { - net string - addr string - root string - httpServer *http.Server + net string + addr string + root string + httpServer *http.Server + shutdownTimeout uint32 } type handler struct{} @@ -22,23 +23,25 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } type Config struct { - Net string `scfg:"net"` - Addr string `scfg:"addr"` - Root string `scfg:"root"` - CookieExpiry int `scfg:"cookie_expiry"` - ReadTimeout uint32 `scfg:"read_timeout"` - WriteTimeout uint32 `scfg:"write_timeout"` - IdleTimeout uint32 `scfg:"idle_timeout"` - MaxHeaderBytes int `scfg:"max_header_bytes"` - ReverseProxy bool `scfg:"reverse_proxy"` + Net string `scfg:"net"` + Addr string `scfg:"addr"` + Root string `scfg:"root"` + CookieExpiry int `scfg:"cookie_expiry"` + ReadTimeout uint32 `scfg:"read_timeout"` + WriteTimeout uint32 `scfg:"write_timeout"` + IdleTimeout uint32 `scfg:"idle_timeout"` + MaxHeaderBytes int `scfg:"max_header_bytes"` + ReverseProxy bool `scfg:"reverse_proxy"` + ShutdownTimeout uint32 `scfg:"shutdown_timeout"` } func New(config Config) (server *Server) { handler := &handler{} return &Server{ - net: config.Net, - addr: config.Addr, - root: config.Root, + net: config.Net, + addr: config.Addr, + root: config.Root, + shutdownTimeout: config.ShutdownTimeout, httpServer: &http.Server{ Handler: handler, ReadTimeout: time.Duration(config.ReadTimeout) * time.Second, @@ -57,9 +60,10 @@ func (server *Server) Run(ctx context.Context) (err error) { go func() { <-ctx.Done() - _ = server.httpServer.Close() - _ = listener.Close() // unnecessary? - // TODO: Log the error + shCtx, cancel := context.WithTimeout(context.Background(), time.Duration(server.shutdownTimeout)*time.Second) + defer cancel() + _ = server.httpServer.Shutdown(shCtx) + _ = listener.Close() }() if err = server.httpServer.Serve(listener); err != nil { |