优化内存池

This commit is contained in:
boyce
2021-01-07 18:34:32 +08:00
parent e712e24de5
commit e6c01c9071
3 changed files with 118 additions and 25 deletions

View File

@@ -2,7 +2,7 @@ package rpc
import (
"reflect"
"sync"
"github.com/duanhf2012/origin/util/sync"
"time"
)
@@ -29,8 +29,13 @@ func (r *Responder) IsInvalid() bool {
}
//var rpcResponsePool sync.Pool
var rpcRequestPool sync.Pool
var rpcCallPool sync.Pool
var rpcRequestPool = sync.NewPoolEx(make(chan sync.IPoolData,10240),func()sync.IPoolData{
return &RpcRequest{}
})
var rpcCallPool = sync.NewPoolEx(make(chan sync.IPoolData,10240),func()sync.IPoolData{
return &Call{done:make(chan *Call,1)}
})
type IRpcRequestData interface {
@@ -72,16 +77,6 @@ type Call struct {
callTime time.Time
}
func init(){
rpcRequestPool.New = func() interface{} {
return &RpcRequest{}
}
rpcCallPool.New = func() interface{} {
return &Call{done:make(chan *Call,1)}
}
}
func (slf *RpcRequest) Clear() *RpcRequest{
slf.RpcRequestData = nil
slf.localReply = nil
@@ -92,6 +87,22 @@ func (slf *RpcRequest) Clear() *RpcRequest{
return slf
}
func (slf *RpcRequest) Reset() {
slf.Clear()
}
func (slf *RpcRequest) IsRef()bool{
return slf.ref
}
func (slf *RpcRequest) Ref(){
slf.ref = true
}
func (slf *RpcRequest) UnRef(){
slf.ref = false
}
func (rpcResponse *RpcResponse) Clear() *RpcResponse{
rpcResponse.RpcResponseData = nil
return rpcResponse
@@ -113,6 +124,22 @@ func (call *Call) Clear() *Call{
return call
}
func (call *Call) Reset() {
call.Clear()
}
func (call *Call) IsRef()bool{
return call.ref
}
func (call *Call) Ref(){
call.ref = true
}
func (call *Call) UnRef(){
call.ref = false
}
func (call *Call) Done() *Call{
return <-call.done
}