From 0bcc1e11b330cd00f41fd1ea06de106cd4e82d9f Mon Sep 17 00:00:00 2001 From: aceld Date: Fri, 6 Dec 2024 16:56:07 +0800 Subject: [PATCH 1/3] fix:bug https://github.com/aceld/zinx/issues/347 --- znet/client.go | 8 ++----- znet/msghandler.go | 54 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/znet/client.go b/znet/client.go index d3b1d3b1..8e51e0d2 100644 --- a/znet/client.go +++ b/znet/client.go @@ -6,7 +6,6 @@ import ( "net" "time" - "github.com/aceld/zinx/zconf" "github.com/aceld/zinx/zdecoder" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" @@ -57,7 +56,7 @@ func NewClient(ip string, port int, opts ...ClientOption) ziface.IClient { Ip: ip, Port: port, - msgHandler: newMsgHandle(), + msgHandler: newCliMsgHandle(), packet: zpack.Factory().NewPack(ziface.ZinxDataPack), // Default to using Zinx's TLV packet format(默认使用zinx的TLV封包方式) decoder: zdecoder.NewTLVDecoder(), // Default to using Zinx's TLV decoder(默认使用zinx的TLV解码器) version: "tcp", @@ -81,7 +80,7 @@ func NewWsClient(ip string, port int, opts ...ClientOption) ziface.IClient { Ip: ip, Port: port, - msgHandler: newMsgHandle(), + msgHandler: newCliMsgHandle(), packet: zpack.Factory().NewPack(ziface.ZinxDataPack), // Default to using Zinx's TLV packet format(默认使用zinx的TLV封包方式) decoder: zdecoder.NewTLVDecoder(), // Default to using Zinx's TLV decoder(默认使用zinx的TLV解码器) version: "websocket", @@ -111,9 +110,6 @@ func NewTLSClient(ip string, port int, opts ...ClientOption) ziface.IClient { func (c *Client) Restart() { c.exitChan = make(chan struct{}) - // Set worker pool size to 0 to turn off the worker pool in the client (客户端将协程池关闭) - zconf.GlobalObject.WorkerPoolSize = 0 - go func() { addr := &net.TCPAddr{ diff --git a/znet/msghandler.go b/znet/msghandler.go index b848693e..7ff0200d 100644 --- a/znet/msghandler.go +++ b/znet/msghandler.go @@ -45,7 +45,7 @@ type MsgHandle struct { } // newMsgHandle creates MsgHandle -// zinxRole: IServer/IClient +// zinxRole: IServer func newMsgHandle() *MsgHandle { var freeWorkers map[uint32]struct{} if zconf.GlobalObject.WorkerMode == zconf.WorkerModeBind { @@ -61,15 +61,51 @@ func newMsgHandle() *MsgHandle { } handle := &MsgHandle{ - Apis: make(map[uint32]ziface.IRouter), - RouterSlices: NewRouterSlices(), - WorkerPoolSize: zconf.GlobalObject.WorkerPoolSize, - // One worker corresponds to one queue (一个worker对应一个queue) - TaskQueue: make([]chan ziface.IRequest, zconf.GlobalObject.WorkerPoolSize), - freeWorkers: freeWorkers, - builder: newChainBuilder(), + Apis: make(map[uint32]ziface.IRouter), + RouterSlices: NewRouterSlices(), + freeWorkers: freeWorkers, + builder: newChainBuilder(), } + // server + handle.WorkerPoolSize = zconf.GlobalObject.WorkerPoolSize + // One worker corresponds to one queue (一个worker对应一个queue) + handle.TaskQueue = make([]chan ziface.IRequest, handle.WorkerPoolSize) + + // It is necessary to add the MsgHandle to the responsibility chain here, and it is the last link in the responsibility chain. After decoding in the MsgHandle, data distribution is done by router + // (此处必须把 msghandler 添加到责任链中,并且是责任链最后一环,在msghandler中进行解码后由router做数据分发) + handle.builder.Tail(handle) + return handle +} + +// newCliMsgHandle creates MsgHandle +// zinxRole: IClient +func newCliMsgHandle() *MsgHandle { + var freeWorkers map[uint32]struct{} + if zconf.GlobalObject.WorkerMode == zconf.WorkerModeBind { + // Assign a workder to each link, avoid interactions when multiple links are processed by the same worker + // MaxWorkerTaskLen can also be reduced, for example, 50 + // 为每个链接分配一个workder,避免同一worker处理多个链接时的互相影响 + // 同时可以减小MaxWorkerTaskLen,比如50,因为每个worker的负担减轻了 + zconf.GlobalObject.WorkerPoolSize = uint32(zconf.GlobalObject.MaxConn) + freeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.WorkerPoolSize) + for i := uint32(0); i < zconf.GlobalObject.WorkerPoolSize; i++ { + freeWorkers[i] = struct{}{} + } + } + + handle := &MsgHandle{ + Apis: make(map[uint32]ziface.IRouter), + RouterSlices: NewRouterSlices(), + freeWorkers: freeWorkers, + builder: newChainBuilder(), + } + + // client: Set worker pool size to 0 to turn off the worker pool in the client (客户端将协程池关闭) + handle.WorkerPoolSize = 0 + // One worker corresponds to one queue (一个worker对应一个queue) + handle.TaskQueue = make([]chan ziface.IRequest, handle.WorkerPoolSize) + // It is necessary to add the MsgHandle to the responsibility chain here, and it is the last link in the responsibility chain. After decoding in the MsgHandle, data distribution is done by router // (此处必须把 msghandler 添加到责任链中,并且是责任链最后一环,在msghandler中进行解码后由router做数据分发) handle.builder.Tail(handle) @@ -138,7 +174,7 @@ func (mh *MsgHandle) Intercept(chain ziface.IChain) ziface.IcResp { switch request.(type) { case ziface.IRequest: iRequest := request.(ziface.IRequest) - if zconf.GlobalObject.WorkerPoolSize > 0 { + if mh.WorkerPoolSize > 0 { // If the worker pool mechanism has been started, hand over the message to the worker for processing // (已经启动工作池机制,将消息交给Worker处理) mh.SendMsgToTaskQueue(iRequest) From 72f6801fe3c4318eca72e88863b07de23d185076 Mon Sep 17 00:00:00 2001 From: aceld Date: Fri, 6 Dec 2024 17:46:22 +0800 Subject: [PATCH 2/3] fix bug: https://github.com/aceld/zinx/issues/347 --- znet/msghandler.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/znet/msghandler.go b/znet/msghandler.go index db121608..8cff96fd 100644 --- a/znet/msghandler.go +++ b/znet/msghandler.go @@ -93,7 +93,7 @@ func newMsgHandle() *MsgHandle { // server handle.WorkerPoolSize = zconf.GlobalObject.WorkerPoolSize // One worker corresponds to one queue (一个worker对应一个queue) - handle.TaskQueue = make([]chan ziface.IRequest, handle.WorkerPoolSize) + handle.TaskQueue = make([]chan ziface.IRequest, TaskQueueLen) // It is necessary to add the MsgHandle to the responsibility chain here, and it is the last link in the responsibility chain. After decoding in the MsgHandle, data distribution is done by router // (此处必须把 msghandler 添加到责任链中,并且是责任链最后一环,在msghandler中进行解码后由router做数据分发) @@ -105,6 +105,8 @@ func newMsgHandle() *MsgHandle { // zinxRole: IClient func newCliMsgHandle() *MsgHandle { var freeWorkers map[uint32]struct{} + var extraFreeWorkers map[uint32]struct{} + if zconf.GlobalObject.WorkerMode == zconf.WorkerModeBind { // Assign a workder to each link, avoid interactions when multiple links are processed by the same worker // MaxWorkerTaskLen can also be reduced, for example, 50 @@ -117,6 +119,22 @@ func newCliMsgHandle() *MsgHandle { } } + TaskQueueLen := zconf.GlobalObject.WorkerPoolSize + + if zconf.GlobalObject.WorkerMode == zconf.WorkerModeDynamicBind { + zlog.Ins().DebugF("WorkerMode = %s", zconf.WorkerModeDynamicBind) + freeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.WorkerPoolSize) + for i := uint32(0); i < zconf.GlobalObject.WorkerPoolSize; i++ { + freeWorkers[i] = struct{}{} + } + + extraFreeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.MaxConn-int(zconf.GlobalObject.WorkerPoolSize)) + for i := zconf.GlobalObject.WorkerPoolSize; i < uint32(zconf.GlobalObject.MaxConn); i++ { + extraFreeWorkers[i] = struct{}{} + } + TaskQueueLen = uint32(zconf.GlobalObject.MaxConn) + } + handle := &MsgHandle{ Apis: make(map[uint32]ziface.IRouter), RouterSlices: NewRouterSlices(), @@ -126,8 +144,9 @@ func newCliMsgHandle() *MsgHandle { // client: Set worker pool size to 0 to turn off the worker pool in the client (客户端将协程池关闭) handle.WorkerPoolSize = 0 + TaskQueueLen = 0 // One worker corresponds to one queue (一个worker对应一个queue) - handle.TaskQueue = make([]chan ziface.IRequest, handle.WorkerPoolSize) + handle.TaskQueue = make([]chan ziface.IRequest, TaskQueueLen) // It is necessary to add the MsgHandle to the responsibility chain here, and it is the last link in the responsibility chain. After decoding in the MsgHandle, data distribution is done by router // (此处必须把 msghandler 添加到责任链中,并且是责任链最后一环,在msghandler中进行解码后由router做数据分发) From d3ec644a0ac3574d364dc2cb7cab53cbf61ba935 Mon Sep 17 00:00:00 2001 From: aceld Date: Fri, 6 Dec 2024 18:10:54 +0800 Subject: [PATCH 3/3] fix:https://github.com/aceld/zinx/issues/347 --- znet/msghandler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/znet/msghandler.go b/znet/msghandler.go index 8cff96fd..b08adecc 100644 --- a/znet/msghandler.go +++ b/znet/msghandler.go @@ -88,6 +88,8 @@ func newMsgHandle() *MsgHandle { RouterSlices: NewRouterSlices(), freeWorkers: freeWorkers, builder: newChainBuilder(), + // 可额外临时分配的workerID集合 + extraFreeWorkers: extraFreeWorkers, } // server @@ -140,11 +142,12 @@ func newCliMsgHandle() *MsgHandle { RouterSlices: NewRouterSlices(), freeWorkers: freeWorkers, builder: newChainBuilder(), + // 可额外临时分配的workerID集合 + extraFreeWorkers: extraFreeWorkers, } // client: Set worker pool size to 0 to turn off the worker pool in the client (客户端将协程池关闭) handle.WorkerPoolSize = 0 - TaskQueueLen = 0 // One worker corresponds to one queue (一个worker对应一个queue) handle.TaskQueue = make([]chan ziface.IRequest, TaskQueueLen)