Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incomplete flush in the sender when flushing the client #163

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 24 additions & 12 deletions statsd/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ type SenderMetrics struct {
}

type sender struct {
transport statsdWriter
pool *bufferPool
queue chan *statsdBuffer
metrics *SenderMetrics
stop chan struct{}
transport statsdWriter
pool *bufferPool
queue chan *statsdBuffer
metrics *SenderMetrics
stop chan struct{}
flushSignal chan struct{}
}

func newSender(transport statsdWriter, queueSize int, pool *bufferPool) *sender {
sender := &sender{
transport: transport,
pool: pool,
queue: make(chan *statsdBuffer, queueSize),
metrics: &SenderMetrics{},
stop: make(chan struct{}),
transport: transport,
pool: pool,
queue: make(chan *statsdBuffer, queueSize),
metrics: &SenderMetrics{},
stop: make(chan struct{}),
flushSignal: make(chan struct{}),
}

go sender.sendLoop()
Expand Down Expand Up @@ -95,11 +97,17 @@ func (s *sender) sendLoop() {
s.write(buffer)
case <-s.stop:
return
case <-s.flushSignal:
// At that point we know that the workers are paused (the statsd client
// will pause them before calling sender.flush()).
// So we can fully flush the input queue
s.flushInputQueue()
s.flushSignal <- struct{}{}
}
}
}

func (s *sender) flush() {
func (s *sender) flushInputQueue() {
for {
select {
case buffer := <-s.queue:
Expand All @@ -109,10 +117,14 @@ func (s *sender) flush() {
}
}
}
func (s *sender) flush() {
s.flushSignal <- struct{}{}
<-s.flushSignal
}

func (s *sender) close() error {
s.stop <- struct{}{}
<-s.stop
s.flush()
s.flushInputQueue()
return s.transport.Close()
}
13 changes: 9 additions & 4 deletions statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,10 @@ func (c *Client) watch() {
}
}

// Flush forces a flush of all the queued dogstatsd payloads
// This method is blocking and will not return until everything is sent
// through the network
// Flush forces a flush of all the queued dogstatsd payloads This method is
// blocking and will not return until everything is sent through the network.
// In MutexMode, this will also block sampling new data to the client while the
// workers and sender are flushed.
func (c *Client) Flush() error {
if c == nil {
return ErrNoClient
Expand All @@ -384,8 +385,12 @@ func (c *Client) Flush() error {
c.agg.sendMetrics()
}
for _, w := range c.workers {
w.flush()
w.pause()
defer w.unpause()
w.flushUnsafe()
}
// Now that the worker are pause the sender can flush the queue between
// worker and senders
c.sender.flush()
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions statsd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (w *worker) flush() {
w.Unlock()
}

func (w *worker) pause() {
w.Lock()
}

func (w *worker) unpause() {
w.Unlock()
}

// flush the current buffer. Lock must be held by caller.
// flushed buffer written to the network asynchronously.
func (w *worker) flushUnsafe() {
Expand Down