mirror of
https://gitcode.com/JianFeeeee/webui4frpc.git
synced 2026-09-21 09:27:57 +00:00
feat: 网络接口负载采样 — 令牌环认领决策引入真实 NIC 饱和度
- netload_linux.go: 从 /proc/net/dev 字节增量 + /sys/class/net/<iface>/speed 计算网卡利用率 (rx+tx 合计, 多网卡按容量加权, [0,100] 钳制) - virtio VM speed=-1 时用 1Gbps 软容量兜底, 保持 VM 上采样有意义 - 排除 lo/docker*/br-*/veth*/tun/tap/wg/virbr* 等非物理 uplink, 容器桥接流量不再虚增主机负载 - netload_other.go: 非 Linux 平台编译期 fallback 返回 0 - main.go LoadFn: NetPct 从写死的 10 换成 cluster.SampleNetLoad() - Score = Forwards*100 + MemPct + NetPct: 主信号仍是转发数, net 在同转发数节点间打破平局 — NIC 已饱和的节点不再吸引新转发 - 单测: 采样范围断言 + 虚拟接口排除表 实测: 三台集群 net 列显示真实值 (0.02-0.03%), 不再是常量
This commit is contained in:
49
internal/cluster/netload_linux_test.go
Normal file
49
internal/cluster/netload_linux_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
//go:build linux
|
||||
|
||||
package cluster
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestSampleNetLoadBasic: the sampler must stay in [0,100], must return 0 on
|
||||
// the first (seeding) call, and must be stable across repeated calls. On a
|
||||
// CI/dev box without a speed-known physical NIC it may legitimately return 0
|
||||
// every time — that's fine; we only assert the invariants, not a specific
|
||||
// value.
|
||||
func TestSampleNetLoadBasic(t *testing.T) {
|
||||
first := SampleNetLoad()
|
||||
if first < 0 || first > 100 {
|
||||
t.Fatalf("first sample out of range: %v", first)
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
v := SampleNetLoad()
|
||||
if v < 0 || v > 100 {
|
||||
t.Fatalf("sample %d out of range: %v", i, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsPhysicalUplink: virtual/container interfaces must never count as
|
||||
// uplinks — a busy docker bridge would otherwise fake host saturation.
|
||||
func TestIsPhysicalUplink(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
typ int
|
||||
want bool
|
||||
}{
|
||||
{"lo", 772, false},
|
||||
{"ens18", 1, true}, // ARPHRD_ETHER physical
|
||||
{"eth0", 1, true}, // ARPHRD_ETHER physical
|
||||
{"docker0", 1, false},
|
||||
{"br-abc123", 1, false},
|
||||
{"veth9f2c1a", 1, false},
|
||||
{"wg0", 1, false},
|
||||
{"tun0", 65534, false}, // ARPHRD_NONE software iface
|
||||
{"tap7", 1, false},
|
||||
{"bond0", 1, true}, // real aggregated uplink counts
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := isPhysicalUplink(c.name, c.typ); got != c.want {
|
||||
t.Errorf("isPhysicalUplink(%q,%d)=%v want %v", c.name, c.typ, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user