mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
feat: 延迟增加高延迟重试机制
This commit is contained in:
@@ -222,26 +222,47 @@ func NewPingTask(conn *ws.SafeConn, taskID uint, pingType, pingTarget string) {
|
|||||||
var latency int64
|
var latency int64
|
||||||
pingResult := -1
|
pingResult := -1
|
||||||
timeout := 3 * time.Second // 默认超时时间
|
timeout := 3 * time.Second // 默认超时时间
|
||||||
|
const highLatencyThreshold = 1000 // ms 阈值
|
||||||
|
|
||||||
|
measure := func() (int64, error) {
|
||||||
switch pingType {
|
switch pingType {
|
||||||
case "icmp":
|
case "icmp":
|
||||||
if latency, err = icmpPing(pingTarget, timeout); err == nil {
|
return icmpPing(pingTarget, timeout)
|
||||||
pingResult = int(latency)
|
|
||||||
}
|
|
||||||
case "tcp":
|
case "tcp":
|
||||||
if latency, err = tcpPing(pingTarget, timeout); err == nil {
|
return tcpPing(pingTarget, timeout)
|
||||||
pingResult = int(latency)
|
|
||||||
}
|
|
||||||
case "http":
|
case "http":
|
||||||
if latency, err = httpPing(pingTarget, timeout); err == nil {
|
return httpPing(pingTarget, timeout)
|
||||||
pingResult = int(latency)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
log.Printf("Unsupported ping type: %s", pingType)
|
return -1, errors.New("unsupported ping type")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
PingHighLatencyRetries := 3
|
||||||
|
// 首次测量
|
||||||
|
if latency, err = measure(); err == nil {
|
||||||
|
if latency > int64(highLatencyThreshold) && PingHighLatencyRetries > 0 {
|
||||||
|
attempts := PingHighLatencyRetries
|
||||||
|
for i := 0; i < attempts; i++ {
|
||||||
|
if second, err2 := measure(); err2 == nil {
|
||||||
|
if second <= int64(highLatencyThreshold) {
|
||||||
|
latency = second
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i == attempts-1 { // 最后一次仍高
|
||||||
|
err = errors.New("latency remains high after retries")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = err2
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Ping task %d failed: %v", taskID, err)
|
log.Printf("Ping task %d failed: %v", taskID, err)
|
||||||
pingResult = -1 // 如果有错误,设置结果为 -1
|
pingResult = -1 // 如果有错误,设置结果为 -1
|
||||||
|
} else {
|
||||||
|
pingResult = int(latency)
|
||||||
}
|
}
|
||||||
payload := map[string]interface{}{
|
payload := map[string]interface{}{
|
||||||
"type": "ping_result",
|
"type": "ping_result",
|
||||||
|
Reference in New Issue
Block a user