chore(controller,app): refactor app server mutex usage
arcad/emissary/pipeline/head There was a failure building this commit Details

This commit is contained in:
wpetit 2023-03-31 17:28:52 +02:00
parent b52c687643
commit 3e02a9f031
1 changed files with 15 additions and 9 deletions

View File

@ -35,12 +35,15 @@ type Server struct {
} }
func (s *Server) Start(ctx context.Context, addr string) (err error) { func (s *Server) Start(ctx context.Context, addr string) (err error) {
if s.server != nil { if s.Running() {
if err := s.Stop(); err != nil { if err := s.Stop(); err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
} }
s.serverMutex.Lock()
defer s.serverMutex.Unlock()
router := chi.NewRouter() router := chi.NewRouter()
router.Use(middleware.Logger) router.Use(middleware.Logger)
@ -85,9 +88,7 @@ func (s *Server) Start(ctx context.Context, addr string) (err error) {
} }
}() }()
s.serverMutex.Lock()
s.server = server s.server = server
s.serverMutex.Unlock()
return nil return nil
} }
@ -100,20 +101,25 @@ func (s *Server) Running() bool {
} }
func (s *Server) Stop() error { func (s *Server) Stop() error {
if !s.Running() {
return nil
}
s.serverMutex.Lock()
defer s.serverMutex.Unlock()
if s.server == nil { if s.server == nil {
return nil return nil
} }
defer func() {
s.serverMutex.Lock()
s.server = nil
s.serverMutex.Unlock()
}()
if err := s.server.Close(); err != nil { if err := s.server.Close(); err != nil {
s.server = nil
return errors.WithStack(err) return errors.WithStack(err)
} }
s.server = nil
return nil return nil
} }