1- package tools
1+ package sysstats
22
33import (
44 "time"
@@ -45,40 +45,31 @@ func GetSystemStats() (*common.SystemStatsResponse, error) {
4545}
4646
4747// getBandwidthSpeed returns the aggregate incoming (rx) and outgoing (tx)
48- // bandwidth in bytes per second, sampled over a 1‑ second interval.
48+ // bandwidth in bytes per second, sampled over a 1- second interval.
4949// Loopback interface (lo) is excluded from the calculation.
5050func getBandwidthSpeed () (uint64 , uint64 , error ) {
51- // 1) First snapshot with timestamp
5251 first , err := net .IOCounters (true )
5352 if err != nil {
5453 return 0 , 0 , err
5554 }
5655
57- // 2) Wait one second
5856 time .Sleep (1 * time .Second )
5957
60- // 3) Second snapshot with timestamp
6158 second , err := net .IOCounters (true )
6259 if err != nil {
6360 return 0 , 0 , err
6461 }
6562
66- // 4) Build a map from interface name → first snapshot
67- // Skip loopback interface
6863 prev := make (map [string ]net.IOCountersStat , len (first ))
6964 for _ , c := range first {
70- // Skip loopback interface
7165 if c .Name == "lo" {
7266 continue
7367 }
7468 prev [c .Name ] = c
7569 }
7670
77- // 5) Compute deltas and sum across all interfaces
78- // Skip loopback interface
7971 var totalRxBytes , totalTxBytes uint64
8072 for _ , c := range second {
81- // Skip loopback interface
8273 if c .Name == "lo" {
8374 continue
8475 }
@@ -88,6 +79,5 @@ func getBandwidthSpeed() (uint64, uint64, error) {
8879 }
8980 }
9081
91- // 6) Return the calculated rates (bytes per second)
9282 return totalRxBytes , totalTxBytes , nil
9383}
0 commit comments