Skip to content

Commit

Permalink
Add mutex to prevent race in statistics reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Oct 28, 2021
1 parent f593f3f commit bdfb89e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/report/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"fmt"
"sync"
"time"

monitor_client "github.com/cyverse/irodsfs-monitor/client"
Expand All @@ -26,6 +27,7 @@ type MonitoringReporter struct {
InstanceID string
FileTransferMap map[string]*monitor_types.ReportFileTransfer
NextFileOffsetMap map[string]int64
Mutex sync.Mutex // lock for FileTransferMap and NextFileOffsetMap
IgnoreError bool
}

Expand All @@ -43,6 +45,7 @@ func NewMonitoringReporter(monitorURL string, ignoreError bool) *MonitoringRepor
InstanceID: "",
FileTransferMap: map[string]*monitor_types.ReportFileTransfer{},
NextFileOffsetMap: map[string]int64{},
Mutex: sync.Mutex{},
IgnoreError: ignoreError,
}
}
Expand Down Expand Up @@ -151,8 +154,13 @@ func (reporter *MonitoringReporter) ReportNewFileTransferStart(path string, file
}

key := reporter.makeFileTransferKey(path, fileHandle)

reporter.Mutex.Lock()
defer reporter.Mutex.Unlock()

reporter.FileTransferMap[key] = transferReport
reporter.NextFileOffsetMap[key] = 0

}
}
}
Expand All @@ -169,6 +177,10 @@ func (reporter *MonitoringReporter) ReportFileTransferDone(path string, fileHand
if !reporter.Failed {
if reporter.MonitoringClient != nil {
key := reporter.makeFileTransferKey(path, fileHandle)

reporter.Mutex.Lock()
defer reporter.Mutex.Unlock()

if transfer, ok := reporter.FileTransferMap[key]; ok {
transfer.FileCloseTime = time.Now().UTC()

Expand Down Expand Up @@ -196,6 +208,10 @@ func (reporter *MonitoringReporter) ReportFileTransfer(path string, fileHandle i
if !reporter.Failed {
if reporter.MonitoringClient != nil {
key := reporter.makeFileTransferKey(path, fileHandle)

reporter.Mutex.Lock()
defer reporter.Mutex.Unlock()

if transfer, ok := reporter.FileTransferMap[key]; ok {
block := monitor_types.FileBlock{
Offset: offset,
Expand Down

0 comments on commit bdfb89e

Please sign in to comment.