Skip to content

Commit

Permalink
Merge pull request #33 from ThomasObenaus/b/fix_race_condition
Browse files Browse the repository at this point in the history
B/fix race condition
  • Loading branch information
ThomasObenaus authored Mar 8, 2022
2 parents 1273ba1 + 9b2b502 commit b4eb3cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

coverage.out

.idea/

go-base.iml
5 changes: 4 additions & 1 deletion health/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func (m *Monitor) evaluateChecks(at time.Time) checkEvaluationResult {
result.numErrors++
logEvent = m.logger.Error()
}
logEvent.Err(err).Msgf("Check - '%s'", name)
logEvent.Err(err).
// don't propagate errors to alerting
Bool("no_alert",true).
Msgf("Check - '%s'", name)
}

if m.onCheckCallback != nil {
Expand Down
10 changes: 9 additions & 1 deletion shutdown/shutdownHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Handler struct {

wg sync.WaitGroup
orderedStopables []Stopable

mux *sync.Mutex
}

// InstallHandler installs a handler for syscall.SIGINT, syscall.SIGTERM
Expand All @@ -29,6 +31,7 @@ func InstallHandler(orderedStopables []Stopable, logger zerolog.Logger) *Handler
logger: logger,
isShutdownPending: atomic.NewBool(false),
orderedStopables: make([]Stopable, 0),
mux: &sync.Mutex{},
}
handler.orderedStopables = append(handler.orderedStopables, orderedStopables...)

Expand All @@ -50,6 +53,8 @@ func (h *Handler) Register(stopable Stopable, front ...bool) {
pushFront = front[0]
}

h.mux.Lock()
defer h.mux.Unlock()
if pushFront {
h.orderedStopables = append([]Stopable{stopable}, h.orderedStopables...)
} else {
Expand All @@ -67,6 +72,8 @@ func (h *Handler) shutdownHandler(shutdownChan <-chan os.Signal, logger zerolog.
logger.Info().Msgf("Received %v. Shutting down...", s)

// Stop all components
h.mux.Lock()
defer h.mux.Unlock()
stop(h.orderedStopables, logger)
}

Expand All @@ -78,12 +85,13 @@ func (h *Handler) WaitUntilSignal() {

// stop calls Stop() on all Stopable in the list as they are ordered.
func stop(orderedStopables []Stopable, logger zerolog.Logger) {

for _, stopable := range orderedStopables {
name := stopable.String()
logger.Debug().Msgf("Stopping %s ...", name)
err := stopable.Stop()
if err != nil {
logger.Error().Err(err).Msgf("Failed stopping '%s'", name)
logger.Error().Err(err).Bool("no_alert", true).Msgf("Failed stopping '%s'", name)
continue
}
logger.Info().Msgf("%s stopped.", name)
Expand Down
4 changes: 4 additions & 0 deletions shutdown/shutdownHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shutdown
import (
"fmt"
"os"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -36,6 +37,7 @@ func Test_ShutdownHandler(t *testing.T) {
h := Handler{
orderedStopables: stopables,
isShutdownPending: atomic.NewBool(false),
mux: &sync.Mutex{},
}
shutDownChan := make(chan os.Signal, 1)

Expand Down Expand Up @@ -71,6 +73,7 @@ func Test_RegisterFront(t *testing.T) {
h := Handler{
orderedStopables: make([]Stopable, 0),
isShutdownPending: atomic.NewBool(false),
mux: &sync.Mutex{},
}
shutDownChan := make(chan os.Signal, 1)

Expand Down Expand Up @@ -111,6 +114,7 @@ func Test_RegisterBack(t *testing.T) {
h := Handler{
orderedStopables: make([]Stopable, 0),
isShutdownPending: atomic.NewBool(false),
mux: &sync.Mutex{},
}
shutDownChan := make(chan os.Signal, 1)

Expand Down

0 comments on commit b4eb3cd

Please sign in to comment.