From 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sat, 20 Sep 2025 09:35:50 +0200 Subject: docs: add comments for all functions --- util/sys/psutil.go | 2 ++ util/sys/sys_linux.go | 4 ++++ util/sys/sys_windows.go | 5 +++++ 3 files changed, 11 insertions(+) (limited to 'util/sys') diff --git a/util/sys/psutil.go b/util/sys/psutil.go index 3d7cac80..98adf775 100644 --- a/util/sys/psutil.go +++ b/util/sys/psutil.go @@ -1,3 +1,5 @@ +// Package sys provides system utilities for monitoring network connections and CPU usage. +// Platform-specific implementations are provided for Windows, Linux, and macOS. package sys import ( diff --git a/util/sys/sys_linux.go b/util/sys/sys_linux.go index 8a494d62..23483b57 100644 --- a/util/sys/sys_linux.go +++ b/util/sys/sys_linux.go @@ -45,6 +45,8 @@ func getLinesNum(filename string) (int, error) { return sum, nil } +// GetTCPCount returns the number of active TCP connections by reading +// /proc/net/tcp and /proc/net/tcp6 when available. func GetTCPCount() (int, error) { root := HostProc() @@ -75,6 +77,8 @@ func GetUDPCount() (int, error) { return udp4 + udp6, nil } +// safeGetLinesNum returns 0 if the file does not exist, otherwise forwards +// to getLinesNum to count the number of lines. func safeGetLinesNum(path string) (int, error) { if _, err := os.Stat(path); os.IsNotExist(err) { return 0, nil diff --git a/util/sys/sys_windows.go b/util/sys/sys_windows.go index f3eae076..186fa4bb 100644 --- a/util/sys/sys_windows.go +++ b/util/sys/sys_windows.go @@ -12,6 +12,7 @@ import ( "github.com/shirou/gopsutil/v4/net" ) +// GetConnectionCount returns the number of active connections for the specified protocol ("tcp" or "udp"). func GetConnectionCount(proto string) (int, error) { if proto != "tcp" && proto != "udp" { return 0, errors.New("invalid protocol") @@ -24,10 +25,12 @@ func GetConnectionCount(proto string) (int, error) { return len(stats), nil } +// GetTCPCount returns the number of active TCP connections. func GetTCPCount() (int, error) { return GetConnectionCount("tcp") } +// GetUDPCount returns the number of active UDP connections. func GetUDPCount() (int, error) { return GetConnectionCount("udp") } @@ -50,6 +53,8 @@ type filetime struct { HighDateTime uint32 } +// ftToUint64 converts a Windows FILETIME-like struct to a uint64 for +// arithmetic and delta calculations used by CPUPercentRaw. func ftToUint64(ft filetime) uint64 { return (uint64(ft.HighDateTime) << 32) | uint64(ft.LowDateTime) } -- cgit v1.2.3