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

Function useWorker

znet/msghandler.go:162–217  ·  view source on GitHub ↗

Use worker ID 占用workerID

(conn ziface.IConnection)

Source from the content-addressed store, hash-verified

160// Use worker ID
161// 占用workerID
162func useWorker(conn ziface.IConnection) uint32 {
163 var workerId uint32
164
165 mh, _ := conn.GetMsgHandler().(*MsgHandle)
166 if mh == nil {
167 zlog.Ins().ErrorF("useWorker failed, mh is nil")
168 return 0
169 }
170
171 if zconf.GlobalObject.WorkerMode == zconf.WorkerModeBind {
172 mh.freeWorkerMu.Lock()
173 defer mh.freeWorkerMu.Unlock()
174
175 for k := range mh.freeWorkers {
176 delete(mh.freeWorkers, k)
177 return k
178 }
179 }
180
181 if zconf.GlobalObject.WorkerMode == zconf.WorkerModeDynamicBind {
182 mh.freeWorkerMu.Lock()
183 // try to get workerID from workerPool first
184 // 首先尝试从工作线程池里获取一个空闲的workerID
185 for workerID := range mh.freeWorkers {
186 delete(mh.freeWorkers, workerID)
187 mh.freeWorkerMu.Unlock()
188 return workerID
189 }
190 mh.freeWorkerMu.Unlock()
191
192 // 工作池的worker用完了,临时从extraFreeWorkers取一个额外的workerID, 并相应启动一个临时的worker
193 mh.extraFreeWorkerMu.Lock()
194 defer mh.extraFreeWorkerMu.Unlock()
195 for workerID := range mh.extraFreeWorkers {
196 zlog.Ins().DebugF("start extra worker, workerID=%d", workerID)
197 mh.TaskQueue[workerID] = make(chan ziface.IRequest, zconf.GlobalObject.MaxWorkerTaskLen)
198 go mh.StartOneWorker(int(workerID), mh.TaskQueue[workerID])
199 return workerID
200 }
201 }
202
203 //Compatible with the situation where the client has no worker, and solve the situation divide 0
204 //(兼容client没有worker情况,解决除0的情况)
205 if mh.WorkerPoolSize == 0 {
206 workerId = 0
207 } else {
208 // Assign the worker responsible for processing the current connection based on the ConnID
209 // Using a round-robin average allocation rule to get the workerID that needs to process this connection
210 // (根据ConnID来分配当前的连接应该由哪个worker负责处理
211 // 轮询的平均分配法则
212 // 得到需要处理此条连接的workerID)
213 workerId = uint32(conn.GetConnID() % uint64(mh.WorkerPoolSize))
214 }
215
216 return workerId
217}
218
219// Free worker ID

Callers 3

StartMethod · 0.85
StartMethod · 0.85
StartMethod · 0.85

Calls 6

InsFunction · 0.92
StartOneWorkerMethod · 0.80
GetMsgHandlerMethod · 0.65
ErrorFMethod · 0.65
DebugFMethod · 0.65
GetConnIDMethod · 0.65

Tested by

no test coverage detected