Skip to content

Commit a3d7a57

Browse files
committed
Add a lock to the tracker.
1 parent 1816156 commit a3d7a57

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

cmd/outline-ss-server/metrics.go

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type IPKey struct {
7777
}
7878

7979
type tunnelTimeTracker struct {
80+
mu sync.Mutex
8081
activeClients map[IPKey]*activeClient
8182
reportTunnelTime ReportTunnelTimeFunc
8283
}
@@ -87,6 +88,8 @@ func (t *tunnelTimeTracker) reportAll(now time.Time) {
8788
logger.Debugf("No active clients. No TunnelTime activity to report.")
8889
return
8990
}
91+
t.mu.Lock()
92+
defer t.mu.Unlock()
9093
for _, c := range t.activeClients {
9194
t.reportDuration(c, now)
9295
}
@@ -108,6 +111,8 @@ func (t *tunnelTimeTracker) startConnection(clientInfo ipinfo.IPInfo, clientAddr
108111
hostname, _, _ := net.SplitHostPort(clientAddr.String())
109112
ipKey := IPKey{ip: hostname, accessKey: accessKey}
110113

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

133+
t.mu.Lock()
134+
defer t.mu.Unlock()
128135
c, exists := t.activeClients[ipKey]
129136
if !exists {
130137
logger.Warningf("Failed to find active client")

0 commit comments

Comments
 (0)