Skip to content

Commit

Permalink
Make activeClients a map of pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Mar 21, 2024
1 parent 09fd19e commit 25c4393
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type IPKey struct {
}

type tunnelTimeTracker struct {
activeClients map[IPKey]activeClient
activeClients map[IPKey]*activeClient
reportTunnelTime ReportTunnelTimeFunc
}

Expand All @@ -91,14 +91,12 @@ func (t *tunnelTimeTracker) reportAll(now time.Time) {
}

// Reports time connected for a given active client.
func (t *tunnelTimeTracker) reportDuration(c activeClient, now time.Time) {
func (t *tunnelTimeTracker) reportDuration(c *activeClient, now time.Time) {
connDuration := now.Sub(c.startTime)
logger.Debugf("Reporting activity for key `%v`, duration: %v", c.IPKey.accessKey, connDuration)
t.reportTunnelTime(c.IPKey, c.clientInfo, connDuration)

// Reset the start time now that it's been reported.
c.startTime = Now()
t.activeClients[c.IPKey] = c
}

// Registers a new active connection for a client [net.Addr] and access key.
Expand All @@ -108,7 +106,7 @@ func (t *tunnelTimeTracker) startConnection(clientInfo ipinfo.IPInfo, clientAddr

c, exists := t.activeClients[ipKey]
if !exists {
c = activeClient{ipKey, clientInfo, 0, Now()}
c = &activeClient{ipKey, clientInfo, 0, Now()}
}
c.connectionCount++
t.activeClients[ipKey] = c
Expand All @@ -119,7 +117,6 @@ func (t *tunnelTimeTracker) stopConnection(clientAddr net.Addr, accessKey string
hostname, _, _ := net.SplitHostPort(clientAddr.String())
ipKey := IPKey{ip: hostname, accessKey: accessKey}

c := t.activeClients[ipKey]
c, exists := t.activeClients[ipKey]
if !exists {
logger.Warningf("Failed to find active client")
Expand All @@ -131,11 +128,10 @@ func (t *tunnelTimeTracker) stopConnection(clientAddr net.Addr, accessKey string
delete(t.activeClients, ipKey)
return
}
t.activeClients[ipKey] = c
}

func newTunnelTimeTracker(report ReportTunnelTimeFunc) *tunnelTimeTracker {
tracker := &tunnelTimeTracker{activeClients: make(map[IPKey]activeClient), reportTunnelTime: report}
tracker := &tunnelTimeTracker{activeClients: make(map[IPKey]*activeClient), reportTunnelTime: report}
ticker := time.NewTicker(tunnelTimeTrackerReportingInterval)
go func() {
for t := range ticker.C {
Expand Down

0 comments on commit 25c4393

Please sign in to comment.