diff --git a/cmd/routed/main.go b/cmd/routed/main.go index 638fb96..cd8f743 100644 --- a/cmd/routed/main.go +++ b/cmd/routed/main.go @@ -51,12 +51,9 @@ func main() { ln.FatalErr(ctx, err, ln.Action("create server instance")) } - wg := &sync.WaitGroup{} - wg.Add(3) - - go setupQuic(ctx, wg, s, scfg) - go setupTLS(ctx, wg, s, scfg) - go setupHTTP(ctx, wg, s, scfg) + go setupQuic(ctx, s, scfg) + go setupTLS(ctx, s, scfg) + go setupHTTP(ctx, s, scfg) ch := make(chan os.Signal, 2) @@ -71,14 +68,21 @@ func main() { <-ctx.Done() - wg.Wait() + signal.Reset(os.Interrupt) + fmt.Printlf("%s is now waiting for final shutdown, press ^C again to kill it now\n", os.Args[0]) + time.Sleep(30 * time.Second) } -func setupHTTP(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) { +func setupHTTP(ctx context.Context, s *Server, scfg Config) { + f := ln.F{ + "kind": "http", + "addr": scfg.WebAddr, + } + // listen on HTTP listener l, err := net.Listen("tcp", scfg.WebAddr) if err != nil { - ln.FatalErr(ctx, err, ln.Action("listening on HTTP port"), ln.F{"addr": scfg.WebAddr}) + ln.FatalErr(ctx, err, f, ln.Action("listening on HTTP port")) } defer l.Close() @@ -92,20 +96,25 @@ func setupHTTP(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) for { select { case <-ctx.Done(): - ln.Log(ctx, ln.F{"kind": "http"}, ln.Action("shutdown signal recieved")) + ln.Log(ctx, f, ln.Action("shutdown signal recieved")) hs.SetKeepAlivesEnabled(false) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() hs.Shutdown(ctx) - wg.Done() + ln.Log(ctx, f, ln.Action("shutdown complete")) return } } } -func setupQuic(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) { +func setupQuic(ctx context.Context, s *Server, scfg Config) { + f := ln.F{ + "kind": "quic", + "addr": scfg.QuicAddr, + } + qs := &h2quic.Server{ Server: &http.Server{ Handler: middleware.Trace(s), @@ -126,20 +135,25 @@ func setupQuic(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) for { select { case <-ctx.Done(): - ln.Log(ctx, ln.F{"kind": "quic"}, ln.Action("shutdown signal recieved")) + ln.Log(ctx, f, ln.Action("shutdown signal recieved")) qs.SetKeepAlivesEnabled(false) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() qs.Shutdown(ctx) - wg.Done() + ln.Log(ctx, f, ln.Action("shutdown complete")) return } } } func setupTLS(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) { + f := ln.F{ + "kind": "https", + "addr": scfg.SSLAddr, + } + hs := &http.Server{ Handler: middleware.Trace(s), Addr: scfg.SSLAddr, @@ -156,14 +170,14 @@ func setupTLS(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) { for { select { case <-ctx.Done(): - ln.Log(ctx, ln.F{"kind": "https"}, ln.Action("shutdown signal recieved")) + ln.Log(ctx, f, ln.Action("shutdown signal recieved")) hs.SetKeepAlivesEnabled(false) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() hs.Shutdown(ctx) - wg.Done() + ln.Log(ctx, f, ln.Action("shutdown complete")) return } }