Lock buffer when adding metrics (#4514)

This function is not thread-safe but is currently used by multiple
goroutines in RunningOutput
This commit is contained in:
Daniel Nelson 2018-08-07 11:22:10 -07:00 committed by GitHub
parent 4dfb80d0fc
commit feb75d493a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -40,19 +40,19 @@ func (b *Buffer) Len() int {
// Add adds metrics to the buffer. // Add adds metrics to the buffer.
func (b *Buffer) Add(metrics ...telegraf.Metric) { func (b *Buffer) Add(metrics ...telegraf.Metric) {
b.mu.Lock()
for i, _ := range metrics { for i, _ := range metrics {
MetricsWritten.Incr(1) MetricsWritten.Incr(1)
select { select {
case b.buf <- metrics[i]: case b.buf <- metrics[i]:
default: default:
b.mu.Lock()
MetricsDropped.Incr(1) MetricsDropped.Incr(1)
<-b.buf <-b.buf
b.buf <- metrics[i] b.buf <- metrics[i]
}
}
b.mu.Unlock() b.mu.Unlock()
} }
}
}
// Batch returns a batch of metrics of size batchSize. // Batch returns a batch of metrics of size batchSize.
// the batch will be of maximum length batchSize. It can be less than batchSize, // the batch will be of maximum length batchSize. It can be less than batchSize,