Skip to content

Commit

Permalink
Pass around the IPKey instead of storing it on the activeClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Mar 22, 2024
1 parent e428f8d commit a5731fc
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func toIPKey(addr net.Addr, accessKey string) (*IPKey, error) {
type ReportTunnelTimeFunc func(IPKey, ipinfo.IPInfo, time.Duration)

type activeClient struct {
IPKey IPKey
clientInfo ipinfo.IPInfo
connectionCount int
startTime time.Time
Expand All @@ -101,16 +100,16 @@ func (t *tunnelTimeTracker) reportAll(now time.Time) {
}
t.mu.Lock()
defer t.mu.Unlock()
for _, c := range t.activeClients {
t.reportDuration(c, now)
for ipKey, c := range t.activeClients {
t.reportDuration(ipKey, c, now)
}
}

// Reports time connected for a given active client.
func (t *tunnelTimeTracker) reportDuration(c *activeClient, tNow time.Time) {
func (t *tunnelTimeTracker) reportDuration(ipKey IPKey, c *activeClient, tNow time.Time) {
connDuration := tNow.Sub(c.startTime)
logger.Debugf("Reporting activity for key `%v`, duration: %v", c.IPKey.accessKey, connDuration)
t.reportTunnelTime(c.IPKey, c.clientInfo, connDuration)
logger.Debugf("Reporting activity for key `%v`, duration: %v", ipKey.accessKey, connDuration)
t.reportTunnelTime(ipKey, c.clientInfo, connDuration)
// Reset the start time now that it's been reported.
c.startTime = tNow
}
Expand All @@ -123,7 +122,6 @@ func (t *tunnelTimeTracker) startConnection(ipKey IPKey) {
if !exists {
clientInfo, _ := ipinfo.GetIPInfoFromIP(t.IPInfoMap, net.IP(ipKey.ip.AsSlice()))
c = &activeClient{
IPKey: ipKey,
clientInfo: clientInfo,
startTime: now(),
}
Expand All @@ -143,7 +141,7 @@ func (t *tunnelTimeTracker) stopConnection(ipKey IPKey) {
}
c.connectionCount--
if c.connectionCount <= 0 {
t.reportDuration(c, now())
t.reportDuration(ipKey, c, now())
delete(t.activeClients, ipKey)
return
}
Expand Down

0 comments on commit a5731fc

Please sign in to comment.