mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-12 05:37:32 +00:00
net/tunstats: add Counts.Add (#5848)
The Counts.Add method merges two Counts together. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -29,6 +29,15 @@ type Counts struct {
|
|||||||
RxBytes uint64 `json:"rxBytes,omitempty"`
|
RxBytes uint64 `json:"rxBytes,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add adds the counts from both c1 and c2.
|
||||||
|
func (c1 Counts) Add(c2 Counts) Counts {
|
||||||
|
c1.TxPackets += c2.TxPackets
|
||||||
|
c1.TxBytes += c2.TxBytes
|
||||||
|
c1.RxPackets += c2.RxPackets
|
||||||
|
c1.RxBytes += c2.RxBytes
|
||||||
|
return c1
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateTx updates the counters for a transmitted IP packet
|
// UpdateTx updates the counters for a transmitted IP packet
|
||||||
// The source and destination of the packet directly correspond with
|
// The source and destination of the packet directly correspond with
|
||||||
// the source and destination in flowtrack.Tuple.
|
// the source and destination in flowtrack.Tuple.
|
||||||
|
@@ -111,13 +111,8 @@ func TestConcurrent(t *testing.T) {
|
|||||||
|
|
||||||
func mergeMaps(dst map[flowtrack.Tuple]Counts, srcs ...map[flowtrack.Tuple]Counts) {
|
func mergeMaps(dst map[flowtrack.Tuple]Counts, srcs ...map[flowtrack.Tuple]Counts) {
|
||||||
for _, src := range srcs {
|
for _, src := range srcs {
|
||||||
for tuple, cntsSrc := range src {
|
for tuple, cnts := range src {
|
||||||
cntsDst := dst[tuple]
|
dst[tuple] = dst[tuple].Add(cnts)
|
||||||
cntsDst.TxPackets += cntsSrc.TxPackets
|
|
||||||
cntsDst.TxBytes += cntsSrc.TxBytes
|
|
||||||
cntsDst.RxPackets += cntsSrc.RxPackets
|
|
||||||
cntsDst.RxBytes += cntsSrc.RxBytes
|
|
||||||
dst[tuple] = cntsDst
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user