From 7e2f6de992da89c2afa603b94622bbd22bd98e27 Mon Sep 17 00:00:00 2001 From: franklin Date: Wed, 9 Aug 2023 07:09:59 +0200 Subject: [PATCH] fix: omit race condition --- servicehttp.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/servicehttp.go b/servicehttp.go index e798576..a1e3cc4 100644 --- a/servicehttp.go +++ b/servicehttp.go @@ -5,6 +5,7 @@ import ( "net" "net/http" "strings" + "sync/atomic" "github.com/pkg/errors" "go.uber.org/zap" @@ -15,7 +16,7 @@ import ( // ServiceHTTP struct type ServiceHTTP struct { - running bool + running atomic.Bool server *http.Server name string l *zap.Logger @@ -44,7 +45,7 @@ func (s *ServiceHTTP) Name() string { } func (s *ServiceHTTP) Healthz() error { - if !s.running { + if !s.running.Load() { return ErrServiceNotRunning } return nil @@ -62,9 +63,9 @@ func (s *ServiceHTTP) Start(ctx context.Context) error { s.l.Info("starting http service", fields...) s.server.BaseContext = func(_ net.Listener) context.Context { return ctx } s.server.RegisterOnShutdown(func() { - s.running = false + s.running.Store(false) }) - s.running = true + s.running.Store(true) if err := s.server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { log.WithError(s.l, err).Error("service error") return err