cmd/routed: don't use waitgroups

This commit is contained in:
Cadey Ratio 2018-01-20 09:40:54 -08:00
parent b628042e29
commit a0d1787b5b
1 changed files with 30 additions and 16 deletions

View File

@ -51,12 +51,9 @@ func main() {
ln.FatalErr(ctx, err, ln.Action("create server instance")) ln.FatalErr(ctx, err, ln.Action("create server instance"))
} }
wg := &sync.WaitGroup{} go setupQuic(ctx, s, scfg)
wg.Add(3) go setupTLS(ctx, s, scfg)
go setupHTTP(ctx, s, scfg)
go setupQuic(ctx, wg, s, scfg)
go setupTLS(ctx, wg, s, scfg)
go setupHTTP(ctx, wg, s, scfg)
ch := make(chan os.Signal, 2) ch := make(chan os.Signal, 2)
@ -71,14 +68,21 @@ func main() {
<-ctx.Done() <-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 // listen on HTTP listener
l, err := net.Listen("tcp", scfg.WebAddr) l, err := net.Listen("tcp", scfg.WebAddr)
if err != nil { 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() defer l.Close()
@ -92,20 +96,25 @@ func setupHTTP(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config)
for { for {
select { select {
case <-ctx.Done(): 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) hs.SetKeepAlivesEnabled(false)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
hs.Shutdown(ctx) hs.Shutdown(ctx)
wg.Done()
ln.Log(ctx, f, ln.Action("shutdown complete"))
return 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{ qs := &h2quic.Server{
Server: &http.Server{ Server: &http.Server{
Handler: middleware.Trace(s), Handler: middleware.Trace(s),
@ -126,20 +135,25 @@ func setupQuic(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config)
for { for {
select { select {
case <-ctx.Done(): 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) qs.SetKeepAlivesEnabled(false)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
qs.Shutdown(ctx) qs.Shutdown(ctx)
wg.Done()
ln.Log(ctx, f, ln.Action("shutdown complete"))
return return
} }
} }
} }
func setupTLS(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) { func setupTLS(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) {
f := ln.F{
"kind": "https",
"addr": scfg.SSLAddr,
}
hs := &http.Server{ hs := &http.Server{
Handler: middleware.Trace(s), Handler: middleware.Trace(s),
Addr: scfg.SSLAddr, Addr: scfg.SSLAddr,
@ -156,14 +170,14 @@ func setupTLS(ctx context.Context, wg *sync.WaitGroup, s *Server, scfg Config) {
for { for {
select { select {
case <-ctx.Done(): 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) hs.SetKeepAlivesEnabled(false)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
hs.Shutdown(ctx) hs.Shutdown(ctx)
wg.Done()
ln.Log(ctx, f, ln.Action("shutdown complete"))
return return
} }
} }