Skip to content

Commit

Permalink
feat: grpc keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Dec 4, 2024
1 parent 98dad74 commit b484fe5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
1 change: 0 additions & 1 deletion cmd/dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func main() {
singleton.CleanServiceHistory()
serviceSentinelDispatchBus := make(chan model.Service) // 用于传递服务监控任务信息的channel
go rpc.DispatchTask(serviceSentinelDispatchBus)
go rpc.DispatchKeepalive()
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)

Expand Down
24 changes: 9 additions & 15 deletions cmd/dashboard/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"

Expand All @@ -21,7 +22,14 @@ import (
)

func ServeRPC() *grpc.Server {
server := grpc.NewServer(grpc.ChainUnaryInterceptor(getRealIp, waf))
server := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(
keepalive.EnforcementPolicy{
MinTime: time.Second * 20,
PermitWithoutStream: true,
}), grpc.KeepaliveParams(
keepalive.ServerParameters{
Time: time.Second * 30,
}), grpc.ChainUnaryInterceptor(getRealIp, waf))
rpcService.NezhaHandlerSingleton = rpcService.NewNezhaHandler()
proto.RegisterNezhaServiceServer(server, rpcService.NezhaHandlerSingleton)
return server
Expand Down Expand Up @@ -116,20 +124,6 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
}
}

func DispatchKeepalive() {
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
}

singleton.SortedServerList[i].TaskStream.Send(&proto.Task{Type: model.TaskTypeKeepalive})
}
})
}

func ServeNAT(w http.ResponseWriter, r *http.Request, natConfig *model.NAT) {
singleton.ServerLock.RLock()
server := singleton.ServerList[natConfig.ServerID]
Expand Down
2 changes: 1 addition & 1 deletion model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
TaskTypeCommand
TaskTypeTerminal
TaskTypeUpgrade
TaskTypeKeepalive
TaskTypeKeepaliveDeprecated
TaskTypeTerminalGRPC
TaskTypeNAT
TaskTypeReportHostInfo
Expand Down

0 comments on commit b484fe5

Please sign in to comment.