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,18 +40,18 @@ func (b *Buffer) Len() int {
// Add adds metrics to the buffer.
func (b *Buffer) Add(metrics ...telegraf.Metric) {
b.mu.Lock()
for i, _ := range metrics {
MetricsWritten.Incr(1)
select {
case b.buf <- metrics[i]:
default:
b.mu.Lock()
MetricsDropped.Incr(1)
<-b.buf
b.buf <- metrics[i]
b.mu.Unlock()
}
}
b.mu.Unlock()
}
// Batch returns a batch of metrics of size batchSize.