Skip to content

Commit

Permalink
fix(race): include mutex to avoid race conditions in health (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito authored Jun 28, 2021
1 parent 4a4a35b commit acb4510
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ install:
- make deps
script:
- make test
after_success:
- bash <(curl -s https://codecov.io/bash)
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ clean:
deps:
go mod download
test:
echo "" > coverage.txt
for d in $(shell go list ./...); do \
go test -race -v -coverprofile=profile.out -covermode=atomic $$d || exit 1; \
[ -f profile.out ] && cat profile.out >> coverage.txt && rm profile.out; \
done
go test -race -cover ./...
12 changes: 11 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var mutex sync.Mutex

// Server server representation
type Server struct {
sync.RWMutex
name string
health *ServerHealth
serverSettings ServerSettings
Expand Down Expand Up @@ -70,13 +71,22 @@ func (s *Server) CheckHealth(traceOn bool, logger Logger) {
var secondsBehindMaster, openConnections, runningConnections *int

// prevent concurrently checks on same server (slow queries/network)
if s.isChecking {
s.RLock()
checking := s.isChecking
s.RUnlock()

if checking {
return
}

s.Lock()
s.isChecking = true
s.Unlock()

defer func() {
s.Lock()
s.isChecking = false
s.Unlock()
}()

if err := s.connectReadUser(traceOn, logger); err != nil {
Expand Down

0 comments on commit acb4510

Please sign in to comment.