mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-15 21:37:31 +08:00
优化协程池退出
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/util/queue"
|
"github.com/duanhf2012/origin/v2/util/queue"
|
||||||
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var idleTimeout = int64(2 * time.Second)
|
var idleTimeout = int64(2 * time.Second)
|
||||||
@@ -30,6 +31,9 @@ type dispatch struct {
|
|||||||
|
|
||||||
waitWorker sync.WaitGroup
|
waitWorker sync.WaitGroup
|
||||||
waitDispatch 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)) {
|
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.workerQueue = make(chan task)
|
||||||
d.cbChannel = cbChannel
|
d.cbChannel = cbChannel
|
||||||
d.queueIdChannel = make(chan int64, cap(tasks))
|
d.queueIdChannel = make(chan int64, cap(tasks))
|
||||||
|
d.cancelContext,d.cancel = context.WithCancel(context.Background())
|
||||||
d.waitDispatch.Add(1)
|
d.waitDispatch.Add(1)
|
||||||
go d.run()
|
go d.run()
|
||||||
}
|
}
|
||||||
@@ -64,10 +68,12 @@ func (d *dispatch) run() {
|
|||||||
d.processqueueEvent(queueId)
|
d.processqueueEvent(queueId)
|
||||||
case <-timeout.C:
|
case <-timeout.C:
|
||||||
d.processTimer()
|
d.processTimer()
|
||||||
if atomic.LoadInt32(&d.minConcurrentNum) == -1 && len(d.tasks) == 0 {
|
case <- d.cancelContext.Done():
|
||||||
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 10))
|
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 5))
|
||||||
}
|
|
||||||
timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout)))
|
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() {
|
func (d *dispatch) close() {
|
||||||
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
||||||
|
d.cancel()
|
||||||
|
|
||||||
|
|
||||||
breakFor:
|
breakFor:
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ type IRpcHandler interface {
|
|||||||
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
||||||
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
||||||
CastGo(serviceMethod string, args interface{}) error
|
CastGo(serviceMethod string, args interface{}) error
|
||||||
IsSingleCoroutine() bool
|
|
||||||
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
||||||
GetRpcServer() FuncRpcServer
|
GetRpcServer() FuncRpcServer
|
||||||
}
|
}
|
||||||
@@ -539,10 +538,6 @@ func (handler *RpcHandler) GetName() string {
|
|||||||
return handler.rpcHandler.GetName()
|
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 {
|
func (handler *RpcHandler) CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error {
|
||||||
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
|
return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ type Service struct {
|
|||||||
serviceCfg interface{}
|
serviceCfg interface{}
|
||||||
goroutineNum int32
|
goroutineNum int32
|
||||||
startStatus bool
|
startStatus bool
|
||||||
|
isRelease int32
|
||||||
retire int32
|
retire int32
|
||||||
eventProcessor event.IEventProcessor
|
eventProcessor event.IEventProcessor
|
||||||
profiler *profiler.Profiler //性能分析器
|
profiler *profiler.Profiler //性能分析器
|
||||||
@@ -148,6 +149,7 @@ func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServe
|
|||||||
|
|
||||||
func (s *Service) Start() {
|
func (s *Service) Start() {
|
||||||
s.startStatus = true
|
s.startStatus = true
|
||||||
|
atomic.StoreInt32(&s.isRelease,0)
|
||||||
var waitRun sync.WaitGroup
|
var waitRun sync.WaitGroup
|
||||||
|
|
||||||
for i:=int32(0);i< s.goroutineNum;i++{
|
for i:=int32(0);i< s.goroutineNum;i++{
|
||||||
@@ -176,6 +178,7 @@ func (s *Service) Run() {
|
|||||||
select {
|
select {
|
||||||
case <- s.closeSig:
|
case <- s.closeSig:
|
||||||
bStop = true
|
bStop = true
|
||||||
|
s.Release()
|
||||||
concurrent.Close()
|
concurrent.Close()
|
||||||
case cb:=<-concurrentCBChannel:
|
case cb:=<-concurrentCBChannel:
|
||||||
concurrent.DoCallback(cb)
|
concurrent.DoCallback(cb)
|
||||||
@@ -248,10 +251,6 @@ func (s *Service) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if bStop == true {
|
if bStop == true {
|
||||||
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
|
|
||||||
s.startStatus = false
|
|
||||||
s.Release()
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,8 +273,11 @@ func (s *Service) Release(){
|
|||||||
log.Dump(string(buf[:l]),log.String("error",errString))
|
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(){
|
func (s *Service) OnRelease(){
|
||||||
@@ -326,10 +328,6 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
|
|||||||
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) IsSingleCoroutine() bool {
|
|
||||||
return s.goroutineNum == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
|
func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){
|
||||||
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
|
s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user