From c2d63c6d5b7f97d150c574213fd10af03fc0804b Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Wed, 16 Dec 2020 15:39:12 -0500 Subject: [PATCH] fix potential issue with race condition (#8577) (cherry picked from commit a27ded6d957f0de5b250d8861f3f308a08700220) --- plugins/inputs/ping/ping.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index da9ab8698e83b..87f7af8e7489f 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -12,6 +12,7 @@ import ( "sort" "strings" "sync" + "sync/atomic" "time" "github.com/glinton/ping" @@ -289,7 +290,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { c := ping.Client{} var doErr error - var packetsSent int + var packetsSent int32 type sentReq struct { err error @@ -304,7 +305,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { doErr = sent.err } if sent.sent { - packetsSent++ + atomic.AddInt32(&packetsSent, 1) } } r.Done() @@ -387,7 +388,7 @@ func percentile(values durationSlice, perc int) time.Duration { } } -func onFin(packetsSent int, resps []*ping.Response, err error, destination string, percentiles []int) (map[string]string, map[string]interface{}) { +func onFin(packetsSent int32, resps []*ping.Response, err error, destination string, percentiles []int) (map[string]string, map[string]interface{}) { packetsRcvd := len(resps) tags := map[string]string{"url": destination} @@ -412,7 +413,7 @@ func onFin(packetsSent int, resps []*ping.Response, err error, destination strin return tags, fields } - fields["percent_packet_loss"] = float64(packetsSent-packetsRcvd) / float64(packetsSent) * 100 + fields["percent_packet_loss"] = float64(int(packetsSent)-packetsRcvd) / float64(packetsSent) * 100 ttl := resps[0].TTL var min, max, avg, total time.Duration