fix: protect updating latencies/throughput slices in obd (#10611)

Additionally close the transferChan upon function exit.
This commit is contained in:
Harshavardhana
2020-10-01 09:50:08 -07:00
committed by GitHub
parent 3047121255
commit 98a08e1644
2 changed files with 8 additions and 7 deletions

View File

@@ -134,6 +134,7 @@ func (r *nullReader) Read(b []byte) (int, error) {
}
func (client *peerRESTClient) doNetOBDTest(ctx context.Context, dataSize int64, threadCount uint) (info madmin.NetOBDInfo, err error) {
var mu sync.Mutex // mutex used to protect these slices in go-routines
latencies := []float64{}
throughputs := []float64{}
@@ -142,6 +143,8 @@ func (client *peerRESTClient) doNetOBDTest(ctx context.Context, dataSize int64,
totalTransferred := int64(0)
transferChan := make(chan int64, threadCount)
defer close(transferChan)
go func() {
for v := range transferChan {
atomic.AddInt64(&totalTransferred, v)
@@ -226,13 +229,16 @@ func (client *peerRESTClient) doNetOBDTest(ctx context.Context, dataSize int64,
/* Throughput = (total data transferred across all threads / time taken) */
throughput := float64((after - before)) / latency
// Protect updating latencies and throughputs slices from
// multiple go-routines.
mu.Lock()
latencies = append(latencies, latency)
throughputs = append(throughputs, throughput)
mu.Unlock()
}(i)
}
}
wg.Wait()
close(transferChan)
if err != nil {
return info, err