mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化rpc内存池
This commit is contained in:
@@ -1,26 +1,20 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/util/sync"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type GoGoPBProcessor struct {
|
||||
}
|
||||
|
||||
var rpcGoGoPbResponseDataPool sync.Pool
|
||||
var rpcGoGoPbRequestDataPool sync.Pool
|
||||
var rpcGoGoPbResponseDataPool =sync.NewPool(make(chan interface{},10240), func()interface{}{
|
||||
return &GoGoPBRpcResponseData{}
|
||||
})
|
||||
|
||||
|
||||
func init(){
|
||||
rpcGoGoPbResponseDataPool.New = func()interface{}{
|
||||
return &GoGoPBRpcResponseData{}
|
||||
}
|
||||
|
||||
rpcGoGoPbRequestDataPool.New = func()interface{}{
|
||||
return &GoGoPBRpcRequestData{}
|
||||
}
|
||||
}
|
||||
var rpcGoGoPbRequestDataPool =sync.NewPool(make(chan interface{},10240), func()interface{}{
|
||||
return &GoGoPBRpcRequestData{}
|
||||
})
|
||||
|
||||
func (slf *GoGoPBRpcRequestData) MakeRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) *GoGoPBRpcRequestData{
|
||||
slf.Seq = seq
|
||||
|
||||
@@ -2,7 +2,7 @@ package rpc
|
||||
|
||||
import (
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"sync"
|
||||
"github.com/duanhf2012/origin/util/sync"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
@@ -29,18 +29,13 @@ type JsonRpcResponseData struct {
|
||||
Reply []byte
|
||||
}
|
||||
|
||||
var rpcJsonResponseDataPool sync.Pool
|
||||
var rpcJsonRequestDataPool sync.Pool
|
||||
var rpcJsonResponseDataPool=sync.NewPool(make(chan interface{},10240), func()interface{}{
|
||||
return &JsonRpcResponseData{}
|
||||
})
|
||||
|
||||
func init(){
|
||||
rpcJsonResponseDataPool.New = func()interface{}{
|
||||
return &JsonRpcResponseData{}
|
||||
}
|
||||
|
||||
rpcJsonRequestDataPool.New = func()interface{}{
|
||||
return &JsonRpcRequestData{}
|
||||
}
|
||||
}
|
||||
var rpcJsonRequestDataPool =sync.NewPool(make(chan interface{},10240), func()interface{}{
|
||||
return &JsonRpcRequestData{}
|
||||
})
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Marshal(v interface{}) ([]byte, error){
|
||||
return json.Marshal(v)
|
||||
|
||||
14
rpc/rpc.go
14
rpc/rpc.go
@@ -28,7 +28,6 @@ func (r *Responder) IsInvalid() bool {
|
||||
return reflect.ValueOf(*r).Pointer() == reflect.ValueOf(reqHandlerNull).Pointer()
|
||||
}
|
||||
|
||||
//var rpcResponsePool sync.Pool
|
||||
var rpcRequestPool = sync.NewPoolEx(make(chan sync.IPoolData,10240),func()sync.IPoolData{
|
||||
return &RpcRequest{}
|
||||
})
|
||||
@@ -148,31 +147,20 @@ func MakeRpcRequest(rpcProcessor IRpcProcessor,seq uint64,rpcMethodId uint32,ser
|
||||
rpcRequest := rpcRequestPool.Get().(*RpcRequest).Clear()
|
||||
rpcRequest.rpcProcessor = rpcProcessor
|
||||
rpcRequest.RpcRequestData = rpcRequest.rpcProcessor.MakeRpcRequest(seq,rpcMethodId,serviceMethod,noReply,inParam)
|
||||
rpcRequest.ref = true
|
||||
|
||||
return rpcRequest
|
||||
}
|
||||
|
||||
func ReleaseRpcRequest(rpcRequest *RpcRequest){
|
||||
if rpcRequest.ref == false {
|
||||
panic("Duplicate memory release!")
|
||||
}
|
||||
rpcRequest.ref = false
|
||||
rpcRequest.rpcProcessor.ReleaseRpcRequest(rpcRequest.RpcRequestData)
|
||||
rpcRequestPool.Put(rpcRequest)
|
||||
}
|
||||
|
||||
func MakeCall() *Call {
|
||||
call := rpcCallPool.Get().(*Call).Clear()
|
||||
call.ref = true
|
||||
return call
|
||||
return rpcCallPool.Get().(*Call)
|
||||
}
|
||||
|
||||
func ReleaseCall(call *Call){
|
||||
if call.ref == false {
|
||||
panic("Duplicate memory release!")
|
||||
}
|
||||
call.ref = false
|
||||
rpcCallPool.Put(call)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user