优化协程池退出

This commit is contained in:
boyce
2024-06-14 15:42:23 +08:00
parent d1935b1bbc
commit 9c26c742fe
3 changed files with 20 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/util/queue"
"context"
)
var idleTimeout = int64(2 * time.Second)
@@ -30,6 +31,9 @@ type dispatch struct {
waitWorker sync.WaitGroup
waitDispatch sync.WaitGroup
cancelContext context.Context
cancel context.CancelFunc
}
func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan task, cbChannel chan func(error)) {
@@ -40,7 +44,7 @@ func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan
d.workerQueue = make(chan task)
d.cbChannel = cbChannel
d.queueIdChannel = make(chan int64, cap(tasks))
d.cancelContext,d.cancel = context.WithCancel(context.Background())
d.waitDispatch.Add(1)
go d.run()
}
@@ -64,10 +68,12 @@ func (d *dispatch) run() {
d.processqueueEvent(queueId)
case <-timeout.C:
d.processTimer()
if atomic.LoadInt32(&d.minConcurrentNum) == -1 && len(d.tasks) == 0 {
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 10))
}
case <- d.cancelContext.Done():
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 5))
timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout)))
for i:=int32(0);i<d.workerNum;i++{
d.processIdle()
}
}
}
@@ -166,6 +172,8 @@ func (c *dispatch) pushAsyncDoCallbackEvent(cb func(err error)) {
func (d *dispatch) close() {
atomic.StoreInt32(&d.minConcurrentNum, -1)
d.cancel()
breakFor:
for {

View File

@@ -110,7 +110,6 @@ type IRpcHandler interface {
GoNode(nodeId string, serviceMethod string, args interface{}) error
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
CastGo(serviceMethod string, args interface{}) error
IsSingleCoroutine() bool
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
GetRpcServer() FuncRpcServer
}
@@ -539,10 +538,6 @@ func (handler *RpcHandler) GetName() string {
return handler.rpcHandler.GetName()
}
func (handler *RpcHandler) IsSingleCoroutine() bool {
return handler.rpcHandler.IsSingleCoroutine()
}
func (handler *RpcHandler) CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error {
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
}

View File

@@ -58,6 +58,7 @@ type Service struct {
serviceCfg interface{}
goroutineNum int32
startStatus bool
isRelease int32
retire int32
eventProcessor event.IEventProcessor
profiler *profiler.Profiler //性能分析器
@@ -148,6 +149,7 @@ func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServe
func (s *Service) Start() {
s.startStatus = true
atomic.StoreInt32(&s.isRelease,0)
var waitRun sync.WaitGroup
for i:=int32(0);i< s.goroutineNum;i++{
@@ -176,6 +178,7 @@ func (s *Service) Run() {
select {
case <- s.closeSig:
bStop = true
s.Release()
concurrent.Close()
case cb:=<-concurrentCBChannel:
concurrent.DoCallback(cb)
@@ -248,10 +251,6 @@ func (s *Service) Run() {
}
if bStop == true {
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
s.startStatus = false
s.Release()
}
break
}
}
@@ -274,8 +273,11 @@ func (s *Service) Release(){
log.Dump(string(buf[:l]),log.String("error",errString))
}
}()
s.self.OnRelease()
if atomic.AddInt32(&s.isRelease,-1) == -1{
s.self.OnRelease()
}
}
func (s *Service) OnRelease(){
@@ -326,10 +328,6 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
}
func (s *Service) IsSingleCoroutine() bool {
return s.goroutineNum == 1
}
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
}