mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-23 17:16:29 +00:00
net/connstats: prepare to remove package (#17554)
The connstats package was an unnecessary layer of indirection. It was seperated out of wgengine/netlog so that net/tstun and wgengine/magicsock wouldn't need a depenedency on the concrete implementation of network flow logging. Instead, we simply register a callback for counting connections. This PR does the bare minimum work to prepare tstun and magicsock to only care about that callback. A future PR will delete connstats and merge it into netlog. Updates tailscale/corp#33352 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -5,7 +5,9 @@
|
||||
package netlogtype
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tailscale.com/tailcfg"
|
||||
@@ -83,3 +85,43 @@ func (c1 Counts) Add(c2 Counts) Counts {
|
||||
c1.RxBytes += c2.RxBytes
|
||||
return c1
|
||||
}
|
||||
|
||||
// CountsByConnection is a count of packets and bytes for each connection.
|
||||
// All methods are safe for concurrent calls.
|
||||
type CountsByConnection struct {
|
||||
mu sync.Mutex
|
||||
m map[Connection]Counts
|
||||
}
|
||||
|
||||
// Add adds packets and bytes for the specified connection.
|
||||
func (c *CountsByConnection) Add(proto ipproto.Proto, src, dst netip.AddrPort, packets, bytes int, recv bool) {
|
||||
conn := Connection{Proto: proto, Src: src, Dst: dst}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.m == nil {
|
||||
c.m = make(map[Connection]Counts)
|
||||
}
|
||||
cnts := c.m[conn]
|
||||
if recv {
|
||||
cnts.RxPackets += uint64(packets)
|
||||
cnts.RxBytes += uint64(bytes)
|
||||
} else {
|
||||
cnts.TxPackets += uint64(packets)
|
||||
cnts.TxBytes += uint64(bytes)
|
||||
}
|
||||
c.m[conn] = cnts
|
||||
}
|
||||
|
||||
// Clone deep copies the map.
|
||||
func (c *CountsByConnection) Clone() map[Connection]Counts {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return maps.Clone(c.m)
|
||||
}
|
||||
|
||||
// Reset clear the map.
|
||||
func (c *CountsByConnection) Reset() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
clear(c.m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user