fix: 网络检测增强 + Tracker changeset 清理

网络检测 (monitor.go):
- TCP 拨测 (8.8.8.8:53 / 1.1.1.1:53 / 208.67.222.222:53)
- 延迟阈值检测 (>5s 标记降级)
- DNS 多目标检测 (google/baidu/cloudflare)
- NetworkCheckResult 新增 TCPReachable + LatencyDegraded

Tracker:
- 新增 WithKeepChangesets / WithMaxChangesetAge 选项
- Init 时自动清理过期 changeset (默认保留100份/30天)
- 先按年龄裁剪,再按数量裁剪
This commit is contained in:
root
2026-07-03 21:31:00 +08:00
parent 068af0569c
commit 77e12f7329
4 changed files with 158 additions and 33 deletions

View File

@ -12,11 +12,11 @@ import (
)
type Monitor struct {
mu sync.RWMutex
client *http.Client
interval time.Duration
mu sync.RWMutex
client *http.Client
interval time.Duration
endpoints []string
status []EndpointStatus
status []EndpointStatus
}
type EndpointStatus struct {
@ -142,7 +142,11 @@ func (m *Monitor) AggregateResult() types.NetworkCheckResult {
m.mu.RLock()
defer m.mu.RUnlock()
result := types.NetworkCheckResult{LLMAPIReachable: true, DNSResolving: true}
result := types.NetworkCheckResult{
LLMAPIReachable: true,
DNSResolving: true,
TCPReachable: true,
}
var totalLatency time.Duration
checked := 0
@ -161,14 +165,49 @@ func (m *Monitor) AggregateResult() types.NetworkCheckResult {
result.Latency = totalLatency / time.Duration(checked)
}
result.DNSResolving = m.checkDNS()
// 延迟阈值检测:平均延迟 > 5s 标记为降级
if result.Latency > 5*time.Second {
result.LatencyDegraded = true
if result.Error == "" {
result.Error = fmt.Sprintf("high latency: %v", result.Latency)
}
}
// DNS 多目标检测
result.DNSResolving = m.checkDNSMulti()
// TCP 拨测:检测基础网络通畅性
result.TCPReachable = m.checkTCPReachability()
return result
}
func (m *Monitor) checkDNS() bool {
_, err := net.LookupHost("google.com")
if err != nil {
_, err = net.LookupHost("baidu.com")
func (m *Monitor) checkDNSMulti() bool {
targets := []string{"google.com", "baidu.com", "cloudflare.com"}
for _, target := range targets {
_, err := net.LookupHost(target)
if err == nil {
return true
}
}
return err == nil
return false
}
func (m *Monitor) checkTCPReachability() bool {
targets := []struct {
host string
port string
}{
{"8.8.8.8", "53"},
{"1.1.1.1", "53"},
{"208.67.222.222", "53"},
}
for _, t := range targets {
conn, err := net.DialTimeout("tcp", net.JoinHostPort(t.host, t.port), 3*time.Second)
if err == nil {
conn.Close()
return true
}
}
return false
}