Skip to content

Commit

Permalink
Introduce a flag and a mutex in the beater so that we can't stop the …
Browse files Browse the repository at this point in the history
…server before assigning it (ie. read before write)

fixes elastic#514
  • Loading branch information
Juan A committed Jan 30, 2018
1 parent 803c271 commit f912c22
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import (
"net/http"
"regexp"

"sync"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)

type beater struct {
config *Config
server *http.Server
config *Config
server *http.Server
mutex sync.RWMutex
stopped bool
}

// Creates beater
Expand All @@ -36,7 +40,8 @@ func New(b *beat.Beat, ucfg *common.Config) (beat.Beater, error) {
}

bt := &beater{
config: beaterConfig,
config: beaterConfig,
stopped: false,
}
return bt, nil
}
Expand All @@ -57,7 +62,14 @@ func (bt *beater) Run(b *beat.Beat) error {
}
go notifyListening(bt.config, pub.Send)

bt.mutex.Lock()
if bt.stopped {
defer bt.mutex.Unlock()
return nil
}

bt.server = newServer(bt.config, pub.Send)
bt.mutex.Unlock()

err = run(bt.server, lis, bt.config)
if err == http.ErrServerClosed {
Expand All @@ -70,5 +82,10 @@ func (bt *beater) Run(b *beat.Beat) error {
// Graceful shutdown
func (bt *beater) Stop() {
logp.Info("stopping apm-server...")
stop(bt.server, bt.config.ShutdownTimeout)
bt.mutex.Lock()
if bt.server != nil {
stop(bt.server, bt.config.ShutdownTimeout)
}
bt.stopped = true
bt.mutex.Unlock()
}

0 comments on commit f912c22

Please sign in to comment.