Skip to content

Commit 59caca6

Browse files
committed
Update.
1 parent abcdc72 commit 59caca6

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

cmd/dashboard/controller/controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ import (
2626
"github.com/xos/serverstatus/service/singleton"
2727
)
2828

29+
// handleBrokenPipe 中间件处理broken pipe错误
30+
func handleBrokenPipe(c *gin.Context) {
31+
defer func() {
32+
if err := recover(); err != nil {
33+
// 检查是否为broken pipe错误
34+
if errStr := fmt.Sprintf("%v", err); strings.Contains(errStr, "broken pipe") ||
35+
strings.Contains(errStr, "connection reset") ||
36+
strings.Contains(errStr, "use of closed network connection") {
37+
// 静默处理broken pipe错误,不记录日志
38+
c.Abort()
39+
return
40+
}
41+
// 其他错误正常处理
42+
log.Printf("HTTP处理错误: %v", err)
43+
c.Abort()
44+
}
45+
}()
46+
c.Next()
47+
}
48+
2949
func ServeWeb(port uint) *http.Server {
3050
gin.SetMode(gin.ReleaseMode)
3151
r := gin.Default()
@@ -34,6 +54,7 @@ func ServeWeb(port uint) *http.Server {
3454
pprof.Register(r)
3555
}
3656
r.Use(natGateway)
57+
r.Use(handleBrokenPipe) // 添加broken pipe错误处理中间件
3758
tmpl := template.New("").Funcs(funcMap)
3859
var err error
3960
// 直接用本地模板目录

cmd/dashboard/rpc/rpc.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Monitor) {
6262
round := 0
6363
endIndex := workedServerIndex
6464
singleton.SortedServerLock.RLock()
65+
66+
// 安全检查:确保SortedServerList不为空
67+
if len(singleton.SortedServerList) == 0 {
68+
singleton.SortedServerLock.RUnlock()
69+
continue
70+
}
71+
6572
// 如果已经轮了一整圈又轮到自己,没有合适机器去请求,跳出循环
6673
for round < 1 || workedServerIndex < endIndex {
6774
// 如果到了圈尾,再回到圈头,圈数加一,游标重置
@@ -70,29 +77,50 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Monitor) {
7077
round++
7178
continue
7279
}
80+
81+
// 安全检查:确保服务器不为nil
82+
currentServer := singleton.SortedServerList[workedServerIndex]
83+
if currentServer == nil {
84+
workedServerIndex++
85+
continue
86+
}
87+
7388
// 如果服务器不在线,跳过这个服务器
74-
if singleton.SortedServerList[workedServerIndex].TaskStream == nil {
89+
if currentServer.TaskStream == nil {
7590
workedServerIndex++
7691
continue
7792
}
93+
94+
// 安全检查:确保SkipServers不为nil
95+
skipServers := task.SkipServers
96+
if skipServers == nil {
97+
skipServers = make(map[uint64]bool)
98+
}
99+
78100
// 如果此任务不可使用此服务器请求,跳过这个服务器(有些 IPv6 only 开了 NAT64 的机器请求 IPv4 总会出问题)
79-
if (task.Cover == model.MonitorCoverAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) ||
80-
(task.Cover == model.MonitorCoverIgnoreAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) {
101+
if (task.Cover == model.MonitorCoverAll && skipServers[currentServer.ID]) ||
102+
(task.Cover == model.MonitorCoverIgnoreAll && !skipServers[currentServer.ID]) {
81103
workedServerIndex++
82104
continue
83105
}
84-
if task.Cover == model.MonitorCoverIgnoreAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
85-
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
106+
if task.Cover == model.MonitorCoverIgnoreAll && skipServers[currentServer.ID] {
107+
if err := currentServer.TaskStream.Send(task.PB()); err != nil {
108+
log.Printf("DispatchTask: 发送任务到服务器 %d 失败: %v", currentServer.ID, err)
109+
}
86110
workedServerIndex++
87111
continue
88112
}
89-
if task.Cover == model.MonitorCoverAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
90-
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
113+
if task.Cover == model.MonitorCoverAll && !skipServers[currentServer.ID] {
114+
if err := currentServer.TaskStream.Send(task.PB()); err != nil {
115+
log.Printf("DispatchTask: 发送任务到服务器 %d 失败: %v", currentServer.ID, err)
116+
}
91117
workedServerIndex++
92118
continue
93119
}
94120
// 找到合适机器执行任务,跳出循环
95-
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
121+
if err := currentServer.TaskStream.Send(task.PB()); err != nil {
122+
log.Printf("DispatchTask: 发送任务到服务器 %d 失败: %v", currentServer.ID, err)
123+
}
96124
workedServerIndex++
97125
break
98126
}

0 commit comments

Comments
 (0)