diff --git a/go.mod b/go.mod index f5438c2ca71d05013187c62a35c76a8fd1fd6f14..f129089203568faf47e3d0874a6a4864946f2add 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( cloud.google.com/go v0.88.0 // indirect - git.autistici.org/ai3/go-common v0.0.0-20230201225947-3ffd6ec2b357 + git.autistici.org/ai3/go-common v0.0.0-20230526131513-5afdaf014661 git.autistici.org/id/usermetadb v0.0.0-20221125171152-3bbb63732147 github.com/bradfitz/gomemcache v0.0.0-20230124162541-5f7a7d875746 github.com/cenkalti/backoff/v4 v4.1.3 diff --git a/go.sum b/go.sum index 50ea4681366c91128b520fe8f75de1ab80dd9fa0..12b0cd84f78970290bf7d7dfca770aa4de749ba6 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ git.autistici.org/ai3/go-common v0.0.0-20221125154433-06304016b1da h1:fizdAjFv2v git.autistici.org/ai3/go-common v0.0.0-20221125154433-06304016b1da/go.mod h1:FTGqOGPpuoFg7TiHshYCyp5j1Ab3ek0J0KcS++vEjxw= git.autistici.org/ai3/go-common v0.0.0-20230201225947-3ffd6ec2b357 h1:kXIwZ3SJ8r2dvBMAnuQ8QbDiR651rg/O150iTRxKkcA= git.autistici.org/ai3/go-common v0.0.0-20230201225947-3ffd6ec2b357/go.mod h1:FTGqOGPpuoFg7TiHshYCyp5j1Ab3ek0J0KcS++vEjxw= +git.autistici.org/ai3/go-common v0.0.0-20230526131513-5afdaf014661 h1:QidAfAxaIMWWu52luWF30wvRpv7t+Ic73xxsTUENqSU= +git.autistici.org/ai3/go-common v0.0.0-20230526131513-5afdaf014661/go.mod h1:FTGqOGPpuoFg7TiHshYCyp5j1Ab3ek0J0KcS++vEjxw= git.autistici.org/id/usermetadb v0.0.0-20220929090407-7c1d2d277883 h1:8jWU0QoxibxytAQCy4L7dzSAT8JnYsKPQJDI26x8fNs= git.autistici.org/id/usermetadb v0.0.0-20220929090407-7c1d2d277883/go.mod h1:Qh9FMGaPH8w1infgm634W2apkk4x2lijYFTZqtxYVmg= git.autistici.org/id/usermetadb v0.0.0-20220929094101-56cbd40bfe90 h1:DHNInqy+lPbxOzIIysoDgQoDHP1UNVDUVL3/YvwnycQ= diff --git a/vendor/git.autistici.org/ai3/go-common/serverutil/http.go b/vendor/git.autistici.org/ai3/go-common/serverutil/http.go index b2575350bcb11e0099375aee69a3b4f37a3500c7..ebfe6b6f7a914b34d76e4e19ae906f8ba9439b3d 100644 --- a/vendor/git.autistici.org/ai3/go-common/serverutil/http.go +++ b/vendor/git.autistici.org/ai3/go-common/serverutil/http.go @@ -104,13 +104,20 @@ func (config *ServerConfig) buildHTTPHandler(h http.Handler) (http.Handler, *tls return h, tlsConfig, nil } -// Serve HTTP(S) content on the specified address. If config.TLS is -// not nil, enable HTTPS and TLS authentication. -// -// This function will return an error if there are problems creating -// the listener, otherwise it will handle graceful termination on -// SIGINT or SIGTERM and return nil. -func Serve(h http.Handler, config *ServerConfig, addr string) error { +func buildListener(addr string, tlsConfig *tls.Config) (net.Listener, error) { + // Create the net.Listener first, so we can detect + // initialization-time errors safely. + l, err := net.Listen("tcp", addr) + if err != nil { + return nil, err + } + if tlsConfig != nil { + l = tls.NewListener(l, tlsConfig) + } + return l, nil +} + +func buildServer(h http.Handler, config *ServerConfig, addr string) (*http.Server, error) { // Wrap with tracing handler (exclude metrics and other // debugging endpoints). h = tracing.WrapHandler(h, guessEndpointName(addr)) @@ -118,7 +125,7 @@ func Serve(h http.Handler, config *ServerConfig, addr string) error { // Create the top-level HTTP handler with all our additions. hh, tlsConfig, err := config.buildHTTPHandler(h) if err != nil { - return err + return nil, err } // These are not meant to be external-facing servers, so we @@ -131,14 +138,24 @@ func Serve(h http.Handler, config *ServerConfig, addr string) error { TLSConfig: tlsConfig, } - // Create the net.Listener first, so we can detect - // initialization-time errors safely. - l, err := net.Listen("tcp", addr) + return srv, nil +} + +// Serve HTTP(S) content on the specified address. If config.TLS is +// not nil, enable HTTPS and TLS authentication. +// +// This function will return an error if there are problems creating +// the listener, otherwise it will handle graceful termination on +// SIGINT or SIGTERM and return nil. +func Serve(h http.Handler, config *ServerConfig, addr string) error { + srv, err := buildServer(h, config, addr) if err != nil { return err } - if srv.TLSConfig != nil { - l = tls.NewListener(l, srv.TLSConfig) + + l, err := buildListener(addr, srv.TLSConfig) + if err != nil { + return err } // Install a signal handler for gentle process termination. @@ -176,6 +193,38 @@ func Serve(h http.Handler, config *ServerConfig, addr string) error { return nil } +// ServeWithContext operates like Serve but with a controlling Context +// that can be used to stop the HTTP server. +func ServeWithContext(ctx context.Context, h http.Handler, config *ServerConfig, addr string) error { + srv, err := buildServer(h, config, addr) + if err != nil { + return err + } + + l, err := buildListener(addr, srv.TLSConfig) + if err != nil { + return err + } + + go func() { + <-ctx.Done() + + sctx, cancel := context.WithTimeout(context.Background(), gracefulShutdownTimeout) + srv.Shutdown(sctx) // nolint: errcheck + srv.Close() + cancel() + }() + + daemon.SdNotify(false, "READY=1") // nolint + + err = srv.Serve(l) + if err == http.ErrServerClosed { + err = nil + } + + return err +} + func addDefaultHandlers(h http.Handler) http.Handler { root := http.NewServeMux() diff --git a/vendor/modules.txt b/vendor/modules.txt index 1dbf0e5222ba5ef0a9dfb96318a60d10c8eee5bc..9bf7ca66d03dbfd7eb930b7b4bb3801cd7b5bad8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # cloud.google.com/go v0.88.0 ## explicit cloud.google.com/go/compute/metadata -# git.autistici.org/ai3/go-common v0.0.0-20230201225947-3ffd6ec2b357 +# git.autistici.org/ai3/go-common v0.0.0-20230526131513-5afdaf014661 ## explicit git.autistici.org/ai3/go-common git.autistici.org/ai3/go-common/clientutil