fix potential issue with race condition (#8577)

This commit is contained in:
Steven Soroka 2020-12-16 15:39:12 -05:00 committed by GitHub
parent e39208d60a
commit a27ded6d95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/glinton/ping" "github.com/glinton/ping"
@ -289,7 +290,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
c := ping.Client{} c := ping.Client{}
var doErr error var doErr error
var packetsSent int var packetsSent int32
type sentReq struct { type sentReq struct {
err error err error
@ -304,7 +305,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
doErr = sent.err doErr = sent.err
} }
if sent.sent { if sent.sent {
packetsSent++ atomic.AddInt32(&packetsSent, 1)
} }
} }
r.Done() 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) packetsRcvd := len(resps)
tags := map[string]string{"url": destination} tags := map[string]string{"url": destination}
@ -412,7 +413,7 @@ func onFin(packetsSent int, resps []*ping.Response, err error, destination strin
return tags, fields 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 ttl := resps[0].TTL
var min, max, avg, total time.Duration var min, max, avg, total time.Duration