diff --git a/README.md b/README.md index 2ce02d1..7a016d2 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它
[![Go Report Card](https://goreportcard.com/badge/github.com/baidu-golang/pbrpc?style=flat-square)](https://goreportcard.com/report/github.com/baidu-golang/pbrpc) -[![Go](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml/badge.svg?branch=v1.3.x)](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml) -[![codecov](https://codecov.io/gh/baidu-golang/pbrpc/branch/v1.3.x/graph/badge.svg?token=EY9Z88E82P)](https://codecov.io/gh/baidu-golang/pbrpc) +[![Go](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml/badge.svg)](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml) +[![codecov](https://codecov.io/gh/baidu-golang/pbrpc/branch/master/graph/badge.svg?token=EY9Z88E82P)](https://codecov.io/gh/baidu-golang/pbrpc) [![Releases](https://img.shields.io/github/release/baidu-golang/pbrpc/all.svg?style=flat-square)](https://github.com/baidu-golang/pbrpc/releases) [![Go Reference](https://golang.com.cn/badge/github.com/baidu-golang/pbrpc.svg)](https://golang.com.cn/github.com/baidu-golang/pbrpc) [![LICENSE](https://img.shields.io/github/license/baidu-golang/pbrpc.svg?style=flat-square)](https://github.com/baidu-golang/pbrpc/blob/master/LICENSE) diff --git a/benchmark_test.go b/benchmark_test.go index 4aea884..657f582 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -25,7 +25,7 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" ) // BenchmarkTestLocalConnectionPerformance bench mark test @@ -60,12 +60,12 @@ func doSimpleRPCInvokeWithSignature(rpcClient *baidurpc.RpcClient, serviceName, rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) name := "马林" - dm := EchoMessage{name} + dm := DataMessage{Name: name} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) - parameterOut := EchoMessage{} + parameterOut := DataMessage{} rpcClient.SendRpcRequest(rpcInvocation, ¶meterOut) } diff --git a/client.go b/client.go index 3f84e41..3a85e97 100644 --- a/client.go +++ b/client.go @@ -23,7 +23,7 @@ import ( "sync/atomic" "time" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" "github.com/jhunters/timewheel" ) diff --git a/client_test.go b/client_test.go index bb04978..ad8bc9e 100644 --- a/client_test.go +++ b/client_test.go @@ -21,8 +21,8 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" . "github.com/smartystreets/goconvey/convey" + "google.golang.org/protobuf/proto" ) const ( @@ -45,9 +45,9 @@ type AddOneTraceService struct { // Trace func (as *AddOneTraceService) Trace(service, name string, traceInfo *baidurpc.TraceInfo) *baidurpc.TraceInfo { - *traceInfo.SpanId++ - *traceInfo.TraceId++ - *traceInfo.ParentSpanId++ + traceInfo.SpanId++ + traceInfo.TraceId++ + traceInfo.ParentSpanId++ return traceInfo } @@ -144,7 +144,7 @@ func TestSingleTcpConnectionClientWithBadChunkCase(t *testing.T) { rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) name := "(马林)(matthew)(XML)(jhunters)" - dm := EchoMessage{name} + dm := DataMessage{Name: name} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) @@ -152,7 +152,7 @@ func TestSingleTcpConnectionClientWithBadChunkCase(t *testing.T) { dataPackage, err := rpcInvocation.GetRequestRpcDataPackage() So(err, ShouldBeNil) - dataPackage.ChunkInfo(10, 1) // bad chunk data package + dataPackage.ChuckInfo(10, 1) // bad chunk data package go func() { client.Session.SendReceive(dataPackage) // send bad chunk data package server will block unitl timeout }() @@ -305,7 +305,7 @@ func doSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.RpcClient, ser rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) name := "(马林)(matthew)(XML)(jhunters)" - dm := EchoMessage{name} + dm := DataMessage{Name: name} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) @@ -323,7 +323,7 @@ func doSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.RpcClient, ser rpcInvocation.AuthenticateData = []byte(AUTH_TOKEN) } - parameterOut := EchoMessage{} + parameterOut := DataMessage{} var response *baidurpc.RpcDataPackage var err error if timeout { @@ -356,9 +356,9 @@ func doSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.RpcClient, ser So(string(response.Attachment), ShouldEqual, "I am a attachementThis is attachment data") } - So(*response.GetTraceId(), ShouldEqual, rpcInvocation.TraceId+1) - So(*response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1) - So(*response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1) + So(response.GetTraceId(), ShouldEqual, rpcInvocation.TraceId+1) + So(response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1) + So(response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1) So(response.GetRpcRequestMetaExt()["key1"], ShouldEqual, "value1") }) diff --git a/data.pb.go b/data.pb.go deleted file mode 100644 index 8cb7394..0000000 --- a/data.pb.go +++ /dev/null @@ -1,261 +0,0 @@ -// Go support for Protocol Buffers RPC which compatiable with https://github.com/Baidu-ecom/Jprotobuf-rpc-socket -// -// Copyright 2002-2007 the original author or authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Code generated by protoc-gen-go. -// source: Request.proto -// DO NOT EDIT! -/* -enum Errno { - // Errno caused by client - ENOSERVICE = 1001; // Service not found - ENOMETHOD = 1002; // Method not found - EREQUEST = 1003; // Bad Request - ERPCAUTH = 1004; // Unauthorized, can't be called EAUTH - // directly which is defined in MACOSX - ETOOMANYFAILS = 1005; // Too many sub calls failed - EPCHANFINISH = 1006; // [Internal] ParallelChannel finished - EBACKUPREQUEST = 1007; // Sending backup request - ERPCTIMEDOUT = 1008; // RPC call is timed out - EFAILEDSOCKET = 1009; // Broken socket - EHTTP = 1010; // Bad http call - EOVERCROWDED = 1011; // The server is overcrowded - ERTMPPUBLISHABLE = 1012; // RtmpRetryingClientStream is publishable - ERTMPCREATESTREAM = 1013; // createStream was rejected by the RTMP server - EEOF = 1014; // Got EOF - EUNUSED = 1015; // The socket was not needed - ESSL = 1016; // SSL related error - EH2RUNOUTSTREAMS = 1017; // The H2 socket was run out of streams - EREJECT = 1018; // The Request is rejected - - // Errno caused by server - EINTERNAL = 2001; // Internal Server Error - ERESPONSE = 2002; // Bad Response - ELOGOFF = 2003; // Server is stopping - ELIMIT = 2004; // Reached server's limit on resources - ECLOSE = 2005; // Close socket initiatively - EITP = 2006; // Failed Itp response -} - - -*/ - -/* -Package baidurpc is a generated protocol buffer package. - -It is generated from these files: - Request.proto - -It has these top-level messages: - Request - Response - ChunkInfo - RpcMeta -*/ -package baidurpc - -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package - -type Request struct { - ServiceName *string `protobuf:"bytes,1,req,name=serviceName" json:"serviceName,omitempty"` - MethodName *string `protobuf:"bytes,2,req,name=methodName" json:"methodName,omitempty"` - LogId *int64 `protobuf:"varint,3,opt,name=logId" json:"logId,omitempty"` - TraceId *int64 `protobuf:"varint,4,opt,name=traceId" json:"traceId,omitempty"` - SpanId *int64 `protobuf:"varint,5,opt,name=spanId" json:"spanId,omitempty"` - ParentSpanId *int64 `protobuf:"varint,6,opt,name=parentSpanId" json:"parentSpanId,omitempty"` - RpcRequestMetaExt []*RpcRequestMetaExtField `protobuf:"bytes,7,rep,name=rpcRequestMetaExt" json:"rpcRequestMetaExt,omitempty"` - ExtraParam []byte `protobuf:"bytes,110,opt,name=extraParam" json:"extraParam,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Request) Reset() { *m = Request{} } -func (m *Request) String() string { return proto.CompactTextString(m) } -func (*Request) ProtoMessage() {} - -func (m *Request) GetServiceName() string { - if m != nil && m.ServiceName != nil { - return *m.ServiceName - } - return "" -} - -func (m *Request) GetMethodName() string { - if m != nil && m.MethodName != nil { - return *m.MethodName - } - return "" -} - -func (m *Request) GetLogId() int64 { - if m != nil && m.LogId != nil { - return *m.LogId - } - return 0 -} - -func (m *Request) GetExtraParam() []byte { - if m != nil { - return m.ExtraParam - } - return nil -} - -type RpcRequestMetaExtField struct { - Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` - Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` -} - -func (m *RpcRequestMetaExtField) Reset() { *m = RpcRequestMetaExtField{} } -func (m *RpcRequestMetaExtField) String() string { return proto.CompactTextString(m) } -func (*RpcRequestMetaExtField) ProtoMessage() {} - -func (m *RpcRequestMetaExtField) GetKey() string { - if m != nil { - return m.Key - } - return "" -} - -func (m *RpcRequestMetaExtField) GetValue() string { - if m != nil { - return m.Value - } - return "" -} - -type Response struct { - ErrorCode *int32 `protobuf:"varint,1,opt,name=errorCode" json:"errorCode,omitempty"` - ErrorText *string `protobuf:"bytes,2,opt,name=errorText" json:"errorText,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Response) Reset() { *m = Response{} } -func (m *Response) String() string { return proto.CompactTextString(m) } -func (*Response) ProtoMessage() {} - -func (m *Response) GetErrorCode() int32 { - if m != nil && m.ErrorCode != nil { - return *m.ErrorCode - } - return 0 -} - -func (m *Response) GetErrorText() string { - if m != nil && m.ErrorText != nil { - return *m.ErrorText - } - return "" -} - -type ChunkInfo struct { - StreamId *int64 `protobuf:"varint,1,req,name=streamId" json:"streamId,omitempty"` - ChunkId *int64 `protobuf:"varint,2,req,name=chunkId" json:"chunkId,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *ChunkInfo) Reset() { *m = ChunkInfo{} } -func (m *ChunkInfo) String() string { return proto.CompactTextString(m) } -func (*ChunkInfo) ProtoMessage() {} - -func (m *ChunkInfo) GetStreamId() int64 { - if m != nil && m.StreamId != nil { - return *m.StreamId - } - return 0 -} - -func (m *ChunkInfo) GetChunkId() int64 { - if m != nil && m.ChunkId != nil { - return *m.ChunkId - } - return 0 -} - -type RpcMeta struct { - Request *Request `protobuf:"bytes,1,opt,name=request" json:"request,omitempty"` - Response *Response `protobuf:"bytes,2,opt,name=response" json:"response,omitempty"` - CompressType *int32 `protobuf:"varint,3,opt,name=compressType" json:"compressType,omitempty"` - CorrelationId *int64 `protobuf:"varint,4,opt,name=correlationId" json:"correlationId,omitempty"` - AttachmentSize *int32 `protobuf:"varint,5,opt,name=attachmentSize" json:"attachmentSize,omitempty"` - ChunkInfo *ChunkInfo `protobuf:"bytes,6,opt,name=chunkInfo" json:"chunkInfo,omitempty"` - AuthenticationData []byte `protobuf:"bytes,7,opt,name=authenticationData" json:"authenticationData,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *RpcMeta) Reset() { *m = RpcMeta{} } -func (m *RpcMeta) String() string { return proto.CompactTextString(m) } -func (*RpcMeta) ProtoMessage() {} - -func (m *RpcMeta) GetRequest() *Request { - if m != nil { - return m.Request - } - return nil -} - -func (m *RpcMeta) GetResponse() *Response { - if m != nil { - return m.Response - } - return nil -} - -func (m *RpcMeta) GetCompressType() int32 { - if m != nil && m.CompressType != nil { - return *m.CompressType - } - return 0 -} - -func (m *RpcMeta) GetCorrelationId() int64 { - if m != nil && m.CorrelationId != nil { - return *m.CorrelationId - } - return 0 -} - -func (m *RpcMeta) GetAttachmentSize() int32 { - if m != nil && m.AttachmentSize != nil { - return *m.AttachmentSize - } - return 0 -} - -func (m *RpcMeta) GetChunkInfo() *ChunkInfo { - if m != nil { - return m.ChunkInfo - } - return nil -} - -func (m *RpcMeta) GetAuthenticationData() []byte { - if m != nil { - return m.AuthenticationData - } - return nil -} diff --git a/datamessage_test.go b/datamessage_test.go new file mode 100644 index 0000000..2e0710c --- /dev/null +++ b/datamessage_test.go @@ -0,0 +1,142 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.9.2 +// source: DataMessage.proto + +package baidurpc_test + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type DataMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *DataMessage) Reset() { + *x = DataMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_DataMessage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DataMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DataMessage) ProtoMessage() {} + +func (x *DataMessage) ProtoReflect() protoreflect.Message { + mi := &file_DataMessage_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DataMessage.ProtoReflect.Descriptor instead. +func (*DataMessage) Descriptor() ([]byte, []int) { + return file_DataMessage_proto_rawDescGZIP(), []int{0} +} + +func (x *DataMessage) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +var File_DataMessage_proto protoreflect.FileDescriptor + +var file_DataMessage_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x44, 0x61, 0x74, 0x61, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x21, 0x0a, 0x0b, 0x44, 0x61, 0x74, 0x61, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x42, 0x1f, 0x5a, 0x1d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x61, 0x69, 0x64, 0x75, 0x2d, 0x67, 0x6f, 0x6c, 0x61, 0x6e, + 0x67, 0x2f, 0x70, 0x62, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_DataMessage_proto_rawDescOnce sync.Once + file_DataMessage_proto_rawDescData = file_DataMessage_proto_rawDesc +) + +func file_DataMessage_proto_rawDescGZIP() []byte { + file_DataMessage_proto_rawDescOnce.Do(func() { + file_DataMessage_proto_rawDescData = protoimpl.X.CompressGZIP(file_DataMessage_proto_rawDescData) + }) + return file_DataMessage_proto_rawDescData +} + +var file_DataMessage_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_DataMessage_proto_goTypes = []interface{}{ + (*DataMessage)(nil), // 0: DataMessage +} +var file_DataMessage_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_DataMessage_proto_init() } +func file_DataMessage_proto_init() { + if File_DataMessage_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_DataMessage_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DataMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_DataMessage_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_DataMessage_proto_goTypes, + DependencyIndexes: file_DataMessage_proto_depIdxs, + MessageInfos: file_DataMessage_proto_msgTypes, + }.Build() + File_DataMessage_proto = out.File + file_DataMessage_proto_rawDesc = nil + file_DataMessage_proto_goTypes = nil + file_DataMessage_proto_depIdxs = nil +} diff --git a/docs/Demo.md b/docs/Demo.md index 59192a4..53e9f21 100644 --- a/docs/Demo.md +++ b/docs/Demo.md @@ -10,23 +10,13 @@ baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它 #### 开发RPC服务端 1. 定义PB对象 - ```go - //手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" - type DataMessage struct { - Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` - } - - func (m *DataMessage) Reset() { *m = DataMessage{} } - func (m *DataMessage) String() string { return proto.CompactTextString(m) } - func (*DataMessage) ProtoMessage() {} - - func (m *DataMessage) GetName() string { - if m.Name != nil { - return *m.Name - } - return "" - } + ```property + message DataMessae { + string name = 1; + } ``` + 用protoc工具 生成 pb go 定义文件 + protoc --go_out=. datamessage.proto 2. 定义一个对象以及方法,用于发布服务 ```go diff --git a/docs/quickstart_client.md b/docs/quickstart_client.md index 38c56ff..fed1af7 100644 --- a/docs/quickstart_client.md +++ b/docs/quickstart_client.md @@ -20,24 +20,13 @@ $ go get github.com/baidu-golang/pbrpc ``` ### 定义protobuf 对象 -以下按pb2进行定义 - ```go - //手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" - type DataMessage struct { - Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` - } - - func (m *DataMessage) Reset() { *m = DataMessage{} } - func (m *DataMessage) String() string { return proto.CompactTextString(m) } - func (*DataMessage) ProtoMessage() {} - - func (m *DataMessage) GetName() string { - if m.Name != nil { - return *m.Name - } - return "" - } + ```property + message DataMessae { + string name = 1; + } ``` + 用protoc工具 生成 pb go 定义文件 + protoc --go_out=. datamessage.proto ### 开发RPC客户端 diff --git a/docs/quickstart_server.md b/docs/quickstart_server.md index 39d7962..6c8c327 100644 --- a/docs/quickstart_server.md +++ b/docs/quickstart_server.md @@ -1,3 +1,8 @@ +@@ -15,24 +20,13 @@ $ go get github.com/baidu-golang/pbrpc ``` ### 定义protobuf 对象 -以下按pb2进行定义 - ```go - //手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" - type DataMessage struct { - Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` - } - - func (m *DataMessage) Reset() { *m = DataMessage{} } - func (m *DataMessage) String() string { return proto.CompactTextString(m) } - func (*DataMessage) ProtoMessage() {} - - func (m *DataMessage) GetName() string { - if m.Name != nil { - return *m.Name - } - return "" - } + ```property + message DataMessae { + string name = 1; + } ``` + 用protoc工具 生成 pb go 定义文件 + protoc --go_out=. datamessage.proto ### 开发RPC服务端 diff --git a/echoservice_test.go b/echoservice_test.go index e0df3df..ce5cbb7 100644 --- a/echoservice_test.go +++ b/echoservice_test.go @@ -31,28 +31,14 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" ) -//手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" -type EchoMessage struct { - Name string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` -} - -func (m *EchoMessage) Reset() { *m = EchoMessage{} } -func (m *EchoMessage) String() string { return proto.CompactTextString(m) } -func (*EchoMessage) ProtoMessage() {} - -func (m *EchoMessage) GetName() string { - return m.Name -} - // EchoService define service type EchoService struct { } // Echo test publish method with return type has context argument -func (rpc *EchoService) Echo(c context.Context, in *EchoMessage) (*EchoMessage, context.Context) { +func (rpc *EchoService) Echo(c context.Context, in *DataMessage) (*DataMessage, context.Context) { var ret = "hello " if len(in.Name) == 0 { @@ -62,7 +48,7 @@ func (rpc *EchoService) Echo(c context.Context, in *EchoMessage) (*EchoMessage, } // return result - dm := EchoMessage{ret} + dm := DataMessage{Name: ret} // time.Sleep(50 * time.Second) @@ -72,7 +58,7 @@ func (rpc *EchoService) Echo(c context.Context, in *EchoMessage) (*EchoMessage, } // Echo test publish method with return type has context argument -func (rpc *EchoService) EchoWithAttchement(c context.Context, in *EchoMessage) (*EchoMessage, context.Context) { +func (rpc *EchoService) EchoWithAttchement(c context.Context, in *DataMessage) (*DataMessage, context.Context) { var ret = "hello " // get attchement @@ -85,7 +71,7 @@ func (rpc *EchoService) EchoWithAttchement(c context.Context, in *EchoMessage) ( } // return result - dm := EchoMessage{ret} + dm := DataMessage{Name: ret} att := "I am a attachement" + string(attachement) @@ -95,7 +81,7 @@ func (rpc *EchoService) EchoWithAttchement(c context.Context, in *EchoMessage) ( } // Echo test publish method with return type has context argument -func (rpc *EchoService) EchoWithCustomizedError(c context.Context, in *EchoMessage) (*EchoMessage, context.Context) { +func (rpc *EchoService) EchoWithCustomizedError(c context.Context, in *DataMessage) (*DataMessage, context.Context) { var ret = "hello " if len(in.Name) == 0 { @@ -105,7 +91,7 @@ func (rpc *EchoService) EchoWithCustomizedError(c context.Context, in *EchoMessa } // return result - dm := EchoMessage{ret} + dm := DataMessage{Name: ret} // bind with err cc := baidurpc.BindError(context.Background(), fmt.Errorf("this is customized error")) @@ -113,7 +99,7 @@ func (rpc *EchoService) EchoWithCustomizedError(c context.Context, in *EchoMessa } // Echo test publish method with return type has context argument -func (rpc *EchoService) EchoWithoutContext(c context.Context, in *EchoMessage) *EchoMessage { +func (rpc *EchoService) EchoWithoutContext(c context.Context, in *DataMessage) *DataMessage { var ret = "hello " if len(in.Name) == 0 { @@ -123,13 +109,13 @@ func (rpc *EchoService) EchoWithoutContext(c context.Context, in *EchoMessage) * } // return result - dm := EchoMessage{ret} + dm := DataMessage{Name: ret} return &dm } // Echo test publish method with return type has context argument -func (rpc *EchoService) EchoSlowTest(c context.Context, in *EchoMessage) *EchoMessage { +func (rpc *EchoService) EchoSlowTest(c context.Context, in *DataMessage) *DataMessage { var ret = "hello " time.Sleep(2 * time.Second) @@ -140,7 +126,7 @@ func (rpc *EchoService) EchoSlowTest(c context.Context, in *EchoMessage) *EchoMe } // return result - dm := EchoMessage{ret} + dm := DataMessage{Name: ret} return &dm } diff --git a/example/client_example_test.go b/example/client_example_test.go index 126efe2..eb9bd02 100644 --- a/example/client_example_test.go +++ b/example/client_example_test.go @@ -25,8 +25,8 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" pool "github.com/jolestar/go-commons-pool/v2" + "google.golang.org/protobuf/proto" ) // ExampleTcpClient @@ -58,7 +58,7 @@ func ExampleTcpClient() { rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) message := "say hello from xiemalin中文测试" - dm := DataMessage{&message} + dm := DataMessage{Name: message} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) @@ -117,7 +117,7 @@ func ExamplePooledTcpClient() { rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) message := "say hello from xiemalin中文测试" - dm := DataMessage{&message} + dm := DataMessage{Name: message} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) diff --git a/example/haclient_example_test.go b/example/haclient_example_test.go index 1870095..dc9d919 100644 --- a/example/haclient_example_test.go +++ b/example/haclient_example_test.go @@ -25,7 +25,7 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" ) // ExampleTcpClient @@ -54,7 +54,7 @@ func ExampleHaTcpClient() { rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) message := "say hello from xiemalin中文测试" - dm := DataMessage{&message} + dm := DataMessage{Name: message} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) diff --git a/example/pb_example_test.go b/example/pb_example_test.go index 16bdbea..15c2a5c 100644 --- a/example/pb_example_test.go +++ b/example/pb_example_test.go @@ -20,20 +20,138 @@ */ package example -import "github.com/golang/protobuf/proto" +import ( + reflect "reflect" + sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) -//手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" type DataMessage struct { - Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` } -func (m *DataMessage) Reset() { *m = DataMessage{} } -func (m *DataMessage) String() string { return proto.CompactTextString(m) } -func (*DataMessage) ProtoMessage() {} +func (x *DataMessage) Reset() { + *x = DataMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_DataMessage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DataMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DataMessage) ProtoMessage() {} + +func (x *DataMessage) ProtoReflect() protoreflect.Message { + mi := &file_DataMessage_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DataMessage.ProtoReflect.Descriptor instead. +func (*DataMessage) Descriptor() ([]byte, []int) { + return file_DataMessage_proto_rawDescGZIP(), []int{0} +} -func (m *DataMessage) GetName() string { - if m.Name != nil { - return *m.Name +func (x *DataMessage) GetName() string { + if x != nil { + return x.Name } return "" } + +var File_DataMessage_proto protoreflect.FileDescriptor + +var file_DataMessage_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x44, 0x61, 0x74, 0x61, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x21, 0x0a, 0x0b, 0x44, 0x61, 0x74, 0x61, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x42, 0x1f, 0x5a, 0x1d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x61, 0x69, 0x64, 0x75, 0x2d, 0x67, 0x6f, 0x6c, 0x61, 0x6e, + 0x67, 0x2f, 0x70, 0x62, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_DataMessage_proto_rawDescOnce sync.Once + file_DataMessage_proto_rawDescData = file_DataMessage_proto_rawDesc +) + +func file_DataMessage_proto_rawDescGZIP() []byte { + file_DataMessage_proto_rawDescOnce.Do(func() { + file_DataMessage_proto_rawDescData = protoimpl.X.CompressGZIP(file_DataMessage_proto_rawDescData) + }) + return file_DataMessage_proto_rawDescData +} + +var file_DataMessage_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_DataMessage_proto_goTypes = []interface{}{ + (*DataMessage)(nil), // 0: DataMessage +} +var file_DataMessage_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_DataMessage_proto_init() } +func file_DataMessage_proto_init() { + if File_DataMessage_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_DataMessage_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DataMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_DataMessage_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_DataMessage_proto_goTypes, + DependencyIndexes: file_DataMessage_proto_depIdxs, + MessageInfos: file_DataMessage_proto_msgTypes, + }.Build() + File_DataMessage_proto = out.File + file_DataMessage_proto_rawDesc = nil + file_DataMessage_proto_goTypes = nil + file_DataMessage_proto_depIdxs = nil +} diff --git a/example/server_example_test.go b/example/server_example_test.go index 04f1c0b..a6791e8 100644 --- a/example/server_example_test.go +++ b/example/server_example_test.go @@ -34,10 +34,9 @@ import ( "net/rpc" "reflect" "strings" - "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" ) // ExampleRpcServer @@ -120,19 +119,19 @@ func ExampleRpcServerRegisterWithCallback() { return nil, nil, errors.New(errStr) } - var name *string = nil + var name string m := msg.(*DataMessage) name = m.Name - if len(*name) == 0 { + if len(name) == 0 { ret = ret + "veryone" } else { - ret = ret + *name + ret = ret + name } } dm := DataMessage{} - dm.Name = proto.String(ret) + dm.Name = ret return &dm, []byte{1, 5, 9}, nil } @@ -150,19 +149,16 @@ type EchoService struct { func (rpc *EchoService) Echo(c context.Context, in *DataMessage) (*DataMessage, context.Context) { var ret = "hello " - // slow here - time.Sleep(1000 * time.Millisecond) - attachement := baidurpc.Attachement(c) fmt.Println("attachement", attachement) - if len(*in.Name) == 0 { + if len(in.Name) == 0 { ret = ret + "veryone" } else { - ret = ret + *in.Name + ret = ret + in.Name } dm := DataMessage{} - dm.Name = proto.String(ret) + dm.Name = ret // bind attachment cc := baidurpc.BindAttachement(context.Background(), []byte("hello")) diff --git a/go.mod b/go.mod index a204538..9f3068f 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,13 @@ go 1.15 require ( github.com/funny/link v0.0.0-20190805113223-98708916287b github.com/funny/utest v0.0.0-20161029064919-43870a374500 // indirect - github.com/golang/protobuf v1.5.2 github.com/golang/snappy v0.0.3 github.com/jhunters/timewheel v0.0.0-20210716021958-85ae0cfe1074 github.com/jolestar/go-commons-pool/v2 v2.1.1 + github.com/kr/pretty v0.2.0 // indirect github.com/smartystreets/goconvey v1.6.4 + github.com/stretchr/testify v1.6.1 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + google.golang.org/protobuf v1.26.0 + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index 2def630..a099bbd 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/funny/link v0.0.0-20190805113223-98708916287b/go.mod h1:ajJ48jAHusPjn github.com/funny/utest v0.0.0-20161029064919-43870a374500 h1:Z0r1CZnoIWFB/Uiwh1BU5FYmuFe6L5NPi6XWQEmsTRg= github.com/funny/utest v0.0.0-20161029064919-43870a374500/go.mod h1:mUn39tBov9jKnTWV1RlOYoNzxdBFHiSzXWdY1FoNGGg= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= @@ -22,6 +20,11 @@ github.com/jolestar/go-commons-pool/v2 v2.1.1 h1:KrbCEvx5KhwcHzLTWIE8SJJQL7zzNto github.com/jolestar/go-commons-pool/v2 v2.1.1/go.mod h1:kTOzcguO2zUoEd+BySdg7Xhk/YE0HEr2bAHdWDkhMXg= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/ouqiang/timewheel v1.0.1/go.mod h1:896mz+8zvRU6i0PLVR0qaNuU5roxC874OB4TxUvUewY= github.com/peterh/liner v1.2.1/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= @@ -33,20 +36,24 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/haclient.go b/haclient.go index 4104bd9..271e806 100644 --- a/haclient.go +++ b/haclient.go @@ -27,7 +27,7 @@ import ( "sync" "time" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" "github.com/jhunters/timewheel" ) diff --git a/haclient_test.go b/haclient_test.go index 7140e42..89c0903 100644 --- a/haclient_test.go +++ b/haclient_test.go @@ -25,8 +25,8 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" . "github.com/smartystreets/goconvey/convey" + "google.golang.org/protobuf/proto" ) // TestHaClient test ha client @@ -65,7 +65,7 @@ func doHaSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.HaRpcClient, rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) name := "马林" - dm := EchoMessage{name} + dm := DataMessage{Name: name} rpcInvocation.SetParameterIn(&dm) rpcInvocation.LogId = proto.Int64(1) @@ -74,7 +74,7 @@ func doHaSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.HaRpcClient, rpcInvocation.Attachment = []byte("This is attachment data") } - parameterOut := EchoMessage{} + parameterOut := DataMessage{} var response *baidurpc.RpcDataPackage var err error if async { diff --git a/httpserver.go b/httpserver.go index cc3733f..9b0daaf 100644 --- a/httpserver.go +++ b/httpserver.go @@ -93,7 +93,7 @@ func (h *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { traceId := getHeaderAsInt64(req, Trace_Id_key) spanId := getHeaderAsInt64(req, Trace_Span_key) parentId := getHeaderAsInt64(req, Trace_Parent_key) - traceInfo := &TraceInfo{TraceId: &traceId, SpanId: &spanId, ParentSpanId: &parentId} + traceInfo := &TraceInfo{TraceId: traceId, SpanId: spanId, ParentSpanId: parentId} value := getHeaderAsByte(req, Request_Meta_Key) if value != nil { @@ -107,15 +107,9 @@ func (h *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { traceRetrun := h.s.traceService.Trace(serviceName, method, traceInfo) if traceRetrun != nil { - if traceRetrun.TraceId != nil { - w.Header().Set(Trace_Id_key, int64ToString(traceRetrun.TraceId)) - } - if traceRetrun.SpanId != nil { - w.Header().Set(Trace_Span_key, int64ToString(traceRetrun.SpanId)) - } - if traceRetrun.ParentSpanId != nil { - w.Header().Set(Trace_Parent_key, int64ToString(traceRetrun.ParentSpanId)) - } + w.Header().Set(Trace_Id_key, int64ToString(traceRetrun.TraceId)) + w.Header().Set(Trace_Span_key, int64ToString(traceRetrun.SpanId)) + w.Header().Set(Trace_Parent_key, int64ToString(traceRetrun.ParentSpanId)) if traceRetrun.RpcRequestMetaExt != nil { metaData, err := json.Marshal(traceRetrun.RpcRequestMetaExt) if err == nil { @@ -182,11 +176,8 @@ func getHeaderAsInt64(req *http.Request, key string) int64 { return int64(id) } -func int64ToString(i *int64) string { - if i == nil { - return "" - } - return strconv.Itoa(int(*i)) +func int64ToString(i int64) string { + return strconv.Itoa(int(i)) } func errResponse(errno int, message string) *ResponseData { diff --git a/pb_data.go b/pb_data.go new file mode 100644 index 0000000..468be50 --- /dev/null +++ b/pb_data.go @@ -0,0 +1,562 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.9.2 +// source: brpc_meta.proto + +package baidurpc + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RpcMeta struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Request *Request `protobuf:"bytes,1,opt,name=request,proto3" json:"request,omitempty"` + Response *Response `protobuf:"bytes,2,opt,name=response,proto3" json:"response,omitempty"` + CompressType int32 `protobuf:"varint,3,opt,name=compress_type,json=compressType,proto3" json:"compress_type,omitempty"` // 0:nocompress 1:Snappy 2:gzip + CorrelationId int64 `protobuf:"varint,4,opt,name=correlation_id,json=correlationId,proto3" json:"correlation_id,omitempty"` + AttachmentSize int32 `protobuf:"varint,5,opt,name=attachment_size,json=attachmentSize,proto3" json:"attachment_size,omitempty"` + ChuckInfo *ChunkInfo `protobuf:"bytes,6,opt,name=chuck_info,json=chuckInfo,proto3" json:"chuck_info,omitempty"` + AuthenticationData []byte `protobuf:"bytes,7,opt,name=authentication_data,json=authenticationData,proto3" json:"authentication_data,omitempty"` +} + +func (x *RpcMeta) Reset() { + *x = RpcMeta{} + if protoimpl.UnsafeEnabled { + mi := &file_brpc_meta_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RpcMeta) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RpcMeta) ProtoMessage() {} + +func (x *RpcMeta) ProtoReflect() protoreflect.Message { + mi := &file_brpc_meta_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RpcMeta.ProtoReflect.Descriptor instead. +func (*RpcMeta) Descriptor() ([]byte, []int) { + return file_brpc_meta_proto_rawDescGZIP(), []int{0} +} + +func (x *RpcMeta) GetRequest() *Request { + if x != nil { + return x.Request + } + return nil +} + +func (x *RpcMeta) GetResponse() *Response { + if x != nil { + return x.Response + } + return nil +} + +func (x *RpcMeta) GetCompressType() int32 { + if x != nil { + return x.CompressType + } + return 0 +} + +func (x *RpcMeta) GetCorrelationId() int64 { + if x != nil { + return x.CorrelationId + } + return 0 +} + +func (x *RpcMeta) GetAttachmentSize() int32 { + if x != nil { + return x.AttachmentSize + } + return 0 +} + +func (x *RpcMeta) GetChuckInfo() *ChunkInfo { + if x != nil { + return x.ChuckInfo + } + return nil +} + +func (x *RpcMeta) GetAuthenticationData() []byte { + if x != nil { + return x.AuthenticationData + } + return nil +} + +type Request struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ServiceName string `protobuf:"bytes,1,opt,name=service_name,json=serviceName,proto3" json:"service_name,omitempty"` + MethodName string `protobuf:"bytes,2,opt,name=method_name,json=methodName,proto3" json:"method_name,omitempty"` + LogId int64 `protobuf:"varint,3,opt,name=log_id,json=logId,proto3" json:"log_id,omitempty"` + TraceId int64 `protobuf:"varint,4,opt,name=traceId,proto3" json:"traceId,omitempty"` + SpanId int64 `protobuf:"varint,5,opt,name=spanId,proto3" json:"spanId,omitempty"` + ParentSpanId int64 `protobuf:"varint,6,opt,name=parentSpanId,proto3" json:"parentSpanId,omitempty"` + RpcRequestMetaExt []*RpcRequestMetaExtField `protobuf:"bytes,7,rep,name=rpcRequestMetaExt,proto3" json:"rpcRequestMetaExt,omitempty"` + ExtraParam []byte `protobuf:"bytes,110,opt,name=extraParam,proto3" json:"extraParam,omitempty"` +} + +func (x *Request) Reset() { + *x = Request{} + if protoimpl.UnsafeEnabled { + mi := &file_brpc_meta_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_brpc_meta_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_brpc_meta_proto_rawDescGZIP(), []int{1} +} + +func (x *Request) GetServiceName() string { + if x != nil { + return x.ServiceName + } + return "" +} + +func (x *Request) GetMethodName() string { + if x != nil { + return x.MethodName + } + return "" +} + +func (x *Request) GetLogId() int64 { + if x != nil { + return x.LogId + } + return 0 +} + +func (x *Request) GetTraceId() int64 { + if x != nil { + return x.TraceId + } + return 0 +} + +func (x *Request) GetSpanId() int64 { + if x != nil { + return x.SpanId + } + return 0 +} + +func (x *Request) GetParentSpanId() int64 { + if x != nil { + return x.ParentSpanId + } + return 0 +} + +func (x *Request) GetRpcRequestMetaExt() []*RpcRequestMetaExtField { + if x != nil { + return x.RpcRequestMetaExt + } + return nil +} + +func (x *Request) GetExtraParam() []byte { + if x != nil { + return x.ExtraParam + } + return nil +} + +type Response struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ErrorCode int32 `protobuf:"varint,1,opt,name=error_code,json=errorCode,proto3" json:"error_code,omitempty"` + ErrorText string `protobuf:"bytes,2,opt,name=error_text,json=errorText,proto3" json:"error_text,omitempty"` +} + +func (x *Response) Reset() { + *x = Response{} + if protoimpl.UnsafeEnabled { + mi := &file_brpc_meta_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_brpc_meta_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_brpc_meta_proto_rawDescGZIP(), []int{2} +} + +func (x *Response) GetErrorCode() int32 { + if x != nil { + return x.ErrorCode + } + return 0 +} + +func (x *Response) GetErrorText() string { + if x != nil { + return x.ErrorText + } + return "" +} + +type ChunkInfo struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StreamId int64 `protobuf:"varint,1,opt,name=stream_id,json=streamId,proto3" json:"stream_id,omitempty"` + ChunkId int64 `protobuf:"varint,2,opt,name=chunk_id,json=chunkId,proto3" json:"chunk_id,omitempty"` +} + +func (x *ChunkInfo) Reset() { + *x = ChunkInfo{} + if protoimpl.UnsafeEnabled { + mi := &file_brpc_meta_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ChunkInfo) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ChunkInfo) ProtoMessage() {} + +func (x *ChunkInfo) ProtoReflect() protoreflect.Message { + mi := &file_brpc_meta_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ChunkInfo.ProtoReflect.Descriptor instead. +func (*ChunkInfo) Descriptor() ([]byte, []int) { + return file_brpc_meta_proto_rawDescGZIP(), []int{3} +} + +func (x *ChunkInfo) GetStreamId() int64 { + if x != nil { + return x.StreamId + } + return 0 +} + +func (x *ChunkInfo) GetChunkId() int64 { + if x != nil { + return x.ChunkId + } + return 0 +} + +type RpcRequestMetaExtField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *RpcRequestMetaExtField) Reset() { + *x = RpcRequestMetaExtField{} + if protoimpl.UnsafeEnabled { + mi := &file_brpc_meta_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RpcRequestMetaExtField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RpcRequestMetaExtField) ProtoMessage() {} + +func (x *RpcRequestMetaExtField) ProtoReflect() protoreflect.Message { + mi := &file_brpc_meta_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RpcRequestMetaExtField.ProtoReflect.Descriptor instead. +func (*RpcRequestMetaExtField) Descriptor() ([]byte, []int) { + return file_brpc_meta_proto_rawDescGZIP(), []int{4} +} + +func (x *RpcRequestMetaExtField) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +func (x *RpcRequestMetaExtField) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +var File_brpc_meta_proto protoreflect.FileDescriptor + +var file_brpc_meta_proto_rawDesc = []byte{ + 0x0a, 0x0f, 0x62, 0x72, 0x70, 0x63, 0x5f, 0x6d, 0x65, 0x74, 0x61, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x22, 0xa5, 0x02, 0x0a, 0x07, 0x52, 0x70, 0x63, 0x4d, 0x65, 0x74, 0x61, 0x12, 0x22, 0x0a, + 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, + 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x52, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x25, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x08, + 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x6f, 0x6d, 0x70, + 0x72, 0x65, 0x73, 0x73, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x0c, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x25, 0x0a, + 0x0e, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x6c, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x27, 0x0a, 0x0f, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, + 0x6e, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0e, 0x61, + 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x29, 0x0a, + 0x0a, 0x63, 0x68, 0x75, 0x63, 0x6b, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x06, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x0a, 0x2e, 0x43, 0x68, 0x75, 0x6e, 0x6b, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x09, 0x63, + 0x68, 0x75, 0x63, 0x6b, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x2f, 0x0a, 0x13, 0x61, 0x75, 0x74, 0x68, + 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x18, + 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x12, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x44, 0x61, 0x74, 0x61, 0x22, 0xa1, 0x02, 0x0a, 0x07, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x6d, 0x65, 0x74, 0x68, + 0x6f, 0x64, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x6d, + 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x6c, 0x6f, 0x67, + 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x6c, 0x6f, 0x67, 0x49, 0x64, + 0x12, 0x18, 0x0a, 0x07, 0x74, 0x72, 0x61, 0x63, 0x65, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x07, 0x74, 0x72, 0x61, 0x63, 0x65, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x70, + 0x61, 0x6e, 0x49, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x73, 0x70, 0x61, 0x6e, + 0x49, 0x64, 0x12, 0x22, 0x0a, 0x0c, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x53, 0x70, 0x61, 0x6e, + 0x49, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, + 0x53, 0x70, 0x61, 0x6e, 0x49, 0x64, 0x12, 0x45, 0x0a, 0x11, 0x72, 0x70, 0x63, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x4d, 0x65, 0x74, 0x61, 0x45, 0x78, 0x74, 0x18, 0x07, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x17, 0x2e, 0x52, 0x70, 0x63, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x4d, 0x65, + 0x74, 0x61, 0x45, 0x78, 0x74, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x52, 0x11, 0x72, 0x70, 0x63, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x4d, 0x65, 0x74, 0x61, 0x45, 0x78, 0x74, 0x12, 0x1e, 0x0a, + 0x0a, 0x65, 0x78, 0x74, 0x72, 0x61, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x18, 0x6e, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x0a, 0x65, 0x78, 0x74, 0x72, 0x61, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x22, 0x48, 0x0a, + 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x72, 0x72, + 0x6f, 0x72, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x09, 0x65, + 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x65, 0x72, + 0x72, 0x6f, 0x72, 0x54, 0x65, 0x78, 0x74, 0x22, 0x43, 0x0a, 0x09, 0x43, 0x68, 0x75, 0x6e, 0x6b, + 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x1b, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x5f, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x49, + 0x64, 0x12, 0x19, 0x0a, 0x08, 0x63, 0x68, 0x75, 0x6e, 0x6b, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x03, 0x52, 0x07, 0x63, 0x68, 0x75, 0x6e, 0x6b, 0x49, 0x64, 0x22, 0x40, 0x0a, 0x16, + 0x52, 0x70, 0x63, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x4d, 0x65, 0x74, 0x61, 0x45, 0x78, + 0x74, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x22, + 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x61, 0x69, + 0x64, 0x75, 0x2d, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x62, 0x61, 0x69, 0x64, 0x75, 0x72, + 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_brpc_meta_proto_rawDescOnce sync.Once + file_brpc_meta_proto_rawDescData = file_brpc_meta_proto_rawDesc +) + +func file_brpc_meta_proto_rawDescGZIP() []byte { + file_brpc_meta_proto_rawDescOnce.Do(func() { + file_brpc_meta_proto_rawDescData = protoimpl.X.CompressGZIP(file_brpc_meta_proto_rawDescData) + }) + return file_brpc_meta_proto_rawDescData +} + +var file_brpc_meta_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_brpc_meta_proto_goTypes = []interface{}{ + (*RpcMeta)(nil), // 0: RpcMeta + (*Request)(nil), // 1: Request + (*Response)(nil), // 2: Response + (*ChunkInfo)(nil), // 3: ChunkInfo + (*RpcRequestMetaExtField)(nil), // 4: RpcRequestMetaExtField +} +var file_brpc_meta_proto_depIdxs = []int32{ + 1, // 0: RpcMeta.request:type_name -> Request + 2, // 1: RpcMeta.response:type_name -> Response + 3, // 2: RpcMeta.chuck_info:type_name -> ChunkInfo + 4, // 3: Request.rpcRequestMetaExt:type_name -> RpcRequestMetaExtField + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name +} + +func init() { file_brpc_meta_proto_init() } +func file_brpc_meta_proto_init() { + if File_brpc_meta_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_brpc_meta_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RpcMeta); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_brpc_meta_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Request); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_brpc_meta_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Response); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_brpc_meta_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ChunkInfo); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_brpc_meta_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RpcRequestMetaExtField); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_brpc_meta_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_brpc_meta_proto_goTypes, + DependencyIndexes: file_brpc_meta_proto_depIdxs, + MessageInfos: file_brpc_meta_proto_msgTypes, + }.Build() + File_brpc_meta_proto = out.File + file_brpc_meta_proto_rawDesc = nil + file_brpc_meta_proto_goTypes = nil + file_brpc_meta_proto_depIdxs = nil +} diff --git a/pb_status.go b/pb_status.go new file mode 100644 index 0000000..d7d5cae --- /dev/null +++ b/pb_status.go @@ -0,0 +1,334 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.9.2 +// source: go.proto + +package baidurpc + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RPCStatus struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Host string `protobuf:"bytes,1,opt,name=host,proto3" json:"host,omitempty"` + Port int32 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"` + TimeoutSenconds int32 `protobuf:"varint,3,opt,name=timeout,proto3" json:"timeout,omitempty"` + Methods []*RPCMethod `protobuf:"bytes,4,rep,name=methods,proto3" json:"methods,omitempty"` +} + +func (x *RPCStatus) Reset() { + *x = RPCStatus{} + if protoimpl.UnsafeEnabled { + mi := &file_go_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RPCStatus) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RPCStatus) ProtoMessage() {} + +func (x *RPCStatus) ProtoReflect() protoreflect.Message { + mi := &file_go_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RPCStatus.ProtoReflect.Descriptor instead. +func (*RPCStatus) Descriptor() ([]byte, []int) { + return file_go_proto_rawDescGZIP(), []int{0} +} + +func (x *RPCStatus) GetHost() string { + if x != nil { + return x.Host + } + return "" +} + +func (x *RPCStatus) GetPort() int32 { + if x != nil { + return x.Port + } + return 0 +} + +func (x *RPCStatus) GetTimeout() int32 { + if x != nil { + return x.TimeoutSenconds + } + return 0 +} + +func (x *RPCStatus) GetMethods() []*RPCMethod { + if x != nil { + return x.Methods + } + return nil +} + +type RPCMethod struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Service string `protobuf:"bytes,1,opt,name=service,proto3" json:"service,omitempty"` + Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"` + InTypeMeta string `protobuf:"bytes,3,opt,name=InTypeMeta,proto3" json:"InTypeMeta,omitempty"` + ReturnTypeMeta string `protobuf:"bytes,4,opt,name=ReturnTypeMeta,proto3" json:"ReturnTypeMeta,omitempty"` +} + +func (x *RPCMethod) Reset() { + *x = RPCMethod{} + if protoimpl.UnsafeEnabled { + mi := &file_go_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RPCMethod) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RPCMethod) ProtoMessage() {} + +func (x *RPCMethod) ProtoReflect() protoreflect.Message { + mi := &file_go_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RPCMethod.ProtoReflect.Descriptor instead. +func (*RPCMethod) Descriptor() ([]byte, []int) { + return file_go_proto_rawDescGZIP(), []int{1} +} + +func (x *RPCMethod) GetService() string { + if x != nil { + return x.Service + } + return "" +} + +func (x *RPCMethod) GetMethod() string { + if x != nil { + return x.Method + } + return "" +} + +func (x *RPCMethod) GetInTypeMeta() string { + if x != nil { + return x.InTypeMeta + } + return "" +} + +func (x *RPCMethod) GetReturnTypeMeta() string { + if x != nil { + return x.ReturnTypeMeta + } + return "" +} + +type QpsData struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Qpsinfo map[int64]int32 `protobuf:"bytes,1,rep,name=qpsinfo,proto3" json:"qpsinfo,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` +} + +func (x *QpsData) Reset() { + *x = QpsData{} + if protoimpl.UnsafeEnabled { + mi := &file_go_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *QpsData) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*QpsData) ProtoMessage() {} + +func (x *QpsData) ProtoReflect() protoreflect.Message { + mi := &file_go_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use QpsData.ProtoReflect.Descriptor instead. +func (*QpsData) Descriptor() ([]byte, []int) { + return file_go_proto_rawDescGZIP(), []int{2} +} + +func (x *QpsData) GetQpsinfo() map[int64]int32 { + if x != nil { + return x.Qpsinfo + } + return nil +} + +var File_go_proto protoreflect.FileDescriptor + +var file_go_proto_rawDesc = []byte{ + 0x0a, 0x08, 0x67, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x73, 0x0a, 0x09, 0x52, 0x50, + 0x43, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, + 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x12, + 0x18, 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, + 0x52, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x24, 0x0a, 0x07, 0x6d, 0x65, 0x74, + 0x68, 0x6f, 0x64, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x52, 0x50, 0x43, + 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x52, 0x07, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x73, 0x22, + 0x85, 0x01, 0x0a, 0x09, 0x52, 0x50, 0x43, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x18, 0x0a, + 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, + 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, + 0x1e, 0x0a, 0x0a, 0x49, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x0a, 0x49, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x12, + 0x26, 0x0a, 0x0e, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x4d, 0x65, 0x74, + 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x54, + 0x79, 0x70, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x22, 0x76, 0x0a, 0x07, 0x51, 0x70, 0x73, 0x44, 0x61, + 0x74, 0x61, 0x12, 0x2f, 0x0a, 0x07, 0x71, 0x70, 0x73, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x51, 0x70, 0x73, 0x44, 0x61, 0x74, 0x61, 0x2e, 0x51, 0x70, + 0x73, 0x69, 0x6e, 0x66, 0x6f, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x07, 0x71, 0x70, 0x73, 0x69, + 0x6e, 0x66, 0x6f, 0x1a, 0x3a, 0x0a, 0x0c, 0x51, 0x70, 0x73, 0x69, 0x6e, 0x66, 0x6f, 0x45, 0x6e, + 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, + 0x22, 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x61, + 0x69, 0x64, 0x75, 0x2d, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x62, 0x61, 0x69, 0x64, 0x75, + 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_go_proto_rawDescOnce sync.Once + file_go_proto_rawDescData = file_go_proto_rawDesc +) + +func file_go_proto_rawDescGZIP() []byte { + file_go_proto_rawDescOnce.Do(func() { + file_go_proto_rawDescData = protoimpl.X.CompressGZIP(file_go_proto_rawDescData) + }) + return file_go_proto_rawDescData +} + +var file_go_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_go_proto_goTypes = []interface{}{ + (*RPCStatus)(nil), // 0: RPCStatus + (*RPCMethod)(nil), // 1: RPCMethod + (*QpsData)(nil), // 2: QpsData + nil, // 3: QpsData.QpsinfoEntry +} +var file_go_proto_depIdxs = []int32{ + 1, // 0: RPCStatus.methods:type_name -> RPCMethod + 3, // 1: QpsData.qpsinfo:type_name -> QpsData.QpsinfoEntry + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_go_proto_init() } +func file_go_proto_init() { + if File_go_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_go_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RPCStatus); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_go_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RPCMethod); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_go_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*QpsData); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_go_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_go_proto_goTypes, + DependencyIndexes: file_go_proto_depIdxs, + MessageInfos: file_go_proto_msgTypes, + }.Build() + File_go_proto = out.File + file_go_proto_rawDesc = nil + file_go_proto_goTypes = nil + file_go_proto_depIdxs = nil +} diff --git a/request.pb_test.go b/request.pb_test.go index 6c6834c..3917e85 100644 --- a/request.pb_test.go +++ b/request.pb_test.go @@ -24,7 +24,7 @@ import ( "testing" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" . "github.com/smartystreets/goconvey/convey" ) @@ -37,9 +37,9 @@ func TestPropertySetAndGet(t *testing.T) { var logId int64 = 1 request := baidurpc.Request{ - ServiceName: &serviceName, - MethodName: &methodName, - LogId: &logId, + ServiceName: serviceName, + MethodName: methodName, + LogId: logId, } So(serviceName, ShouldEqual, request.GetServiceName()) diff --git a/rpcpackage.go b/rpcpackage.go index 6ded90c..c9f76b3 100644 --- a/rpcpackage.go +++ b/rpcpackage.go @@ -25,8 +25,8 @@ import ( "math/rand" "strings" - "github.com/golang/protobuf/proto" "github.com/golang/snappy" + "google.golang.org/protobuf/proto" ) // error log info definition @@ -59,7 +59,7 @@ message RpcMeta { optional int32 compress_type = 3; // 0:nocompress 1:Snappy 2:gzip optional int64 correlation_id = 4; optional int32 attachment_size = 5; - optional ChunkInfo chuck_info = 6; + optional ChuckInfo chuck_info = 6; optional bytes authentication_data = 7; }; @@ -82,7 +82,7 @@ message Response { optional string error_text = 2; }; -messsage ChunkInfo { +messsage ChuckInfo { required int64 stream_id = 1; required int64 chunk_id = 2; }; @@ -139,10 +139,10 @@ func initRpcMeta(r *RpcDataPackage) { } } -func initChunkinfo(r *RpcDataPackage) { +func initChuckInfo(r *RpcDataPackage) { initRpcMeta(r) - if r.Meta.ChunkInfo == nil { - r.Meta.ChunkInfo = &ChunkInfo{} + if r.Meta.ChuckInfo == nil { + r.Meta.ChuckInfo = &ChunkInfo{} } } @@ -169,7 +169,7 @@ func initResponse(r *RpcDataPackage) { func (r *RpcDataPackage) ServiceName(serviceName string) *RpcDataPackage { initRequest(r) - r.Meta.Request.ServiceName = &serviceName + r.Meta.Request.ServiceName = serviceName return r } @@ -177,7 +177,7 @@ func (r *RpcDataPackage) ServiceName(serviceName string) *RpcDataPackage { func (r *RpcDataPackage) MethodName(methodName string) *RpcDataPackage { initRequest(r) - r.Meta.Request.MethodName = &methodName + r.Meta.Request.MethodName = methodName return r } @@ -202,21 +202,21 @@ func (r *RpcDataPackage) AuthenticationData(authenticationData []byte) *RpcDataP func (r *RpcDataPackage) CorrelationId(correlationId int64) *RpcDataPackage { initRpcMeta(r) - r.Meta.CorrelationId = &correlationId + r.Meta.CorrelationId = correlationId return r } func (r *RpcDataPackage) CompressType(compressType int32) *RpcDataPackage { initRpcMeta(r) - r.Meta.CompressType = &compressType + r.Meta.CompressType = compressType return r } func (r *RpcDataPackage) LogId(logId int64) *RpcDataPackage { initRequest(r) - r.Meta.Request.LogId = &logId + r.Meta.Request.LogId = logId return r } @@ -228,33 +228,33 @@ func (r *RpcDataPackage) GetLogId() int64 { func (r *RpcDataPackage) TraceId(traceId int64) *RpcDataPackage { initRequest(r) - r.Meta.Request.TraceId = &traceId + r.Meta.Request.TraceId = traceId return r } -func (r *RpcDataPackage) GetTraceId() *int64 { +func (r *RpcDataPackage) GetTraceId() int64 { initRequest(r) return r.Meta.Request.TraceId } func (r *RpcDataPackage) SpanId(spanId int64) *RpcDataPackage { initRequest(r) - r.Meta.Request.SpanId = &spanId + r.Meta.Request.SpanId = spanId return r } -func (r *RpcDataPackage) GetSpanId() *int64 { +func (r *RpcDataPackage) GetSpanId() int64 { initRequest(r) return r.Meta.Request.SpanId } func (r *RpcDataPackage) ParentSpanId(parentSpanId int64) *RpcDataPackage { initRequest(r) - r.Meta.Request.ParentSpanId = &parentSpanId + r.Meta.Request.ParentSpanId = parentSpanId return r } -func (r *RpcDataPackage) GetParentSpanId() *int64 { +func (r *RpcDataPackage) GetParentSpanId() int64 { initRequest(r) return r.Meta.Request.ParentSpanId } @@ -283,7 +283,7 @@ func (r *RpcDataPackage) GetRpcRequestMetaExt() map[string]string { func (r *RpcDataPackage) ErrorCode(errorCode int32) *RpcDataPackage { initResponse(r) - r.Meta.Response.ErrorCode = &errorCode + r.Meta.Response.ErrorCode = errorCode return r } @@ -291,7 +291,7 @@ func (r *RpcDataPackage) ErrorCode(errorCode int32) *RpcDataPackage { func (r *RpcDataPackage) ErrorText(errorText string) *RpcDataPackage { initResponse(r) - r.Meta.Response.ErrorText = &errorText + r.Meta.Response.ErrorText = errorText return r } @@ -304,12 +304,12 @@ func (r *RpcDataPackage) ExtraParams(extraParams []byte) *RpcDataPackage { return r } -func (r *RpcDataPackage) ChunkInfo(streamId int64, chunkId int64) *RpcDataPackage { - chunkInfo := ChunkInfo{} - chunkInfo.StreamId = &streamId - chunkInfo.ChunkId = &chunkId +func (r *RpcDataPackage) ChuckInfo(streamId int64, chunkId int64) *RpcDataPackage { + ChuckInfo := ChunkInfo{} + ChuckInfo.StreamId = streamId + ChuckInfo.ChunkId = chunkId initRpcMeta(r) - r.Meta.ChunkInfo = &chunkInfo + r.Meta.ChuckInfo = &ChuckInfo return r } @@ -390,7 +390,7 @@ func (r *RpcDataPackage) Write() ([]byte, error) { totalSize = totalSize + attachmentSize } - r.Meta.AttachmentSize = proto.Int32(int32(attachmentSize)) + r.Meta.AttachmentSize = int32(attachmentSize) metaBytes, err := proto.Marshal(r.Meta) if err != nil { @@ -538,24 +538,24 @@ func (r *RpcDataPackage) Chunk(chunkSize int) []*RpcDataPackage { tempMeta := *base.Meta base.Meta = &tempMeta - initChunkinfo(base) + initChuckInfo(base) offset := startPos + chunkSize if offset > dataSize { offset = dataSize } base.Data = base.Data[startPos:offset] startPos += chunkSize - tempChunkInfo := &ChunkInfo{StreamId: &chunkStreamID, ChunkId: proto.Int64(int64(i + 1))} + tempChuckInfo := &ChunkInfo{StreamId: chunkStreamID, ChunkId: int64(i + 1)} if i == chunkCount-1 { // this is last package - tempChunkInfo.ChunkId = proto.Int64(int64(-1)) + tempChuckInfo.ChunkId = int64(-1) } if i > 0 { // fix duplicate attachment data base.Attachment = nil } - chunkInfo := *tempChunkInfo - base.Meta.ChunkInfo = &chunkInfo + ChuckInfo := *tempChuckInfo + base.Meta.ChuckInfo = &ChuckInfo ret[i] = base } @@ -565,13 +565,13 @@ func (r *RpcDataPackage) Chunk(chunkSize int) []*RpcDataPackage { // GetChunkStreamId func (r *RpcDataPackage) GetChunkStreamId() int64 { initRpcMeta(r) - return r.Meta.ChunkInfo.GetStreamId() + return r.Meta.ChuckInfo.GetStreamId() } // getChunkId func (r *RpcDataPackage) getChunkId() int64 { initRpcMeta(r) - return r.Meta.ChunkInfo.GetChunkId() + return r.Meta.ChuckInfo.GetChunkId() } // IsChunkPackage @@ -585,5 +585,5 @@ func (r *RpcDataPackage) IsFinalPackage() bool { } func (r *RpcDataPackage) ClearChunkStatus() { - r.ChunkInfo(0, 0) + r.ChuckInfo(0, 0) } diff --git a/rpcpackage_test.go b/rpcpackage_test.go index 96e7fdd..b474d35 100644 --- a/rpcpackage_test.go +++ b/rpcpackage_test.go @@ -19,26 +19,10 @@ import ( "testing" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" . "github.com/smartystreets/goconvey/convey" + "google.golang.org/protobuf/proto" ) -//手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" -type DataMessage struct { - Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` -} - -func (m *DataMessage) Reset() { *m = DataMessage{} } -func (m *DataMessage) String() string { return proto.CompactTextString(m) } -func (*DataMessage) ProtoMessage() {} - -func (m *DataMessage) GetName() string { - if m.Name != nil { - return *m.Name - } - return "" -} - var sericeName = "thisIsAServiceName" var methodName = "thisIsAMethodName" var magicCode = "PRPC" @@ -66,11 +50,11 @@ func initRpcDataPackage() *baidurpc.RpcDataPackage { func equalRpcDataPackage(r baidurpc.RpcDataPackage, t *testing.T) error { Convey("test RpcDataPackage", func() { - So(sericeName, ShouldEqual, *r.Meta.Request.ServiceName) - So(methodName, ShouldEqual, *r.Meta.Request.MethodName) + So(sericeName, ShouldEqual, r.Meta.Request.ServiceName) + So(methodName, ShouldEqual, r.Meta.Request.MethodName) So(string(magicCode), ShouldEqual, string(r.GetMagicCode())) - So(*r.Meta.Request.LogId, ShouldEqual, logId) - So(*r.Meta.CorrelationId, ShouldEqual, correlationId) + So(r.Meta.Request.LogId, ShouldEqual, logId) + So(r.Meta.CorrelationId, ShouldEqual, correlationId) So(len(data), ShouldEqual, len(r.Data)) So(len(attachment), ShouldEqual, len(r.Attachment)) }) @@ -116,7 +100,7 @@ func WriteReaderWithRealData(rpcDataPackage *baidurpc.RpcDataPackage, Convey("Test with real data", func() { dataMessage := DataMessage{} name := "hello, xiemalin. this is repeated string aaaaaaaaaaaaaaaaaaaaaa" - dataMessage.Name = &name + dataMessage.Name = name data, err := proto.Marshal(&dataMessage) So(err, ShouldBeNil) @@ -137,7 +121,7 @@ func WriteReaderWithRealData(rpcDataPackage *baidurpc.RpcDataPackage, dataMessage2 := DataMessage{} proto.Unmarshal(newData, &dataMessage2) - So(name, ShouldEqual, *dataMessage2.Name) + So(name, ShouldEqual, dataMessage2.Name) }) } diff --git a/server.go b/server.go index e48f775..24fc5dc 100644 --- a/server.go +++ b/server.go @@ -32,7 +32,7 @@ import ( "github.com/funny/link" "github.com/baidu-golang/pbrpc/nettool" - "github.com/golang/protobuf/proto" + "google.golang.org/protobuf/proto" ) const ( @@ -160,6 +160,9 @@ func parsePbMetaFromType(t reflect.Type) []*PbFieldMeta { } func parseMetaString(meta string) *PbFieldMeta { + if len(meta) == 0 { + return nil // empty tag meta return directly + } partials := strings.Split(meta, ",") metaSize := len(partials) if metaSize >= 4 { @@ -230,9 +233,9 @@ type AuthService interface { } type TraceInfo struct { - TraceId *int64 - SpanId *int64 - ParentSpanId *int64 + TraceId int64 + SpanId int64 + ParentSpanId int64 RpcRequestMetaExt map[string]string } @@ -496,15 +499,9 @@ func (s *TcpServer) doHandleProcess(session *link.Session, r *RpcDataPackage) er traceInfo.RpcRequestMetaExt = r.GetRpcRequestMetaExt() traceRetrun := s.traceService.Trace(serviceName, methodName, traceInfo) if traceRetrun != nil { - if traceRetrun.TraceId != nil { - r.TraceId(*traceRetrun.TraceId) - } - if traceRetrun.SpanId != nil { - r.SpanId(*traceRetrun.SpanId) - } - if traceRetrun.ParentSpanId != nil { - r.ParentSpanId(*traceRetrun.ParentSpanId) - } + r.TraceId(traceRetrun.TraceId) + r.SpanId(traceRetrun.SpanId) + r.ParentSpanId(traceRetrun.ParentSpanId) if traceRetrun.RpcRequestMetaExt != nil { r.RpcRequestMetaExt(traceRetrun.RpcRequestMetaExt) } @@ -538,7 +535,7 @@ func (s *TcpServer) doHandleProcess(session *link.Session, r *RpcDataPackage) er ec := &ErrorContext{} // do service here - messageRet, attachment, err := s.doServiceInvoke(ec, msg, *r.Meta.Request.ServiceName, *r.Meta.Request.MethodName, r.GetAttachment(), r.GetLogId(), service) + messageRet, attachment, err := s.doServiceInvoke(ec, msg, r.Meta.Request.ServiceName, r.Meta.Request.MethodName, r.GetAttachment(), r.GetLogId(), service) if ec.err != nil { err = ec.err } @@ -603,8 +600,8 @@ func (s *TcpServer) doServiceInvoke(c *ErrorContext, msg proto.Message, sname, m func wrapResponse(r *RpcDataPackage, errorCode int, errorText string) { r.GetMeta().Response = &Response{} - r.GetMeta().GetResponse().ErrorCode = proto.Int32(int32(errorCode)) - r.GetMeta().GetResponse().ErrorText = proto.String(errorText) + r.GetMeta().GetResponse().ErrorCode = int32(errorCode) + r.GetMeta().GetResponse().ErrorText = errorText } func GetServiceId(serviceName, methodName string) string { diff --git a/server_test.go b/server_test.go index 8ba03fb..fa5e7e1 100644 --- a/server_test.go +++ b/server_test.go @@ -20,8 +20,8 @@ import ( "testing" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" . "github.com/smartystreets/goconvey/convey" + "google.golang.org/protobuf/proto" ) const ( @@ -126,7 +126,7 @@ func (ss *SimpleService) DoService(msg proto.Message, attachment []byte, logId * if msg != nil { - var name *string = nil + var name string m, ok := msg.(*DataMessage) if !ok { @@ -135,14 +135,14 @@ func (ss *SimpleService) DoService(msg proto.Message, attachment []byte, logId * } name = m.Name - if len(*name) == 0 { + if len(name) == 0 { ret = ret + "veryone" } else { - ret = ret + *name + ret = ret + name } } dm := DataMessage{} - dm.Name = proto.String(ret) + dm.Name = ret return &dm, []byte{1, 5, 9}, nil } diff --git a/status.go b/status.go index 0cf9317..8c2e461 100644 --- a/status.go +++ b/status.go @@ -26,7 +26,6 @@ import ( "fmt" "time" - "github.com/golang/protobuf/proto" "github.com/jhunters/timewheel" ) @@ -35,36 +34,6 @@ type HttpStatusView struct { server *TcpServer } -type RPCStatus struct { - Host string `protobuf:"bytes,1,opt,name=host,proto3" json:"host,omitempty"` - Port int32 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"` - TimeoutSenconds int32 `protobuf:"varint,3,opt,name=timeout,proto3" json:"timeout,omitempty"` - Methods []*RPCMethod `protobuf:"bytes,4,rep,name=methods,proto3" json:"methods,omitempty"` -} - -type RPCMethod struct { - Service string `protobuf:"bytes,1,opt,name=service,proto3" json:"service,omitempty"` - Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"` - InTypeMeta string `protobuf:"bytes,3,opt,name=intype,proto3" json:"intype,omitempty"` - ReturnTypeMeta string `protobuf:"bytes,4,opt,name=returntype,proto3" json:"returntype,omitempty"` -} - -type QpsData struct { - Qpsinfo map[int64]int32 `protobuf:"bytes,1,rep,name=qpsinfo,proto3" json:"qpsinfo,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` -} - -func (m *RPCStatus) Reset() { *m = RPCStatus{} } -func (m *RPCStatus) String() string { return proto.CompactTextString(m) } -func (*RPCStatus) ProtoMessage() {} - -func (m *RPCMethod) Reset() { *m = RPCMethod{} } -func (m *RPCMethod) String() string { return proto.CompactTextString(m) } -func (*RPCMethod) ProtoMessage() {} - -func (m *QpsData) Reset() { *m = QpsData{} } -func (m *QpsData) String() string { return proto.CompactTextString(m) } -func (*QpsData) ProtoMessage() {} - func (hsv *HttpStatusView) Status(c context.Context) (*RPCStatus, context.Context) { s := hsv.server result := &RPCStatus{} diff --git a/status_test.go b/status_test.go index 58b385c..72ebf6f 100644 --- a/status_test.go +++ b/status_test.go @@ -32,8 +32,8 @@ import ( "time" baidurpc "github.com/baidu-golang/pbrpc" - "github.com/golang/protobuf/proto" . "github.com/smartystreets/goconvey/convey" + "google.golang.org/protobuf/proto" ) const ( @@ -101,7 +101,7 @@ func sendRpc(host string, port int, serviceName, methodName string, parameterIn, } rpcDataPackage, err := rpcClient.SendRpcRequest(rpcInvocation, parameterOut) - if int(*rpcDataPackage.Meta.GetResponse().ErrorCode) == baidurpc.ST_SERVICE_NOTFOUND { + if int(rpcDataPackage.Meta.GetResponse().ErrorCode) == baidurpc.ST_SERVICE_NOTFOUND { return fmt.Errorf("remote server not support this feature, please upgrade version") }