diff --git a/cmd/agent/main.go b/cmd/agent/main.go index d21e089..55e4cd7 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -254,9 +254,6 @@ func run() { }() } - // 上报服务器信息 - go reportStateDaemon() - // 定时检查更新 if _, err := semver.Parse(version); err == nil && !agentConfig.DisableAutoUpdate { doSelfUpdate(true) @@ -309,6 +306,9 @@ func run() { } cancel() initialized = true + + errCh := make(chan error) + // 执行 Task tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB()) if err != nil { @@ -316,8 +316,26 @@ func run() { retry() continue } - err = receiveTasks(tasks) - printf("receiveTasks exit to main: %v", err) + go receiveTasks(tasks, errCh) + + 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() } } @@ -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 - defer printf("receiveTasks exit %v => %v", time.Now(), err) for { - var task *pb.Task task, err = tasks.Recv() if err != nil { - return err + errCh <- fmt.Errorf("receiveTasks exit: %v", err) + return } go func() { defer func() { @@ -427,6 +445,7 @@ func doTask(task *pb.Task) { return case model.TaskTypeReportHostInfo: reportHost() + monitor.GeoQueryIPChanged = true reportGeoIP(agentConfig.UseIPv6CountryCode) return case model.TaskTypeFM: @@ -442,43 +461,42 @@ func doTask(task *pb.Task) { } // reportStateDaemon 向server上报状态信息 -func reportStateDaemon() { +func reportStateDaemon(stateClient pb.NezhaService_ReportSystemStateClient, errCh chan<- error) { var lastReportHostInfo, lastReportIPInfo time.Time var err error - defer printf("reportState exit %v => %v", time.Now(), err) for { // 为了更准确的记录时段流量,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)) } } -func reportState(host, ip time.Time) (time.Time, time.Time) { - if client != nil && initialized { - monitor.TrackNetworkSpeed() - timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) - _, err := client.ReportSystemState(timeOutCtx, monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()) - cancel() - if err != nil { - printf("reportState error: %v", err) - time.Sleep(delayWhenError) - } - - // 每10分钟重新获取一次硬件信息 - if host.Before(time.Now().Add(-10 * time.Minute)) { - if reportHost() { - host = time.Now() - } - } - - // 更新IP信息 - if time.Since(ip) > time.Second*time.Duration(agentConfig.IPReportPeriod) { - if reportGeoIP(agentConfig.UseIPv6CountryCode) { - ip = time.Now() - } +func reportState(statClient pb.NezhaService_ReportSystemStateClient, host, ip time.Time) (time.Time, time.Time, error) { + monitor.TrackNetworkSpeed() + if err := statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()); err != nil { + return host, ip, err + } + _, err := statClient.Recv() + if err != nil { + return host, ip, err + } + // 每10分钟重新获取一次硬件信息 + if host.Before(time.Now().Add(-10 * time.Minute)) { + if reportHost() { + host = 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 { @@ -506,6 +524,7 @@ func reportGeoIP(use6 bool) bool { geoip, err := client.ReportGeoIP(context.Background(), pbg) if err == nil { monitor.CachedCountryCode = geoip.GetCountryCode() + monitor.GeoQueryIPChanged = false } } else { return false diff --git a/proto/nezha.pb.go b/proto/nezha.pb.go index ec5c12b..8573cb1 100644 --- a/proto/nezha.pb.go +++ b/proto/nezha.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.34.1 -// protoc v5.28.3 +// protoc-gen-go v1.34.2 +// protoc v5.28.1 // source: proto/nezha.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, 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, - 0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0xbf, 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, + 0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0xc3, 0x02, 0x0a, 0x0c, 0x4e, 0x65, 0x7a, 0x68, 0x61, 0x53, + 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, 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, - 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x6e, 0x66, 0x6f, 0x12, - 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x31, - 0x0a, 0x0a, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x11, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x1a, - 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, - 0x00, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x61, 0x73, 0x6b, - 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x0b, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x22, 0x00, 0x30, 0x01, 0x12, 0x3a, - 0x0a, 0x08, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x1a, - 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, - 0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, - 0x70, 0x6f, 0x72, 0x74, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x1a, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, - 0x47, 0x65, 0x6f, 0x49, 0x50, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, + 0x31, 0x0a, 0x10, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, + 0x6e, 0x66, 0x6f, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, + 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, + 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0a, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x54, 0x61, 0x73, 0x6b, + 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, + 0x75, 0x6c, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, + 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, + 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x22, 0x00, + 0x30, 0x01, 0x12, 0x3a, 0x0a, 0x08, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x13, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, + 0x61, 0x74, 0x61, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, + 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, + 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x0c, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x1a, 0x0c, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } 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_goTypes = []interface{}{ +var file_proto_nezha_proto_goTypes = []any{ (*Host)(nil), // 0: proto.Host (*State)(nil), // 1: proto.State (*State_SensorTemperature)(nil), // 2: proto.State_SensorTemperature @@ -896,7 +896,7 @@ func file_proto_nezha_proto_init() { return } 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 { case 0: return &v.state @@ -908,7 +908,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -920,7 +920,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -932,7 +932,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -944,7 +944,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -956,7 +956,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -968,7 +968,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -980,7 +980,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state @@ -992,7 +992,7 @@ func file_proto_nezha_proto_init() { 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 { case 0: return &v.state diff --git a/proto/nezha.proto b/proto/nezha.proto index 5f12b01..632bbd3 100644 --- a/proto/nezha.proto +++ b/proto/nezha.proto @@ -4,7 +4,7 @@ option go_package = "./proto"; package proto; service NezhaService { - rpc ReportSystemState(State) returns (Receipt) {} + rpc ReportSystemState(stream State) returns (stream Receipt) {} rpc ReportSystemInfo(Host) returns (Receipt) {} rpc ReportTask(TaskResult) returns (Receipt) {} rpc RequestTask(Host) returns (stream Task) {} diff --git a/proto/nezha_grpc.pb.go b/proto/nezha_grpc.pb.go index b719f76..0e80ed1 100644 --- a/proto/nezha_grpc.pb.go +++ b/proto/nezha_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.5.1 -// - protoc v5.28.3 +// - protoc-gen-go-grpc v1.3.0 +// - protoc v5.28.1 // source: proto/nezha.proto package proto @@ -15,8 +15,8 @@ import ( // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.64.0 or later. -const _ = grpc.SupportPackageIsVersion9 +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 const ( 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. 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) ReportTask(ctx context.Context, in *TaskResult, opts ...grpc.CallOption) (*Receipt, error) - RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) - IOStream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[IOStreamData, IOStreamData], error) + RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (NezhaService_RequestTaskClient, error) + IOStream(ctx context.Context, opts ...grpc.CallOption) (NezhaService_IOStreamClient, 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} } -func (c *nezhaServiceClient) ReportSystemState(ctx context.Context, in *State, opts ...grpc.CallOption) (*Receipt, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(Receipt) - err := c.cc.Invoke(ctx, NezhaService_ReportSystemState_FullMethodName, in, out, cOpts...) +func (c *nezhaServiceClient) ReportSystemState(ctx context.Context, opts ...grpc.CallOption) (NezhaService_ReportSystemStateClient, error) { + stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[0], NezhaService_ReportSystemState_FullMethodName, opts...) if err != nil { 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) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) 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 { 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) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) 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 { return nil, err } return out, nil } -func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[0], NezhaService_RequestTask_FullMethodName, cOpts...) +func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ...grpc.CallOption) (NezhaService_RequestTaskClient, error) { + stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[1], NezhaService_RequestTask_FullMethodName, opts...) if err != nil { return nil, err } - x := &grpc.GenericClientStream[Host, Task]{ClientStream: stream} + x := &nezhaServiceRequestTaskClient{stream} if err := x.ClientStream.SendMsg(in); err != nil { return nil, err } @@ -93,26 +111,57 @@ func (c *nezhaServiceClient) RequestTask(ctx context.Context, in *Host, opts ... 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 = grpc.ServerStreamingClient[Task] +type NezhaService_RequestTaskClient interface { + Recv() (*Task, error) + grpc.ClientStream +} -func (c *nezhaServiceClient) IOStream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[IOStreamData, IOStreamData], error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - stream, err := c.cc.NewStream(ctx, &NezhaService_ServiceDesc.Streams[1], NezhaService_IOStream_FullMethodName, cOpts...) +type nezhaServiceRequestTaskClient struct { + grpc.ClientStream +} + +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 { return nil, err } - x := &grpc.GenericClientStream[IOStreamData, IOStreamData]{ClientStream: stream} + x := &nezhaServiceIOStreamClient{stream} 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 = grpc.BidiStreamingClient[IOStreamData, IOStreamData] +type NezhaService_IOStreamClient interface { + 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) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) 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 { 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. // All implementations should embed UnimplementedNezhaServiceServer -// for forward compatibility. +// for forward compatibility type NezhaServiceServer interface { - ReportSystemState(context.Context, *State) (*Receipt, error) + ReportSystemState(NezhaService_ReportSystemStateServer) error ReportSystemInfo(context.Context, *Host) (*Receipt, error) ReportTask(context.Context, *TaskResult) (*Receipt, error) - RequestTask(*Host, grpc.ServerStreamingServer[Task]) error - IOStream(grpc.BidiStreamingServer[IOStreamData, IOStreamData]) error + RequestTask(*Host, NezhaService_RequestTaskServer) error + IOStream(NezhaService_IOStreamServer) error ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) } -// UnimplementedNezhaServiceServer should be embedded to have -// forward compatible implementations. -// -// NOTE: this should be embedded by value instead of pointer to avoid a nil -// pointer dereference when methods are called. -type UnimplementedNezhaServiceServer struct{} +// UnimplementedNezhaServiceServer should be embedded to have forward compatible implementations. +type UnimplementedNezhaServiceServer struct { +} -func (UnimplementedNezhaServiceServer) ReportSystemState(context.Context, *State) (*Receipt, error) { - return nil, status.Errorf(codes.Unimplemented, "method ReportSystemState not implemented") +func (UnimplementedNezhaServiceServer) ReportSystemState(NezhaService_ReportSystemStateServer) error { + return status.Errorf(codes.Unimplemented, "method ReportSystemState not implemented") } func (UnimplementedNezhaServiceServer) ReportSystemInfo(context.Context, *Host) (*Receipt, error) { 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) { 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") } -func (UnimplementedNezhaServiceServer) IOStream(grpc.BidiStreamingServer[IOStreamData, IOStreamData]) error { +func (UnimplementedNezhaServiceServer) IOStream(NezhaService_IOStreamServer) error { return status.Errorf(codes.Unimplemented, "method IOStream not implemented") } func (UnimplementedNezhaServiceServer) ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) { 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. // 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) { - // 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) } -func _NezhaService_ReportSystemState_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(State) - if err := dec(in); err != nil { +func _NezhaService_ReportSystemState_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(NezhaServiceServer).ReportSystemState(&nezhaServiceReportSystemStateServer{stream}) +} + +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 } - if interceptor == 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) + return m, nil } 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 { 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 = grpc.ServerStreamingServer[Task] +type NezhaService_RequestTaskServer interface { + 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 { - 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 = grpc.BidiStreamingServer[IOStreamData, IOStreamData] +type NezhaService_IOStreamServer interface { + 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) { in := new(GeoIP) @@ -273,10 +348,6 @@ var NezhaService_ServiceDesc = grpc.ServiceDesc{ ServiceName: "proto.NezhaService", HandlerType: (*NezhaServiceServer)(nil), Methods: []grpc.MethodDesc{ - { - MethodName: "ReportSystemState", - Handler: _NezhaService_ReportSystemState_Handler, - }, { MethodName: "ReportSystemInfo", Handler: _NezhaService_ReportSystemInfo_Handler, @@ -291,6 +362,12 @@ var NezhaService_ServiceDesc = grpc.ServiceDesc{ }, }, Streams: []grpc.StreamDesc{ + { + StreamName: "ReportSystemState", + Handler: _NezhaService_ReportSystemState_Handler, + ServerStreams: true, + ClientStreams: true, + }, { StreamName: "RequestTask", Handler: _NezhaService_RequestTask_Handler,