优化原始rpc,采用register方式

This commit is contained in:
boyce
2020-12-21 16:34:06 +08:00
parent ce2a14fa83
commit 4b67c59b1e
10 changed files with 294 additions and 194 deletions

View File

@@ -177,7 +177,7 @@ func (client *Client) AsyncCall(rpcHandler IRpcHandler,serviceMethod string,call
} }
seq := client.generateSeq() seq := client.generateSeq()
request:=MakeRpcRequest(processor,seq,serviceMethod,false,InParam) request:=MakeRpcRequest(processor,seq,0,serviceMethod,false,InParam)
bytes,err := processor.Marshal(request.RpcRequestData) bytes,err := processor.Marshal(request.RpcRequestData)
ReleaseRpcRequest(request) ReleaseRpcRequest(request)
if err != nil { if err != nil {
@@ -206,13 +206,13 @@ func (client *Client) AsyncCall(rpcHandler IRpcHandler,serviceMethod string,call
return nil return nil
} }
func (client *Client) RawGo(processor IRpcProcessor,noReply bool,serviceMethod string,args []byte,reply interface{}) *Call { func (client *Client) RawGo(processor IRpcProcessor,noReply bool,rpcMethodId uint32,serviceMethod string,args []byte,reply interface{}) *Call {
call := MakeCall() call := MakeCall()
call.ServiceMethod = serviceMethod call.ServiceMethod = serviceMethod
call.Reply = reply call.Reply = reply
call.Seq = client.generateSeq() call.Seq = client.generateSeq()
request := MakeRpcRequest(processor,call.Seq,serviceMethod,noReply,args) request := MakeRpcRequest(processor,call.Seq,rpcMethodId,serviceMethod,noReply,args)
bytes,err := processor.Marshal(request.RpcRequestData) bytes,err := processor.Marshal(request.RpcRequestData)
ReleaseRpcRequest(request) ReleaseRpcRequest(request)
if err != nil { if err != nil {
@@ -250,7 +250,7 @@ func (client *Client) Go(noReply bool,serviceMethod string, args interface{},rep
return call return call
} }
return client.RawGo(processor,noReply,serviceMethod,InParam,reply) return client.RawGo(processor,noReply,0,serviceMethod,InParam,reply)
} }
func (client *Client) Run(){ func (client *Client) Run(){

View File

@@ -12,11 +12,12 @@ type JsonProcessor struct {
type JsonRpcRequestData struct { type JsonRpcRequestData struct {
//packhead //packhead
Seq uint64 // sequence number chosen by client Seq uint64 // sequence number chosen by client
rpcMethodId uint32
ServiceMethod string // format: "Service.Method" ServiceMethod string // format: "Service.Method"
NoReply bool //是否需要返回 NoReply bool //是否需要返回
//packbody //packbody
InParam []byte InParam []byte
} }
type JsonRpcResponseData struct { type JsonRpcResponseData struct {
@@ -49,9 +50,10 @@ func (jsonProcessor *JsonProcessor) Unmarshal(data []byte, v interface{}) error{
return json.Unmarshal(data,v) return json.Unmarshal(data,v)
} }
func (jsonProcessor *JsonProcessor) MakeRpcRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{ func (jsonProcessor *JsonProcessor) MakeRpcRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{
jsonRpcRequestData := rpcJsonRequestDataPool.Get().(*JsonRpcRequestData) jsonRpcRequestData := rpcJsonRequestDataPool.Get().(*JsonRpcRequestData)
jsonRpcRequestData.Seq = seq jsonRpcRequestData.Seq = seq
jsonRpcRequestData.rpcMethodId = rpcMethodId
jsonRpcRequestData.ServiceMethod = serviceMethod jsonRpcRequestData.ServiceMethod = serviceMethod
jsonRpcRequestData.NoReply = noReply jsonRpcRequestData.NoReply = noReply
jsonRpcRequestData.InParam = inParam jsonRpcRequestData.InParam = inParam
@@ -92,6 +94,10 @@ func (jsonRpcRequestData *JsonRpcRequestData) GetSeq() uint64{
return jsonRpcRequestData.Seq return jsonRpcRequestData.Seq
} }
func (jsonRpcRequestData *JsonRpcRequestData) GetRpcMethodId() uint32{
return jsonRpcRequestData.rpcMethodId
}
func (jsonRpcRequestData *JsonRpcRequestData) GetServiceMethod() string{ func (jsonRpcRequestData *JsonRpcRequestData) GetServiceMethod() string{
return jsonRpcRequestData.ServiceMethod return jsonRpcRequestData.ServiceMethod
} }

View File

@@ -22,8 +22,9 @@ func init(){
} }
} }
func (slf *PBRpcRequestData) MakeRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) *PBRpcRequestData{ func (slf *PBRpcRequestData) MakeRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) *PBRpcRequestData{
slf.Seq = seq slf.Seq = seq
slf.RpcMethodId = rpcMethodId
slf.ServiceMethod = serviceMethod slf.ServiceMethod = serviceMethod
slf.NoReply = noReply slf.NoReply = noReply
slf.InParam = inParam slf.InParam = inParam
@@ -31,6 +32,7 @@ func (slf *PBRpcRequestData) MakeRequest(seq uint64,serviceMethod string,noReply
return slf return slf
} }
func (slf *PBRpcResponseData) MakeRespone(seq uint64,err RpcError,reply []byte) *PBRpcResponseData{ func (slf *PBRpcResponseData) MakeRespone(seq uint64,err RpcError,reply []byte) *PBRpcResponseData{
slf.Seq = seq slf.Seq = seq
slf.Error = err.Error() slf.Error = err.Error()
@@ -48,9 +50,9 @@ func (slf *PBProcessor) Unmarshal(data []byte, msg interface{}) error{
return proto.Unmarshal(data, protoMsg) return proto.Unmarshal(data, protoMsg)
} }
func (slf *PBProcessor) MakeRpcRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{ func (slf *PBProcessor) MakeRpcRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{
pPbRpcRequestData := rpcPbRequestDataPool.Get().(*PBRpcRequestData) pPbRpcRequestData := rpcPbRequestDataPool.Get().(*PBRpcRequestData)
pPbRpcRequestData.MakeRequest(seq,serviceMethod,noReply,inParam) pPbRpcRequestData.MakeRequest(seq,rpcMethodId,serviceMethod,noReply,inParam)
return pPbRpcRequestData return pPbRpcRequestData
} }

View File

@@ -3,7 +3,7 @@ package rpc
type IRpcProcessor interface { type IRpcProcessor interface {
Marshal(v interface{}) ([]byte, error) //b表示自定义缓冲区可以填nil由系统自动分配 Marshal(v interface{}) ([]byte, error) //b表示自定义缓冲区可以填nil由系统自动分配
Unmarshal(data []byte, v interface{}) error Unmarshal(data []byte, v interface{}) error
MakeRpcRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData MakeRpcRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData
MakeRpcResponse(seq uint64,err RpcError,reply []byte) IRpcResponseData MakeRpcResponse(seq uint64,err RpcError,reply []byte) IRpcResponseData
ReleaseRpcRequest(rpcRequestData IRpcRequestData) ReleaseRpcRequest(rpcRequestData IRpcRequestData)

View File

@@ -40,6 +40,7 @@ type IRpcRequestData interface {
GetServiceMethod() string GetServiceMethod() string
GetInParam() []byte GetInParam() []byte
IsNoReply() bool IsNoReply() bool
GetRpcMethodId() uint32
} }
type IRpcResponseData interface { type IRpcResponseData interface {
@@ -120,10 +121,10 @@ func (call *Call) Done() *Call{
return <-call.done return <-call.done
} }
func MakeRpcRequest(rpcProcessor IRpcProcessor,seq uint64,serviceMethod string,noReply bool,inParam []byte) *RpcRequest{ func MakeRpcRequest(rpcProcessor IRpcProcessor,seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) *RpcRequest{
rpcRequest := rpcRequestPool.Get().(*RpcRequest).Clear() rpcRequest := rpcRequestPool.Get().(*RpcRequest).Clear()
rpcRequest.rpcProcessor = rpcProcessor rpcRequest.rpcProcessor = rpcProcessor
rpcRequest.RpcRequestData = rpcRequest.rpcProcessor.MakeRpcRequest(seq,serviceMethod,noReply,inParam) rpcRequest.RpcRequestData = rpcRequest.rpcProcessor.MakeRpcRequest(seq,rpcMethodId,serviceMethod,noReply,inParam)
rpcRequest.ref = true rpcRequest.ref = true
return rpcRequest return rpcRequest

View File

@@ -1,163 +1,268 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// source: rpcproto/rpc.proto // versions:
// protoc-gen-go v1.23.0
// protoc v3.11.4
// source: proto/rpcproto/rpc.proto
package rpc package rpc
import ( import (
fmt "fmt"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
math "math" protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
) )
// Reference imports to suppress errors if they are not otherwise used. const (
var _ = proto.Marshal // Verify that this generated code is sufficiently up-to-date.
var _ = fmt.Errorf _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
var _ = math.Inf // Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion to ensure that this generated file // This is a compile-time assertion that a sufficiently up-to-date version
// is compatible with the proto package it is being compiled against. // of the legacy proto package is being used.
// A compilation error at this line likely means your copy of the const _ = proto.ProtoPackageIsVersion4
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type PBRpcRequestData struct { type PBRpcRequestData struct {
Seq uint64 `protobuf:"varint,1,opt,name=Seq,proto3" json:"Seq,omitempty"` state protoimpl.MessageState
ServiceMethod string `protobuf:"bytes,2,opt,name=ServiceMethod,proto3" json:"ServiceMethod,omitempty"` sizeCache protoimpl.SizeCache
NoReply bool `protobuf:"varint,3,opt,name=NoReply,proto3" json:"NoReply,omitempty"` unknownFields protoimpl.UnknownFields
InParam []byte `protobuf:"bytes,4,opt,name=InParam,proto3" json:"InParam,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` Seq uint64 `protobuf:"varint,1,opt,name=Seq,proto3" json:"Seq,omitempty"`
XXX_unrecognized []byte `json:"-"` RpcMethodId uint32 `protobuf:"varint,2,opt,name=RpcMethodId,proto3" json:"RpcMethodId,omitempty"`
XXX_sizecache int32 `json:"-"` ServiceMethod string `protobuf:"bytes,3,opt,name=ServiceMethod,proto3" json:"ServiceMethod,omitempty"`
NoReply bool `protobuf:"varint,4,opt,name=NoReply,proto3" json:"NoReply,omitempty"`
InParam []byte `protobuf:"bytes,5,opt,name=InParam,proto3" json:"InParam,omitempty"`
} }
func (m *PBRpcRequestData) Reset() { *m = PBRpcRequestData{} } func (x *PBRpcRequestData) Reset() {
func (m *PBRpcRequestData) String() string { return proto.CompactTextString(m) } *x = PBRpcRequestData{}
func (*PBRpcRequestData) ProtoMessage() {} if protoimpl.UnsafeEnabled {
mi := &file_proto_rpcproto_rpc_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *PBRpcRequestData) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*PBRpcRequestData) ProtoMessage() {}
func (x *PBRpcRequestData) ProtoReflect() protoreflect.Message {
mi := &file_proto_rpcproto_rpc_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 PBRpcRequestData.ProtoReflect.Descriptor instead.
func (*PBRpcRequestData) Descriptor() ([]byte, []int) { func (*PBRpcRequestData) Descriptor() ([]byte, []int) {
return fileDescriptor_0008ed1de5480352, []int{0} return file_proto_rpcproto_rpc_proto_rawDescGZIP(), []int{0}
} }
func (m *PBRpcRequestData) XXX_Unmarshal(b []byte) error { func (x *PBRpcRequestData) GetSeq() uint64 {
return xxx_messageInfo_PBRpcRequestData.Unmarshal(m, b) if x != nil {
} return x.Seq
func (m *PBRpcRequestData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_PBRpcRequestData.Marshal(b, m, deterministic)
}
func (m *PBRpcRequestData) XXX_Merge(src proto.Message) {
xxx_messageInfo_PBRpcRequestData.Merge(m, src)
}
func (m *PBRpcRequestData) XXX_Size() int {
return xxx_messageInfo_PBRpcRequestData.Size(m)
}
func (m *PBRpcRequestData) XXX_DiscardUnknown() {
xxx_messageInfo_PBRpcRequestData.DiscardUnknown(m)
}
var xxx_messageInfo_PBRpcRequestData proto.InternalMessageInfo
func (m *PBRpcRequestData) GetSeq() uint64 {
if m != nil {
return m.Seq
} }
return 0 return 0
} }
func (m *PBRpcRequestData) GetServiceMethod() string { func (x *PBRpcRequestData) GetRpcMethodId() uint32 {
if m != nil { if x != nil {
return m.ServiceMethod return x.RpcMethodId
}
return 0
}
func (x *PBRpcRequestData) GetServiceMethod() string {
if x != nil {
return x.ServiceMethod
} }
return "" return ""
} }
func (m *PBRpcRequestData) GetNoReply() bool { func (x *PBRpcRequestData) GetNoReply() bool {
if m != nil { if x != nil {
return m.NoReply return x.NoReply
} }
return false return false
} }
func (m *PBRpcRequestData) GetInParam() []byte { func (x *PBRpcRequestData) GetInParam() []byte {
if m != nil { if x != nil {
return m.InParam return x.InParam
} }
return nil return nil
} }
type PBRpcResponseData struct { type PBRpcResponseData struct {
Seq uint64 `protobuf:"varint,1,opt,name=Seq,proto3" json:"Seq,omitempty"` state protoimpl.MessageState
Error string `protobuf:"bytes,2,opt,name=Error,proto3" json:"Error,omitempty"` sizeCache protoimpl.SizeCache
Reply []byte `protobuf:"bytes,3,opt,name=Reply,proto3" json:"Reply,omitempty"` unknownFields protoimpl.UnknownFields
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` Seq uint64 `protobuf:"varint,1,opt,name=Seq,proto3" json:"Seq,omitempty"`
XXX_sizecache int32 `json:"-"` Error string `protobuf:"bytes,2,opt,name=Error,proto3" json:"Error,omitempty"`
Reply []byte `protobuf:"bytes,3,opt,name=Reply,proto3" json:"Reply,omitempty"`
} }
func (m *PBRpcResponseData) Reset() { *m = PBRpcResponseData{} } func (x *PBRpcResponseData) Reset() {
func (m *PBRpcResponseData) String() string { return proto.CompactTextString(m) } *x = PBRpcResponseData{}
func (*PBRpcResponseData) ProtoMessage() {} if protoimpl.UnsafeEnabled {
mi := &file_proto_rpcproto_rpc_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *PBRpcResponseData) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*PBRpcResponseData) ProtoMessage() {}
func (x *PBRpcResponseData) ProtoReflect() protoreflect.Message {
mi := &file_proto_rpcproto_rpc_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 PBRpcResponseData.ProtoReflect.Descriptor instead.
func (*PBRpcResponseData) Descriptor() ([]byte, []int) { func (*PBRpcResponseData) Descriptor() ([]byte, []int) {
return fileDescriptor_0008ed1de5480352, []int{1} return file_proto_rpcproto_rpc_proto_rawDescGZIP(), []int{1}
} }
func (m *PBRpcResponseData) XXX_Unmarshal(b []byte) error { func (x *PBRpcResponseData) GetSeq() uint64 {
return xxx_messageInfo_PBRpcResponseData.Unmarshal(m, b) if x != nil {
} return x.Seq
func (m *PBRpcResponseData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_PBRpcResponseData.Marshal(b, m, deterministic)
}
func (m *PBRpcResponseData) XXX_Merge(src proto.Message) {
xxx_messageInfo_PBRpcResponseData.Merge(m, src)
}
func (m *PBRpcResponseData) XXX_Size() int {
return xxx_messageInfo_PBRpcResponseData.Size(m)
}
func (m *PBRpcResponseData) XXX_DiscardUnknown() {
xxx_messageInfo_PBRpcResponseData.DiscardUnknown(m)
}
var xxx_messageInfo_PBRpcResponseData proto.InternalMessageInfo
func (m *PBRpcResponseData) GetSeq() uint64 {
if m != nil {
return m.Seq
} }
return 0 return 0
} }
func (m *PBRpcResponseData) GetError() string { func (x *PBRpcResponseData) GetError() string {
if m != nil { if x != nil {
return m.Error return x.Error
} }
return "" return ""
} }
func (m *PBRpcResponseData) GetReply() []byte { func (x *PBRpcResponseData) GetReply() []byte {
if m != nil { if x != nil {
return m.Reply return x.Reply
} }
return nil return nil
} }
func init() { var File_proto_rpcproto_rpc_proto protoreflect.FileDescriptor
proto.RegisterType((*PBRpcRequestData)(nil), "rpc.PBRpcRequestData")
proto.RegisterType((*PBRpcResponseData)(nil), "rpc.PBRpcResponseData") var file_proto_rpcproto_rpc_proto_rawDesc = []byte{
0x0a, 0x18, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x70, 0x63, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x2f, 0x72, 0x70, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x72, 0x70, 0x63, 0x22,
0xa0, 0x01, 0x0a, 0x10, 0x50, 0x42, 0x52, 0x70, 0x63, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x65, 0x71, 0x18, 0x01, 0x20, 0x01, 0x28,
0x04, 0x52, 0x03, 0x53, 0x65, 0x71, 0x12, 0x20, 0x0a, 0x0b, 0x52, 0x70, 0x63, 0x4d, 0x65, 0x74,
0x68, 0x6f, 0x64, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0b, 0x52, 0x70, 0x63,
0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x49, 0x64, 0x12, 0x24, 0x0a, 0x0d, 0x53, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
0x0d, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x18,
0x0a, 0x07, 0x4e, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52,
0x07, 0x4e, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x6e, 0x50, 0x61,
0x72, 0x61, 0x6d, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x49, 0x6e, 0x50, 0x61, 0x72,
0x61, 0x6d, 0x22, 0x51, 0x0a, 0x11, 0x50, 0x42, 0x52, 0x70, 0x63, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x65, 0x71, 0x18, 0x01,
0x20, 0x01, 0x28, 0x04, 0x52, 0x03, 0x53, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x72, 0x72,
0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12,
0x14, 0x0a, 0x05, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05,
0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x3b, 0x72, 0x70, 0x63, 0x62, 0x06,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
func init() { proto.RegisterFile("rpcproto/rpc.proto", fileDescriptor_0008ed1de5480352) } var (
file_proto_rpcproto_rpc_proto_rawDescOnce sync.Once
file_proto_rpcproto_rpc_proto_rawDescData = file_proto_rpcproto_rpc_proto_rawDesc
)
var fileDescriptor_0008ed1de5480352 = []byte{ func file_proto_rpcproto_rpc_proto_rawDescGZIP() []byte {
// 193 bytes of a gzipped FileDescriptorProto file_proto_rpcproto_rpc_proto_rawDescOnce.Do(func() {
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2a, 0x2a, 0x48, 0x2e, file_proto_rpcproto_rpc_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_rpcproto_rpc_proto_rawDescData)
0x28, 0xca, 0x2f, 0xc9, 0xd7, 0x2f, 0x2a, 0x48, 0xd6, 0x03, 0xb3, 0x84, 0x98, 0x8b, 0x0a, 0x92, })
0x95, 0xea, 0xb8, 0x04, 0x02, 0x9c, 0x82, 0x0a, 0x92, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, return file_proto_rpcproto_rpc_proto_rawDescData
0x5c, 0x12, 0x4b, 0x12, 0x85, 0x04, 0xb8, 0x98, 0x83, 0x53, 0x0b, 0x25, 0x18, 0x15, 0x18, 0x35, }
0x58, 0x82, 0x40, 0x4c, 0x21, 0x15, 0x2e, 0xde, 0xe0, 0xd4, 0xa2, 0xb2, 0xcc, 0xe4, 0x54, 0xdf,
0xd4, 0x92, 0x8c, 0xfc, 0x14, 0x09, 0x26, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x54, 0x41, 0x21, 0x09, var file_proto_rpcproto_rpc_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
0x2e, 0x76, 0xbf, 0xfc, 0xa0, 0xd4, 0x82, 0x9c, 0x4a, 0x09, 0x66, 0x05, 0x46, 0x0d, 0x8e, 0x20, var file_proto_rpcproto_rpc_proto_goTypes = []interface{}{
0x18, 0x17, 0x24, 0xe3, 0x99, 0x17, 0x90, 0x58, 0x94, 0x98, 0x2b, 0xc1, 0xa2, 0xc0, 0xa8, 0xc1, (*PBRpcRequestData)(nil), // 0: rpc.PBRpcRequestData
0x13, 0x04, 0xe3, 0x2a, 0x05, 0x72, 0x09, 0x42, 0xed, 0x2f, 0x2e, 0xc8, 0xcf, 0x2b, 0x4e, 0xc5, (*PBRpcResponseData)(nil), // 1: rpc.PBRpcResponseData
0xe1, 0x00, 0x11, 0x2e, 0x56, 0xd7, 0xa2, 0xa2, 0xfc, 0x22, 0xa8, 0xc5, 0x10, 0x0e, 0x48, 0x14, }
0x61, 0x1d, 0x4f, 0x10, 0x84, 0xe3, 0xc4, 0x1e, 0xc5, 0xaa, 0x67, 0x5d, 0x54, 0x90, 0x9c, 0xc4, var file_proto_rpcproto_rpc_proto_depIdxs = []int32{
0x06, 0xf6, 0xa7, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0xcf, 0x5b, 0x74, 0xd8, 0xfd, 0x00, 0x00, 0, // [0:0] is the sub-list for method output_type
0x00, 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_proto_rpcproto_rpc_proto_init() }
func file_proto_rpcproto_rpc_proto_init() {
if File_proto_rpcproto_rpc_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_rpcproto_rpc_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*PBRpcRequestData); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_rpcproto_rpc_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*PBRpcResponseData); 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_proto_rpcproto_rpc_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_proto_rpcproto_rpc_proto_goTypes,
DependencyIndexes: file_proto_rpcproto_rpc_proto_depIdxs,
MessageInfos: file_proto_rpcproto_rpc_proto_msgTypes,
}.Build()
File_proto_rpcproto_rpc_proto = out.File
file_proto_rpcproto_rpc_proto_rawDesc = nil
file_proto_rpcproto_rpc_proto_goTypes = nil
file_proto_rpcproto_rpc_proto_depIdxs = nil
} }

View File

@@ -2,10 +2,11 @@ syntax = "proto3";
package rpc; package rpc;
option go_package =".;rpc"; option go_package =".;rpc";
message PBRpcRequestData{ message PBRpcRequestData{
uint64 Seq = 1; uint64 Seq = 1;
string ServiceMethod = 2; uint32 RpcMethodId = 2;
bool NoReply = 3; string ServiceMethod = 3;
bytes InParam = 4; bool NoReply = 4;
bytes InParam = 5;
} }
message PBRpcResponseData{ message PBRpcResponseData{

View File

@@ -45,10 +45,12 @@ type RpcMethodInfo struct {
rpcProcessorType RpcProcessorType rpcProcessorType RpcProcessorType
} }
type RawRpcCallBack func(rawData []byte)
type RpcHandler struct { type RpcHandler struct {
callRequest chan *RpcRequest callRequest chan *RpcRequest
rpcHandler IRpcHandler rpcHandler IRpcHandler
mapFunctions map[string]RpcMethodInfo mapFunctions map[string]RpcMethodInfo
mapRawFunctions map[uint32] RawRpcCallBack
funcRpcClient FuncRpcClient funcRpcClient FuncRpcClient
funcRpcServer FuncRpcServer funcRpcServer FuncRpcServer
@@ -79,7 +81,7 @@ type IRpcHandler interface {
AsyncCallNode(nodeId int,serviceMethod string,args interface{},callback interface{}) error AsyncCallNode(nodeId int,serviceMethod string,args interface{},callback interface{}) error
CallNode(nodeId int,serviceMethod string,args interface{},reply interface{}) error CallNode(nodeId int,serviceMethod string,args interface{},reply interface{}) error
GoNode(nodeId int,serviceMethod string,args interface{}) error GoNode(nodeId int,serviceMethod string,args interface{}) error
RawGoNode(rpcProcessorType RpcProcessorType,nodeId int,serviceMethod string,args IRawInputArgs) error RawGoNode(rpcProcessorType RpcProcessorType,nodeId int,rpcMethodId uint32,serviceName string,args IRawInputArgs) error
CastGo(serviceMethod string,args interface{}) CastGo(serviceMethod string,args interface{})
IsSingleCoroutine() bool IsSingleCoroutine() bool
} }
@@ -99,7 +101,7 @@ func (handler *RpcHandler) GetRpcHandler() IRpcHandler{
func (handler *RpcHandler) InitRpcHandler(rpcHandler IRpcHandler,getClientFun FuncRpcClient,getServerFun FuncRpcServer) { func (handler *RpcHandler) InitRpcHandler(rpcHandler IRpcHandler,getClientFun FuncRpcClient,getServerFun FuncRpcServer) {
handler.callRequest = make(chan *RpcRequest,1000000) handler.callRequest = make(chan *RpcRequest,1000000)
handler.callResponseCallBack = make(chan *Call,1000000) handler.callResponseCallBack = make(chan *Call,1000000)
handler.mapRawFunctions = make(map[uint32] RawRpcCallBack)
handler.rpcHandler = rpcHandler handler.rpcHandler = rpcHandler
handler.mapFunctions = map[string]RpcMethodInfo{} handler.mapFunctions = map[string]RpcMethodInfo{}
handler.funcRpcClient = getClientFun handler.funcRpcClient = getClientFun
@@ -157,15 +159,11 @@ func (handler *RpcHandler) suitableMethods(method reflect.Method) error {
return fmt.Errorf("%s Unsupported parameter types!",method.Name) return fmt.Errorf("%s Unsupported parameter types!",method.Name)
} }
} }
a := typ.In(parIdx).Kind()
if a == reflect.Interface { rpcMethodInfo.inParamValue = reflect.New(typ.In(parIdx).Elem()) //append(rpcMethodInfo.iparam,)
rpcMethodInfo.inParam = nil rpcMethodInfo.inParam = reflect.New(typ.In(parIdx).Elem()).Interface()
}else{ pt,_ := GetProcessorType(rpcMethodInfo.inParamValue.Interface())
rpcMethodInfo.inParamValue = reflect.New(typ.In(parIdx).Elem()) //append(rpcMethodInfo.iparam,) rpcMethodInfo.rpcProcessorType = pt
rpcMethodInfo.inParam = reflect.New(typ.In(parIdx).Elem()).Interface()
pt,_ := GetProcessorType(rpcMethodInfo.inParamValue.Interface())
rpcMethodInfo.rpcProcessorType = pt
}
parIdx++ parIdx++
if parIdx< typ.NumIn() { if parIdx< typ.NumIn() {
@@ -243,6 +241,20 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
defer request.inputArgs.DoGc() defer request.inputArgs.DoGc()
} }
//如果是原始RPC请求
rawRpcId := request.RpcRequestData.GetRpcMethodId()
if rawRpcId>0 {
v,ok := handler.mapRawFunctions[rawRpcId]
if ok == false {
err := fmt.Sprintf("RpcHandler cannot find request rpc id %d!",rawRpcId)
log.Error(err)
return
}
v(request.inputArgs.GetRawData())
return
}
//普通的rpc请求
v,ok := handler.mapFunctions[request.RpcRequestData.GetServiceMethod()] v,ok := handler.mapFunctions[request.RpcRequestData.GetServiceMethod()]
if ok == false { if ok == false {
err := "RpcHandler "+handler.rpcHandler.GetName()+"cannot find "+request.RpcRequestData.GetServiceMethod() err := "RpcHandler "+handler.rpcHandler.GetName()+"cannot find "+request.RpcRequestData.GetServiceMethod()
@@ -255,45 +267,22 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
var paramList []reflect.Value var paramList []reflect.Value
var err error var err error
var iParam interface{} iParam := reflect.New(v.inParamValue.Type().Elem()).Interface()
//单协程或非异步调用时直接使用预置对象
if v.inParam!= nil {
iParam = reflect.New(v.inParamValue.Type().Elem()).Interface()
}
if request.bLocalRequest == false { if request.bLocalRequest == false {
if iParam == nil { err = request.rpcProcessor.Unmarshal(request.RpcRequestData.GetInParam(),iParam)
//原始调用 if err!=nil {
iParam = request.RpcRequestData.GetInParam() rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
}else{ log.Error(rErr)
err = request.rpcProcessor.Unmarshal(request.RpcRequestData.GetInParam(),iParam) if request.requestHandle!=nil {
if err!=nil { request.requestHandle(nil, RpcError(rErr))
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
log.Error(rErr)
if request.requestHandle!=nil {
request.requestHandle(nil, RpcError(rErr))
}
return
} }
return
} }
}else { }else {
if iParam == nil { iParam = request.localParam
iParam = request.inputArgs.GetRawData()
}else if request.inputArgs!=nil {
err = request.rpcProcessor.Unmarshal(request.inputArgs.GetRawData(),iParam)
if err!=nil {
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
log.Error(rErr)
if request.requestHandle!=nil {
request.requestHandle(nil, RpcError(rErr))
}
return
}
}else {
iParam = request.localParam
}
} }
//生成Call参数
paramList = append(paramList,reflect.ValueOf(handler.GetRpcHandler())) //接受者 paramList = append(paramList,reflect.ValueOf(handler.GetRpcHandler())) //接受者
if v.hasResponder == true { if v.hasResponder == true {
if request.requestHandle!=nil { if request.requestHandle!=nil {
@@ -387,7 +376,7 @@ func (handler *RpcHandler) goRpc(processor IRpcProcessor,bCast bool,nodeId int,s
return pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceMethod,args,nil) return pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceMethod,args,nil)
} }
//其他的rpcHandler的处理器 //其他的rpcHandler的处理器
pCall := pLocalRpcServer.selfNodeRpcHandlerGo(processor,pClientList[i],true,serviceName,serviceMethod,args,nil,nil) pCall := pLocalRpcServer.selfNodeRpcHandlerGo(processor,pClientList[i],true,serviceName,0,serviceMethod,args,nil,nil)
if pCall.Err!=nil { if pCall.Err!=nil {
err = pCall.Err err = pCall.Err
} }
@@ -438,7 +427,7 @@ func (handler *RpcHandler) callRpc(nodeId int,serviceMethod string,args interfac
return pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceMethod,args,reply) return pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceMethod,args,reply)
} }
//其他的rpcHandler的处理器 //其他的rpcHandler的处理器
pCall := pLocalRpcServer.selfNodeRpcHandlerGo(nil,pClient,false,serviceName,serviceMethod,args,reply,nil) pCall := pLocalRpcServer.selfNodeRpcHandlerGo(nil,pClient,false,serviceName,0,serviceMethod,args,reply,nil)
err = pCall.Done().Err err = pCall.Done().Err
pClient.RemovePending(pCall.Seq) pClient.RemovePending(pCall.Seq)
ReleaseCall(pCall) ReleaseCall(pCall)
@@ -570,11 +559,11 @@ func (handler *RpcHandler) CastGo(serviceMethod string,args interface{}) {
handler.goRpc(nil,true,0,serviceMethod,args) handler.goRpc(nil,true,0,serviceMethod,args)
} }
//RawGoNode(rpcProcessorType RpcProcessorType,nodeId int,serviceName string,rpcMethodId uint32,args IRawInputArgs) error
func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType,nodeId int,serviceMethod string,args IRawInputArgs) error { func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType,nodeId int,rpcMethodId uint32,serviceName string,args IRawInputArgs) error {
processor := GetProcessor(uint8(rpcProcessorType)) processor := GetProcessor(uint8(rpcProcessorType))
var pClientList [maxClusterNode]*Client var pClientList [maxClusterNode]*Client
err,count := handler.funcRpcClient(nodeId,serviceMethod,pClientList[:]) err,count := handler.funcRpcClient(nodeId,serviceName,pClientList[:])
if count==0||err != nil { if count==0||err != nil {
args.DoGc() args.DoGc()
log.Error("Call serviceMethod is error:%+v!",err) log.Error("Call serviceMethod is error:%+v!",err)
@@ -591,26 +580,15 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType,nodeId in
for i:=0;i<count;i++{ for i:=0;i<count;i++{
if pClientList[i].bSelfNode == true { if pClientList[i].bSelfNode == true {
pLocalRpcServer:= handler.funcRpcServer() pLocalRpcServer:= handler.funcRpcServer()
//判断是否是同一服务
findIndex := strings.Index(serviceMethod,".")
if findIndex==-1 {
serr := fmt.Errorf("Call serviceMethod %s is error!",serviceMethod)
log.Error("%+v",serr)
if serr!= nil {
err = serr
}
continue
}
serviceName := serviceMethod[:findIndex]
//调用自己rpcHandler处理器 //调用自己rpcHandler处理器
if serviceName == handler.rpcHandler.GetName() { //自己服务调用 if serviceName == handler.rpcHandler.GetName() { //自己服务调用
err:= pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceMethod,args,nil) err:= pLocalRpcServer.myselfRpcHandlerGo(serviceName,serviceName,args,nil)
args.DoGc() args.DoGc()
return err return err
} }
//其他的rpcHandler的处理器 //其他的rpcHandler的处理器
pCall := pLocalRpcServer.selfNodeRpcHandlerGo(processor,pClientList[i],true,serviceName,serviceMethod,nil,nil,args) pCall := pLocalRpcServer.selfNodeRpcHandlerGo(processor,pClientList[i],true,serviceName,rpcMethodId,serviceName,nil,nil,args)
if pCall.Err!=nil { if pCall.Err!=nil {
err = pCall.Err err = pCall.Err
} }
@@ -620,7 +598,7 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType,nodeId in
} }
//跨node调用 //跨node调用
pCall := pClientList[i].RawGo(processor,true,serviceMethod,args.GetRawData(),nil) pCall := pClientList[i].RawGo(processor,true,rpcMethodId,serviceName,args.GetRawData(),nil)
args.DoGc() args.DoGc()
if pCall.Err!=nil { if pCall.Err!=nil {
err = pCall.Err err = pCall.Err
@@ -632,3 +610,6 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType,nodeId in
return err return err
} }
func (handler *RpcHandler) RegRawRpc(rpcMethodId uint32,rawRpcCB RawRpcCallBack){
handler.mapRawFunctions[rpcMethodId] = rawRpcCB
}

View File

@@ -127,7 +127,7 @@ func (agent *RpcAgent) Run() {
} }
//解析head //解析head
req := MakeRpcRequest(processor,0,"",false,nil) req := MakeRpcRequest(processor,0,0,"",false,nil)
err = processor.Unmarshal(data[1:],req.RpcRequestData) err = processor.Unmarshal(data[1:],req.RpcRequestData)
agent.conn.ReleaseReadMsg(data) agent.conn.ReleaseReadMsg(data)
if err != nil { if err != nil {
@@ -222,7 +222,7 @@ func (server *Server) myselfRpcHandlerGo(handlerName string,serviceMethod string
} }
func (server *Server) selfNodeRpcHandlerGo(processor IRpcProcessor,client *Client,noReply bool,handlerName string,serviceMethod string, args interface{},reply interface{},inputArgs IRawInputArgs) *Call { func (server *Server) selfNodeRpcHandlerGo(processor IRpcProcessor,client *Client,noReply bool,handlerName string,rpcMethodId uint32,serviceMethod string, args interface{},reply interface{},inputArgs IRawInputArgs) *Call {
pCall := MakeCall() pCall := MakeCall()
pCall.Seq = client.generateSeq() pCall.Seq = client.generateSeq()
@@ -242,7 +242,7 @@ func (server *Server) selfNodeRpcHandlerGo(processor IRpcProcessor,client *Clien
if processor == nil { if processor == nil {
_,processor = GetProcessorType(args) _,processor = GetProcessorType(args)
} }
req := MakeRpcRequest(processor,0, serviceMethod,noReply,nil) req := MakeRpcRequest(processor,0,rpcMethodId, serviceMethod,noReply,nil)
req.bLocalRequest = true req.bLocalRequest = true
req.localParam = args req.localParam = args
@@ -285,7 +285,7 @@ func (server *Server) selfNodeRpcHandlerAsyncGo(client *Client,callerRpcHandler
} }
_,processor := GetProcessorType(args) _,processor := GetProcessorType(args)
req := MakeRpcRequest(processor,0,serviceMethod,noReply,nil) req := MakeRpcRequest(processor,0,0,serviceMethod,noReply,nil)
req.localParam = args req.localParam = args
req.localReply = reply req.localReply = reply
req.bLocalRequest = true req.bLocalRequest = true

View File

@@ -207,4 +207,8 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
func (s *Service) IsSingleCoroutine() bool { func (s *Service) IsSingleCoroutine() bool {
return s.goroutineNum == 1 return s.goroutineNum == 1
}
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
} }