From 03c472359c65a93fb1cf5c329d4b8495398d1088 Mon Sep 17 00:00:00 2001 From: Herman Slatman Date: Fri, 26 Mar 2021 16:21:02 +0100 Subject: [PATCH] Add sync.WaitGroup for proper error handling in Run() --- ca/ca.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ca/ca.go b/ca/ca.go index 4998b45b..1882e8a6 100644 --- a/ca/ca.go +++ b/ca/ca.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "reflect" + "sync" "github.com/go-chi/chi" "github.com/pkg/errors" @@ -251,7 +252,29 @@ func (ca *CA) Init(config *config.Config) (*CA, error) { // Run starts the CA calling to the server ListenAndServe method. func (ca *CA) Run() error { - return ca.srv.ListenAndServe() + var wg sync.WaitGroup + errors := make(chan error, 1) + + if ca.insecureSrv != nil { + wg.Add(1) + go func() { + defer wg.Done() + errors <- ca.insecureSrv.ListenAndServe() + }() + } + + wg.Add(1) + go func() { + defer wg.Done() + errors <- ca.srv.ListenAndServe() + }() + + // wait till error occurs; ensures the servers keep listening + err := <-errors + + wg.Wait() + + return err } // Stop stops the CA calling to the server Shutdown method.