nezha/cmd/dashboard/rpc/rpc.go

78 lines
2.6 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package rpc
import (
2024-10-22 11:44:50 -04:00
"net/http"
2019-12-08 03:59:58 -05:00
"google.golang.org/grpc"
"github.com/naiba/nezha/model"
2020-11-10 21:07:45 -05:00
pb "github.com/naiba/nezha/proto"
rpcService "github.com/naiba/nezha/service/rpc"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-08 03:59:58 -05:00
)
2024-10-22 11:44:50 -04:00
func ServeRPC() http.Handler {
2019-12-08 03:59:58 -05:00
server := grpc.NewServer()
rpcService.NezhaHandlerSingleton = rpcService.NewNezhaHandler()
pb.RegisterNezhaServiceServer(server, rpcService.NezhaHandlerSingleton)
2024-10-22 11:44:50 -04:00
return server
2019-12-08 03:59:58 -05:00
}
func DispatchTask(serviceSentinelDispatchBus <-chan model.Monitor) {
workedServerIndex := 0
for task := range serviceSentinelDispatchBus {
round := 0
endIndex := workedServerIndex
2022-01-08 22:54:14 -05:00
singleton.SortedServerLock.RLock()
// 如果已经轮了一整圈又轮到自己,没有合适机器去请求,跳出循环
for round < 1 || workedServerIndex < endIndex {
// 如果到了圈尾,再回到圈头,圈数加一,游标重置
2022-01-08 22:54:14 -05:00
if workedServerIndex >= len(singleton.SortedServerList) {
workedServerIndex = 0
round++
continue
}
// 如果服务器不在线,跳过这个服务器
2022-01-08 22:54:14 -05:00
if singleton.SortedServerList[workedServerIndex].TaskStream == nil {
workedServerIndex++
continue
}
// 如果此任务不可使用此服务器请求,跳过这个服务器(有些 IPv6 only 开了 NAT64 的机器请求 IPv4 总会出问题)
2022-01-08 22:54:14 -05:00
if (task.Cover == model.MonitorCoverAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) ||
(task.Cover == model.MonitorCoverIgnoreAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) {
workedServerIndex++
continue
}
if task.Cover == model.MonitorCoverIgnoreAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
workedServerIndex++
continue
}
if task.Cover == model.MonitorCoverAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
workedServerIndex++
continue
}
// 找到合适机器执行任务,跳出循环
// singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
// workedServerIndex++
// break
}
2022-01-08 22:54:14 -05:00
singleton.SortedServerLock.RUnlock()
}
}
func DispatchKeepalive() {
2022-01-08 22:54:14 -05:00
singleton.Cron.AddFunc("@every 60s", func() {
singleton.SortedServerLock.RLock()
defer singleton.SortedServerLock.RUnlock()
for i := 0; i < len(singleton.SortedServerList); i++ {
if singleton.SortedServerList[i] == nil || singleton.SortedServerList[i].TaskStream == nil {
continue
}
2022-01-08 22:54:14 -05:00
singleton.SortedServerList[i].TaskStream.Send(&pb.Task{Type: model.TaskTypeKeepalive})
}
})
}