-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
修复"如果程序里同时启动了 Server 和 Client ,GlobalObject 混淆的问题" #349
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ type MsgHandle struct { | |
} | ||
|
||
// newMsgHandle creates MsgHandle | ||
// zinxRole: IServer/IClient | ||
// zinxRole: IServer | ||
func newMsgHandle() *MsgHandle { | ||
var freeWorkers map[uint32]struct{} | ||
var extraFreeWorkers map[uint32]struct{} | ||
|
@@ -84,17 +84,70 @@ 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, TaskQueueLen), | ||
freeWorkers: freeWorkers, | ||
builder: newChainBuilder(), | ||
// 可额外临时分配的workerID集合 | ||
extraFreeWorkers: extraFreeWorkers, | ||
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, 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做数据分发) | ||
handle.builder.Tail(handle) | ||
return handle | ||
} | ||
|
||
// newCliMsgHandle creates 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 | ||
// 为每个链接分配一个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{}{} | ||
} | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
} | ||
|
||
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 | ||
TaskQueueLen = 0 | ||
// One worker corresponds to one queue (一个worker对应一个queue) | ||
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做数据分发) | ||
handle.builder.Tail(handle) | ||
|
@@ -202,7 +255,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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [golangci] reported by reviewdog 🐶
ineffectual assignment to TaskQueueLen (ineffassign)