MCPcopy
hub / github.com/aceld/zinx / newCliMsgHandle

Function newCliMsgHandle

znet/msghandler.go:108–158  ·  view source on GitHub ↗

newCliMsgHandle creates MsgHandle zinxRole: IClient

()

Source from the content-addressed store, hash-verified

106// newCliMsgHandle creates MsgHandle
107// zinxRole: IClient
108func newCliMsgHandle() *MsgHandle {
109 var freeWorkers map[uint32]struct{}
110 var extraFreeWorkers map[uint32]struct{}
111
112 if zconf.GlobalObject.WorkerMode == zconf.WorkerModeBind {
113 // Assign a workder to each link, avoid interactions when multiple links are processed by the same worker
114 // MaxWorkerTaskLen can also be reduced, for example, 50
115 // 为每个连接分配一个workder,避免同一worker处理多个连接时的互相影响
116 // 同时可以减小MaxWorkerTaskLen,比如50,因为每个worker的负担减轻了
117 zconf.GlobalObject.WorkerPoolSize = uint32(zconf.GlobalObject.MaxConn)
118 freeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.WorkerPoolSize)
119 for i := uint32(0); i < zconf.GlobalObject.WorkerPoolSize; i++ {
120 freeWorkers[i] = struct{}{}
121 }
122 }
123
124 TaskQueueLen := zconf.GlobalObject.WorkerPoolSize
125
126 if zconf.GlobalObject.WorkerMode == zconf.WorkerModeDynamicBind {
127 zlog.Ins().DebugF("WorkerMode = %s", zconf.WorkerModeDynamicBind)
128 freeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.WorkerPoolSize)
129 for i := uint32(0); i < zconf.GlobalObject.WorkerPoolSize; i++ {
130 freeWorkers[i] = struct{}{}
131 }
132
133 extraFreeWorkers = make(map[uint32]struct{}, zconf.GlobalObject.MaxConn-int(zconf.GlobalObject.WorkerPoolSize))
134 for i := zconf.GlobalObject.WorkerPoolSize; i < uint32(zconf.GlobalObject.MaxConn); i++ {
135 extraFreeWorkers[i] = struct{}{}
136 }
137 TaskQueueLen = uint32(zconf.GlobalObject.MaxConn)
138 }
139
140 handle := &MsgHandle{
141 Apis: make(map[uint32]ziface.IRouter),
142 RouterSlices: NewRouterSlices(),
143 freeWorkers: freeWorkers,
144 builder: newChainBuilder(),
145 // 可额外临时分配的workerID集合
146 extraFreeWorkers: extraFreeWorkers,
147 }
148
149 // client: Set worker pool size to 0 to turn off the worker pool in the client (客户端将协程池关闭)
150 handle.WorkerPoolSize = 0
151 // One worker corresponds to one queue (一个worker对应一个queue)
152 handle.TaskQueue = make([]chan ziface.IRequest, TaskQueueLen)
153
154 // 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
155 // (此处必须把 msghandler 添加到责任链中,并且是责任链最后一环,在msghandler中进行解码后由router做数据分发)
156 handle.builder.Tail(handle)
157 return handle
158}
159
160// Use worker ID
161// 占用workerID

Callers 2

NewClientFunction · 0.85
NewWsClientFunction · 0.85

Calls 5

InsFunction · 0.92
NewRouterSlicesFunction · 0.85
newChainBuilderFunction · 0.85
TailMethod · 0.80
DebugFMethod · 0.65

Tested by

no test coverage detected