forked from TrueCloudLab/certificates
Add sync.WaitGroup for proper error handling in Run()
This commit is contained in:
parent
9bda3c465a
commit
2320d0911e
1 changed files with 15 additions and 4 deletions
19
ca/ca.go
19
ca/ca.go
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -238,18 +239,28 @@ func (ca *CA) Init(config *authority.Config) (*CA, error) {
|
|||
// Run starts the CA calling to the server ListenAndServe method.
|
||||
func (ca *CA) Run() error {
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, 1)
|
||||
go func() {
|
||||
if ca.insecureSrv != nil {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue