improve: use stream reduce auth check

This commit is contained in:
naiba 2024-11-23 12:41:25 +08:00
parent 3dfb62287b
commit 7d04e739e9
4 changed files with 241 additions and 145 deletions

View File

@ -254,9 +254,6 @@ func run() {
}() }()
} }
// 上报服务器信息
go reportStateDaemon()
// 定时检查更新 // 定时检查更新
if _, err := semver.Parse(version); err == nil && !agentConfig.DisableAutoUpdate { if _, err := semver.Parse(version); err == nil && !agentConfig.DisableAutoUpdate {
doSelfUpdate(true) doSelfUpdate(true)
@ -309,6 +306,9 @@ func run() {
} }
cancel() cancel()
initialized = true initialized = true
errCh := make(chan error)
// 执行 Task // 执行 Task
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB()) tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
if err != nil { if err != nil {
@ -316,8 +316,26 @@ func run() {
retry() retry()
continue continue
} }
err = receiveTasks(tasks) go receiveTasks(tasks, errCh)
printf("receiveTasks exit to main: %v", err)
reportState, err := client.ReportSystemState(context.Background())
if err != nil {
printf("上报状态信息失败: %v", err)
retry()
continue
}
go reportStateDaemon(reportState, errCh)
for i := 0; i < 2; i++ {
err = <-errCh
if i == 0 {
tasks.CloseSend()
reportState.CloseSend()
}
printf("worker exit to main: %v", err)
}
close(errCh)
retry() retry()
} }
} }
@ -384,14 +402,14 @@ func runService(action string, path string) {
} }
} }
func receiveTasks(tasks pb.NezhaService_RequestTaskClient) error { func receiveTasks(tasks pb.NezhaService_RequestTaskClient, errCh chan<- error) {
var task *pb.Task
var err error var err error
defer printf("receiveTasks exit %v => %v", time.Now(), err)
for { for {
var task *pb.Task
task, err = tasks.Recv() task, err = tasks.Recv()
if err != nil { if err != nil {
return err errCh <- fmt.Errorf("receiveTasks exit: %v", err)
return
} }
go func() { go func() {
defer func() { defer func() {
@ -427,6 +445,7 @@ func doTask(task *pb.Task) {
return return
case model.TaskTypeReportHostInfo: case model.TaskTypeReportHostInfo:
reportHost() reportHost()
monitor.GeoQueryIPChanged = true
reportGeoIP(agentConfig.UseIPv6CountryCode) reportGeoIP(agentConfig.UseIPv6CountryCode)
return return
case model.TaskTypeFM: case model.TaskTypeFM:
@ -442,43 +461,42 @@ func doTask(task *pb.Task) {
} }
// reportStateDaemon 向server上报状态信息 // reportStateDaemon 向server上报状态信息
func reportStateDaemon() { func reportStateDaemon(stateClient pb.NezhaService_ReportSystemStateClient, errCh chan<- error) {
var lastReportHostInfo, lastReportIPInfo time.Time var lastReportHostInfo, lastReportIPInfo time.Time
var err error var err error
defer printf("reportState exit %v => %v", time.Now(), err)
for { for {
// 为了更准确的记录时段流量inited 后再上传状态信息 // 为了更准确的记录时段流量inited 后再上传状态信息
lastReportHostInfo, lastReportIPInfo = reportState(lastReportHostInfo, lastReportIPInfo) lastReportHostInfo, lastReportIPInfo, err = reportState(stateClient, lastReportHostInfo, lastReportIPInfo)
if err != nil {
errCh <- fmt.Errorf("reportStateDaemon exit: %v", err)
return
}
time.Sleep(time.Second * time.Duration(agentConfig.ReportDelay)) time.Sleep(time.Second * time.Duration(agentConfig.ReportDelay))
} }
} }
func reportState(host, ip time.Time) (time.Time, time.Time) { func reportState(statClient pb.NezhaService_ReportSystemStateClient, host, ip time.Time) (time.Time, time.Time, error) {
if client != nil && initialized { monitor.TrackNetworkSpeed()
monitor.TrackNetworkSpeed() if err := statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()); err != nil {
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) return host, ip, err
_, err := client.ReportSystemState(timeOutCtx, monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()) }
cancel() _, err := statClient.Recv()
if err != nil { if err != nil {
printf("reportState error: %v", err) return host, ip, err
time.Sleep(delayWhenError) }
} // 每10分钟重新获取一次硬件信息
if host.Before(time.Now().Add(-10 * time.Minute)) {
// 每10分钟重新获取一次硬件信息 if reportHost() {
if host.Before(time.Now().Add(-10 * time.Minute)) { host = time.Now()
if reportHost() {
host = time.Now()
}
}
// 更新IP信息
if time.Since(ip) > time.Second*time.Duration(agentConfig.IPReportPeriod) {
if reportGeoIP(agentConfig.UseIPv6CountryCode) {
ip = time.Now()
}
} }
} }
return host, ip // 更新IP信息
if time.Since(ip) > time.Second*time.Duration(agentConfig.IPReportPeriod) {
if reportGeoIP(agentConfig.UseIPv6CountryCode) {
ip = time.Now()
}
}
return host, ip, nil
} }
func reportHost() bool { func reportHost() bool {
@ -506,6 +524,7 @@ func reportGeoIP(use6 bool) bool {
geoip, err := client.ReportGeoIP(context.Background(), pbg) geoip, err := client.ReportGeoIP(context.Background(), pbg)
if err == nil { if err == nil {
monitor.CachedCountryCode = geoip.GetCountryCode() monitor.CachedCountryCode = geoip.GetCountryCode()
monitor.GeoQueryIPChanged = false
} }
} else { } else {
return false return false

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.34.1 // protoc-gen-go v1.34.2
// protoc v5.28.3 // protoc v5.28.1
// source: proto/nezha.proto // source: proto/nezha.proto
package proto package proto
@ -820,28 +820,28 @@ var file_proto_nezha_proto_rawDesc = []byte{
0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x02, 0x49, 0x50, 0x12, 0x12, 0x0a, 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x02, 0x49, 0x50, 0x12, 0x12, 0x0a,
0x04, 0x69, 0x70, 0x76, 0x34, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x70, 0x76, 0x04, 0x69, 0x70, 0x76, 0x34, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x70, 0x76,
0x34, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x70, 0x76, 0x36, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x34, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x70, 0x76, 0x36, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0xbf, 0x02, 0x0a, 0x0c, 0x4e, 0x65, 0x7a, 0x68, 0x61, 0x53, 0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0xc3, 0x02, 0x0a, 0x0c, 0x4e, 0x65, 0x7a, 0x68, 0x61, 0x53,
0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x33, 0x0a, 0x11, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x37, 0x0a, 0x11, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74,
0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0c, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x10, 0x52, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12,
0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x31, 0x0a, 0x10, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49,
0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x6e, 0x66, 0x6f, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74,
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x31, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74,
0x0a, 0x0a, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x11, 0x2e, 0x70, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0a, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x54, 0x61, 0x73, 0x6b,
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x1a, 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73,
0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x75, 0x6c, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65,
0x00, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x61, 0x73, 0x6b, 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x22, 0x00, 0x30, 0x01, 0x12, 0x3a, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x22, 0x00,
0x0a, 0x08, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x30, 0x01, 0x12, 0x3a, 0x0a, 0x08, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x13,
0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x1a, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44,
0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x61, 0x74, 0x61, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74,
0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b,
0x70, 0x6f, 0x72, 0x74, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x0c, 0x2e,
0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x1a, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x1a, 0x0c, 0x2e, 0x70, 0x72,
0x47, 0x65, 0x6f, 0x49, 0x50, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e,
0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@ -857,7 +857,7 @@ func file_proto_nezha_proto_rawDescGZIP() []byte {
} }
var file_proto_nezha_proto_msgTypes = make([]protoimpl.MessageInfo, 9) var file_proto_nezha_proto_msgTypes = make([]protoimpl.MessageInfo, 9)
var file_proto_nezha_proto_goTypes = []interface{}{ var file_proto_nezha_proto_goTypes = []any{
(*Host)(nil), // 0: proto.Host (*Host)(nil), // 0: proto.Host
(*State)(nil), // 1: proto.State (*State)(nil), // 1: proto.State
(*State_SensorTemperature)(nil), // 2: proto.State_SensorTemperature (*State_SensorTemperature)(nil), // 2: proto.State_SensorTemperature
@ -896,7 +896,7 @@ func file_proto_nezha_proto_init() {
return return
} }
if !protoimpl.UnsafeEnabled { if !protoimpl.UnsafeEnabled {
file_proto_nezha_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[0].Exporter = func(v any, i int) any {
switch v := v.(*Host); i { switch v := v.(*Host); i {
case 0: case 0:
return &v.state return &v.state
@ -908,7 +908,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[1].Exporter = func(v any, i int) any {
switch v := v.(*State); i { switch v := v.(*State); i {
case 0: case 0:
return &v.state return &v.state
@ -920,7 +920,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[2].Exporter = func(v any, i int) any {
switch v := v.(*State_SensorTemperature); i { switch v := v.(*State_SensorTemperature); i {
case 0: case 0:
return &v.state return &v.state
@ -932,7 +932,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[3].Exporter = func(v any, i int) any {
switch v := v.(*Task); i { switch v := v.(*Task); i {
case 0: case 0:
return &v.state return &v.state
@ -944,7 +944,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[4].Exporter = func(v any, i int) any {
switch v := v.(*TaskResult); i { switch v := v.(*TaskResult); i {
case 0: case 0:
return &v.state return &v.state
@ -956,7 +956,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[5].Exporter = func(v any, i int) any {
switch v := v.(*Receipt); i { switch v := v.(*Receipt); i {
case 0: case 0:
return &v.state return &v.state
@ -968,7 +968,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[6].Exporter = func(v any, i int) any {
switch v := v.(*IOStreamData); i { switch v := v.(*IOStreamData); i {
case 0: case 0:
return &v.state return &v.state
@ -980,7 +980,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[7].Exporter = func(v any, i int) any {
switch v := v.(*GeoIP); i { switch v := v.(*GeoIP); i {
case 0: case 0:
return &v.state return &v.state
@ -992,7 +992,7 @@ func file_proto_nezha_proto_init() {
return nil return nil
} }
} }
file_proto_nezha_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { file_proto_nezha_proto_msgTypes[8].Exporter = func(v any, i int) any {
switch v := v.(*IP); i { switch v := v.(*IP); i {
case 0: case 0:
return &v.state return &v.state

View File

@ -4,7 +4,7 @@ option go_package = "./proto";
package proto; package proto;
service NezhaService { service NezhaService {
rpc ReportSystemState(State) returns (Receipt) {} rpc ReportSystemState(stream State) returns (stream Receipt) {}
rpc ReportSystemInfo(Host) returns (Receipt) {} rpc ReportSystemInfo(Host) returns (Receipt) {}
rpc ReportTask(TaskResult) returns (Receipt) {} rpc ReportTask(TaskResult) returns (Receipt) {}
rpc RequestTask(Host) returns (stream Task) {} rpc RequestTask(Host) returns (stream Task) {}

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT. // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions: // versions:
// - protoc-gen-go-grpc v1.5.1 // - protoc-gen-go-grpc v1.3.0
// - protoc v5.28.3 // - protoc v5.28.1
// source: proto/nezha.proto // source: proto/nezha.proto
package proto package proto
@ -15,8 +15,8 @@ import (
// This is a compile-time assertion to ensure that this generated file // This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against. // is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.64.0 or later. // Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion9 const _ = grpc.SupportPackageIsVersion7
const ( const (
NezhaService_ReportSystemState_FullMethodName = "/proto.NezhaService/ReportSystemState" NezhaService_ReportSystemState_FullMethodName = "/proto.NezhaService/ReportSystemState"
@ -31,11 +31,11 @@ const (
// //
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type NezhaServiceClient interface { type NezhaServiceClient interface {
ReportSystemState(ctx context.Context, in *State, opts ...grpc.CallOption) (*Receipt, error) ReportSystemState(ctx context.Context, opts ...grpc.CallOption) (NezhaService_ReportSystemStateClient, error)
ReportSystemInfo(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Receipt, error) ReportSystemInfo(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Receipt, error)
ReportTask(ctx context.Context, in *TaskResult, opts ...grpc.CallOption) (*Receipt, error) ReportTask(ctx context.Context, in *TaskResult, opts ...grpc.CallOption) (*Receipt, error)
RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (NezhaService_RequestTaskClient, error)
IOStream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[IOStreamData, IOStreamData], error) IOStream(ctx context.Context, opts ...grpc.CallOption) (NezhaService_IOStreamClient, error)
ReportGeoIP(ctx context.Context, in *GeoIP, opts ...grpc.CallOption) (*GeoIP, error) ReportGeoIP(ctx context.Context, in *GeoIP, opts ...grpc.CallOption) (*GeoIP, error)
} }
@ -47,20 +47,40 @@ func NewNezhaServiceClient(cc grpc.ClientConnInterface) NezhaServiceClient {
return &nezhaServiceClient{cc} return &nezhaServiceClient{cc}
} }
func (c *nezhaServiceClient) ReportSystemState(ctx context.Context, in *State, opts ...grpc.CallOption) (*Receipt, error) { func (c *nezhaServiceClient) ReportSystemState(ctx context.Context, opts ...grpc.CallOption) (NezhaService_ReportSystemStateClient, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[0], NezhaService_ReportSystemState_FullMethodName, opts...)
out := new(Receipt)
err := c.cc.Invoke(ctx, NezhaService_ReportSystemState_FullMethodName, in, out, cOpts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return out, nil x := &nezhaServiceReportSystemStateClient{stream}
return x, nil
}
type NezhaService_ReportSystemStateClient interface {
Send(*State) error
Recv() (*Receipt, error)
grpc.ClientStream
}
type nezhaServiceReportSystemStateClient struct {
grpc.ClientStream
}
func (x *nezhaServiceReportSystemStateClient) Send(m *State) error {
return x.ClientStream.SendMsg(m)
}
func (x *nezhaServiceReportSystemStateClient) Recv() (*Receipt, error) {
m := new(Receipt)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
} }
func (c *nezhaServiceClient) ReportSystemInfo(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Receipt, error) { func (c *nezhaServiceClient) ReportSystemInfo(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Receipt, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(Receipt) out := new(Receipt)
err := c.cc.Invoke(ctx, NezhaService_ReportSystemInfo_FullMethodName, in, out, cOpts...) err := c.cc.Invoke(ctx, NezhaService_ReportSystemInfo_FullMethodName, in, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -68,22 +88,20 @@ func (c *nezhaServiceClient) ReportSystemInfo(ctx context.Context, in *Host, opt
} }
func (c *nezhaServiceClient) ReportTask(ctx context.Context, in *TaskResult, opts ...grpc.CallOption) (*Receipt, error) { func (c *nezhaServiceClient) ReportTask(ctx context.Context, in *TaskResult, opts ...grpc.CallOption) (*Receipt, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(Receipt) out := new(Receipt)
err := c.cc.Invoke(ctx, NezhaService_ReportTask_FullMethodName, in, out, cOpts...) err := c.cc.Invoke(ctx, NezhaService_ReportTask_FullMethodName, in, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return out, nil return out, nil
} }
func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) { func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (NezhaService_RequestTaskClient, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[1], NezhaService_RequestTask_FullMethodName, opts...)
stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[0], NezhaService_RequestTask_FullMethodName, cOpts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
x := &grpc.GenericClientStream[Host, Task]{ClientStream: stream} x := &nezhaServiceRequestTaskClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil { if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err return nil, err
} }
@ -93,26 +111,57 @@ func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ...
return x, nil return x, nil
} }
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type NezhaService_RequestTaskClient interface {
type NezhaService_RequestTaskClient = grpc.ServerStreamingClient[Task] Recv() (*Task, error)
grpc.ClientStream
}
func (c *nezhaServiceClient) IOStream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[IOStreamData, IOStreamData], error) { type nezhaServiceRequestTaskClient struct {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) grpc.ClientStream
stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[1], NezhaService_IOStream_FullMethodName, cOpts...) }
func (x *nezhaServiceRequestTaskClient) Recv() (*Task, error) {
m := new(Task)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *nezhaServiceClient) IOStream(ctx context.Context, opts ...grpc.CallOption) (NezhaService_IOStreamClient, error) {
stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[2], NezhaService_IOStream_FullMethodName, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
x := &grpc.GenericClientStream[IOStreamData, IOStreamData]{ClientStream: stream} x := &nezhaServiceIOStreamClient{stream}
return x, nil return x, nil
} }
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type NezhaService_IOStreamClient interface {
type NezhaService_IOStreamClient = grpc.BidiStreamingClient[IOStreamData, IOStreamData] Send(*IOStreamData) error
Recv() (*IOStreamData, error)
grpc.ClientStream
}
type nezhaServiceIOStreamClient struct {
grpc.ClientStream
}
func (x *nezhaServiceIOStreamClient) Send(m *IOStreamData) error {
return x.ClientStream.SendMsg(m)
}
func (x *nezhaServiceIOStreamClient) Recv() (*IOStreamData, error) {
m := new(IOStreamData)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *nezhaServiceClient) ReportGeoIP(ctx context.Context, in *GeoIP, opts ...grpc.CallOption) (*GeoIP, error) { func (c *nezhaServiceClient) ReportGeoIP(ctx context.Context, in *GeoIP, opts ...grpc.CallOption) (*GeoIP, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GeoIP) out := new(GeoIP)
err := c.cc.Invoke(ctx, NezhaService_ReportGeoIP_FullMethodName, in, out, cOpts...) err := c.cc.Invoke(ctx, NezhaService_ReportGeoIP_FullMethodName, in, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,25 +170,22 @@ func (c *nezhaServiceClient) ReportGeoIP(ctx context.Context, in *GeoIP, opts ..
// NezhaServiceServer is the server API for NezhaService service. // NezhaServiceServer is the server API for NezhaService service.
// All implementations should embed UnimplementedNezhaServiceServer // All implementations should embed UnimplementedNezhaServiceServer
// for forward compatibility. // for forward compatibility
type NezhaServiceServer interface { type NezhaServiceServer interface {
ReportSystemState(context.Context, *State) (*Receipt, error) ReportSystemState(NezhaService_ReportSystemStateServer) error
ReportSystemInfo(context.Context, *Host) (*Receipt, error) ReportSystemInfo(context.Context, *Host) (*Receipt, error)
ReportTask(context.Context, *TaskResult) (*Receipt, error) ReportTask(context.Context, *TaskResult) (*Receipt, error)
RequestTask(*Host, grpc.ServerStreamingServer[Task]) error RequestTask(*Host, NezhaService_RequestTaskServer) error
IOStream(grpc.BidiStreamingServer[IOStreamData, IOStreamData]) error IOStream(NezhaService_IOStreamServer) error
ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error)
} }
// UnimplementedNezhaServiceServer should be embedded to have // UnimplementedNezhaServiceServer should be embedded to have forward compatible implementations.
// forward compatible implementations. type UnimplementedNezhaServiceServer struct {
// }
// NOTE: this should be embedded by value instead of pointer to avoid a nil
// pointer dereference when methods are called.
type UnimplementedNezhaServiceServer struct{}
func (UnimplementedNezhaServiceServer) ReportSystemState(context.Context, *State) (*Receipt, error) { func (UnimplementedNezhaServiceServer) ReportSystemState(NezhaService_ReportSystemStateServer) error {
return nil, status.Errorf(codes.Unimplemented, "method ReportSystemState not implemented") return status.Errorf(codes.Unimplemented, "method ReportSystemState not implemented")
} }
func (UnimplementedNezhaServiceServer) ReportSystemInfo(context.Context, *Host) (*Receipt, error) { func (UnimplementedNezhaServiceServer) ReportSystemInfo(context.Context, *Host) (*Receipt, error) {
return nil, status.Errorf(codes.Unimplemented, "method ReportSystemInfo not implemented") return nil, status.Errorf(codes.Unimplemented, "method ReportSystemInfo not implemented")
@ -147,16 +193,15 @@ func (UnimplementedNezhaServiceServer) ReportSystemInfo(context.Context, *Host)
func (UnimplementedNezhaServiceServer) ReportTask(context.Context, *TaskResult) (*Receipt, error) { func (UnimplementedNezhaServiceServer) ReportTask(context.Context, *TaskResult) (*Receipt, error) {
return nil, status.Errorf(codes.Unimplemented, "method ReportTask not implemented") return nil, status.Errorf(codes.Unimplemented, "method ReportTask not implemented")
} }
func (UnimplementedNezhaServiceServer) RequestTask(*Host, grpc.ServerStreamingServer[Task]) error { func (UnimplementedNezhaServiceServer) RequestTask(*Host, NezhaService_RequestTaskServer) error {
return status.Errorf(codes.Unimplemented, "method RequestTask not implemented") return status.Errorf(codes.Unimplemented, "method RequestTask not implemented")
} }
func (UnimplementedNezhaServiceServer) IOStream(grpc.BidiStreamingServer[IOStreamData, IOStreamData]) error { func (UnimplementedNezhaServiceServer) IOStream(NezhaService_IOStreamServer) error {
return status.Errorf(codes.Unimplemented, "method IOStream not implemented") return status.Errorf(codes.Unimplemented, "method IOStream not implemented")
} }
func (UnimplementedNezhaServiceServer) ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) { func (UnimplementedNezhaServiceServer) ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) {
return nil, status.Errorf(codes.Unimplemented, "method ReportGeoIP not implemented") return nil, status.Errorf(codes.Unimplemented, "method ReportGeoIP not implemented")
} }
func (UnimplementedNezhaServiceServer) testEmbeddedByValue() {}
// UnsafeNezhaServiceServer may be embedded to opt out of forward compatibility for this service. // UnsafeNezhaServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to NezhaServiceServer will // Use of this interface is not recommended, as added methods to NezhaServiceServer will
@ -166,32 +211,33 @@ type UnsafeNezhaServiceServer interface {
} }
func RegisterNezhaServiceServer(s grpc.ServiceRegistrar, srv NezhaServiceServer) { func RegisterNezhaServiceServer(s grpc.ServiceRegistrar, srv NezhaServiceServer) {
// If the following call panics, it indicates UnimplementedNezhaServiceServer was
// embedded by pointer and is nil. This will cause panics if an
// unimplemented method is ever invoked, so we test this at initialization
// time to prevent it from happening at runtime later due to I/O.
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
t.testEmbeddedByValue()
}
s.RegisterService(&NezhaService_ServiceDesc, srv) s.RegisterService(&NezhaService_ServiceDesc, srv)
} }
func _NezhaService_ReportSystemState_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _NezhaService_ReportSystemState_Handler(srv interface{}, stream grpc.ServerStream) error {
in := new(State) return srv.(NezhaServiceServer).ReportSystemState(&nezhaServiceReportSystemStateServer{stream})
if err := dec(in); err != nil { }
type NezhaService_ReportSystemStateServer interface {
Send(*Receipt) error
Recv() (*State, error)
grpc.ServerStream
}
type nezhaServiceReportSystemStateServer struct {
grpc.ServerStream
}
func (x *nezhaServiceReportSystemStateServer) Send(m *Receipt) error {
return x.ServerStream.SendMsg(m)
}
func (x *nezhaServiceReportSystemStateServer) Recv() (*State, error) {
m := new(State)
if err := x.ServerStream.RecvMsg(m); err != nil {
return nil, err return nil, err
} }
if interceptor == nil { return m, nil
return srv.(NezhaServiceServer).ReportSystemState(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: NezhaService_ReportSystemState_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NezhaServiceServer).ReportSystemState(ctx, req.(*State))
}
return interceptor(ctx, in, info, handler)
} }
func _NezhaService_ReportSystemInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _NezhaService_ReportSystemInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
@ -235,18 +281,47 @@ func _NezhaService_RequestTask_Handler(srv interface{}, stream grpc.ServerStream
if err := stream.RecvMsg(m); err != nil { if err := stream.RecvMsg(m); err != nil {
return err return err
} }
return srv.(NezhaServiceServer).RequestTask(m, &grpc.GenericServerStream[Host, Task]{ServerStream: stream}) return srv.(NezhaServiceServer).RequestTask(m, &nezhaServiceRequestTaskServer{stream})
} }
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type NezhaService_RequestTaskServer interface {
type NezhaService_RequestTaskServer = grpc.ServerStreamingServer[Task] Send(*Task) error
grpc.ServerStream
}
type nezhaServiceRequestTaskServer struct {
grpc.ServerStream
}
func (x *nezhaServiceRequestTaskServer) Send(m *Task) error {
return x.ServerStream.SendMsg(m)
}
func _NezhaService_IOStream_Handler(srv interface{}, stream grpc.ServerStream) error { func _NezhaService_IOStream_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(NezhaServiceServer).IOStream(&grpc.GenericServerStream[IOStreamData, IOStreamData]{ServerStream: stream}) return srv.(NezhaServiceServer).IOStream(&nezhaServiceIOStreamServer{stream})
} }
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type NezhaService_IOStreamServer interface {
type NezhaService_IOStreamServer = grpc.BidiStreamingServer[IOStreamData, IOStreamData] Send(*IOStreamData) error
Recv() (*IOStreamData, error)
grpc.ServerStream
}
type nezhaServiceIOStreamServer struct {
grpc.ServerStream
}
func (x *nezhaServiceIOStreamServer) Send(m *IOStreamData) error {
return x.ServerStream.SendMsg(m)
}
func (x *nezhaServiceIOStreamServer) Recv() (*IOStreamData, error) {
m := new(IOStreamData)
if err := x.ServerStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func _NezhaService_ReportGeoIP_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _NezhaService_ReportGeoIP_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GeoIP) in := new(GeoIP)
@ -273,10 +348,6 @@ var NezhaService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "proto.NezhaService", ServiceName: "proto.NezhaService",
HandlerType: (*NezhaServiceServer)(nil), HandlerType: (*NezhaServiceServer)(nil),
Methods: []grpc.MethodDesc{ Methods: []grpc.MethodDesc{
{
MethodName: "ReportSystemState",
Handler: _NezhaService_ReportSystemState_Handler,
},
{ {
MethodName: "ReportSystemInfo", MethodName: "ReportSystemInfo",
Handler: _NezhaService_ReportSystemInfo_Handler, Handler: _NezhaService_ReportSystemInfo_Handler,
@ -291,6 +362,12 @@ var NezhaService_ServiceDesc = grpc.ServiceDesc{
}, },
}, },
Streams: []grpc.StreamDesc{ Streams: []grpc.StreamDesc{
{
StreamName: "ReportSystemState",
Handler: _NezhaService_ReportSystemState_Handler,
ServerStreams: true,
ClientStreams: true,
},
{ {
StreamName: "RequestTask", StreamName: "RequestTask",
Handler: _NezhaService_RequestTask_Handler, Handler: _NezhaService_RequestTask_Handler,