Skip to content

Commit

Permalink
fix potential issue with race condition (#8577)
Browse files Browse the repository at this point in the history
(cherry picked from commit a27ded6)
  • Loading branch information
ssoroka committed Dec 16, 2020
1 parent 6d4bfb7 commit c2d63c6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/glinton/ping"
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit c2d63c6

Please sign in to comment.