mirror of
https://gitcode.com/JianFeeeee/webui4frpc.git
synced 2026-09-20 17:07:57 +00:00
- 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%), 不再是常量
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
// Runtime handlers the ring engine needs, provided by the app:
|
|
// - RuntimeLoad: sample this node's memory/network load;
|
|
// - Claim: create the frpc worker/forward for a claimed task.
|
|
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"runtime"
|
|
)
|
|
|
|
// AppHandler is the app-provided bridge for ring load sampling + task claims.
|
|
type AppHandler struct {
|
|
// LoadFn returns (mem%, net%) — lower is more idle.
|
|
LoadFn func() (memPct, netPct float64)
|
|
// ClaimFn creates the forward on this node (persist + spawn worker).
|
|
ClaimFn func(ctx context.Context, tk *Task) error
|
|
// RevokeFn cancels the forward on this node (stop worker + drop from store).
|
|
RevokeFn func(ctx context.Context, tk *Task) error
|
|
}
|
|
|
|
// RuntimeLoad implements Handler.
|
|
func (a *AppHandler) RuntimeLoad() Load {
|
|
mem, net := 0.0, 0.0
|
|
if a.LoadFn != nil {
|
|
mem, net = a.LoadFn()
|
|
}
|
|
return Load{MemPct: mem, NetPct: net}
|
|
}
|
|
|
|
// Claim implements Handler.
|
|
func (a *AppHandler) Claim(ctx context.Context, tk *Task) error {
|
|
if a.ClaimFn == nil {
|
|
return nil
|
|
}
|
|
return a.ClaimFn(ctx, tk)
|
|
}
|
|
|
|
// Revoke implements Handler.
|
|
func (a *AppHandler) Revoke(ctx context.Context, tk *Task) error {
|
|
if a.RevokeFn == nil {
|
|
return nil
|
|
}
|
|
return a.RevokeFn(ctx, tk)
|
|
}
|
|
|
|
// SampleMemLoad returns a cheap memory-usage percentage (0..100).
|
|
func SampleMemLoad() float64 {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
total := m.Sys
|
|
if total == 0 {
|
|
return 0
|
|
}
|
|
return float64(m.Alloc) / float64(total) * 100
|
|
}
|
|
|
|
// SampleNetLoad is implemented in netload_linux.go for Linux and returns a
|
|
// safe zero fallback on other platforms. It is kept here as a package-level
|
|
// API because the ring handler injects it into RuntimeLoad.
|
|
|
|
// HostID returns a stable node id (hostname; empty fallback to pid).
|
|
func HostID() string {
|
|
h, _ := os.Hostname()
|
|
if h == "" {
|
|
return "node"
|
|
}
|
|
return h
|
|
}
|