Skip to content

Commit

Permalink
Add a lock to the tracker.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Mar 21, 2024
1 parent 1816156 commit a3d7a57
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type IPKey struct {
}

type tunnelTimeTracker struct {
mu sync.Mutex
activeClients map[IPKey]*activeClient
reportTunnelTime ReportTunnelTimeFunc
}
Expand All @@ -87,6 +88,8 @@ func (t *tunnelTimeTracker) reportAll(now time.Time) {
logger.Debugf("No active clients. No TunnelTime activity to report.")
return
}
t.mu.Lock()
defer t.mu.Unlock()
for _, c := range t.activeClients {
t.reportDuration(c, now)
}
Expand All @@ -108,6 +111,8 @@ func (t *tunnelTimeTracker) startConnection(clientInfo ipinfo.IPInfo, clientAddr
hostname, _, _ := net.SplitHostPort(clientAddr.String())
ipKey := IPKey{ip: hostname, accessKey: accessKey}

t.mu.Lock()
defer t.mu.Unlock()
c, exists := t.activeClients[ipKey]
if !exists {
c = &activeClient{
Expand All @@ -125,6 +130,8 @@ func (t *tunnelTimeTracker) stopConnection(clientAddr net.Addr, accessKey string
hostname, _, _ := net.SplitHostPort(clientAddr.String())
ipKey := IPKey{ip: hostname, accessKey: accessKey}

t.mu.Lock()
defer t.mu.Unlock()
c, exists := t.activeClients[ipKey]
if !exists {
logger.Warningf("Failed to find active client")
Expand Down

0 comments on commit a3d7a57

Please sign in to comment.