|
@ -3,19 +3,38 @@ package znet |
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"strconv" |
|
|
"strconv" |
|
|
|
|
|
"zinx/utils" |
|
|
"zinx/ziface" |
|
|
"zinx/ziface" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type MsgHandle struct { |
|
|
type MsgHandle struct { |
|
|
Apis map[uint32]ziface.IRouter //存放每个MsgId 所对应的处理方法的map属性
|
|
|
Apis map[uint32]ziface.IRouter //存放每个MsgId 所对应的处理方法的map属性
|
|
|
|
|
|
WorkerPoolSize uint32 //业务工作Worker池的数量
|
|
|
|
|
|
TaskQueue []chan ziface.IRequest //Worker负责取任务的消息队列
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewMsgHandle() *MsgHandle { |
|
|
func NewMsgHandle() *MsgHandle { |
|
|
return &MsgHandle{ |
|
|
return &MsgHandle{ |
|
|
Apis: make(map[uint32]ziface.IRouter), |
|
|
Apis: make(map[uint32]ziface.IRouter), |
|
|
|
|
|
WorkerPoolSize:utils.GlobalObject.WorkerPoolSize, |
|
|
|
|
|
//一个worker对应一个queue
|
|
|
|
|
|
TaskQueue:make([]chan ziface.IRequest, utils.GlobalObject.WorkerPoolSize), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将消息交给TaskQueue,由worker进行处理
|
|
|
|
|
|
func (mh *MsgHandle)SendMsgToTaskQueue(request ziface.IRequest) { |
|
|
|
|
|
//根据ConnID来分配当前的连接应该由哪个worker负责处理
|
|
|
|
|
|
//轮询的平均分配法则
|
|
|
|
|
|
|
|
|
|
|
|
//得到需要处理此条连接的workerID
|
|
|
|
|
|
workerID := request.GetConnection().GetConnID() % mh.WorkerPoolSize |
|
|
|
|
|
fmt.Println("Add ConnID=", request.GetConnection().GetConnID()," request msgID=", request.GetMsgID(), "to workerID=", workerID) |
|
|
|
|
|
//将请求消息发送给任务队列
|
|
|
|
|
|
mh.TaskQueue[workerID] <- request |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//马上以非阻塞方式处理消息
|
|
|
//马上以非阻塞方式处理消息
|
|
|
func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest) { |
|
|
func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest) { |
|
|
handler, ok := mh.Apis[request.GetMsgID()] |
|
|
handler, ok := mh.Apis[request.GetMsgID()] |
|
@ -29,6 +48,7 @@ func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest) { |
|
|
handler.Handle(request) |
|
|
handler.Handle(request) |
|
|
handler.PostHandle(request) |
|
|
handler.PostHandle(request) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//为消息添加具体的处理逻辑
|
|
|
//为消息添加具体的处理逻辑
|
|
|
func (mh *MsgHandle) AddRouter(msgId uint32, router ziface.IRouter) { |
|
|
func (mh *MsgHandle) AddRouter(msgId uint32, router ziface.IRouter) { |
|
|
//1 判断当前msg绑定的API处理方法是否已经存在
|
|
|
//1 判断当前msg绑定的API处理方法是否已经存在
|
|
@ -40,5 +60,27 @@ func (mh *MsgHandle) AddRouter(msgId uint32, router ziface.IRouter) { |
|
|
fmt.Println("Add api msgId = ", msgId) |
|
|
fmt.Println("Add api msgId = ", msgId) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//启动一个Worker工作流程
|
|
|
|
|
|
func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan ziface.IRequest) { |
|
|
|
|
|
fmt.Println("Worker ID = ", workerID, " is started.") |
|
|
|
|
|
//不断的等待队列中的消息
|
|
|
|
|
|
for { |
|
|
|
|
|
select { |
|
|
|
|
|
//有消息则取出队列的Request,并执行绑定的业务方法
|
|
|
|
|
|
case request := <-taskQueue: |
|
|
|
|
|
mh.DoMsgHandler(request) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//启动worker工作池
|
|
|
|
|
|
func (mh *MsgHandle) StartWorkerPool() { |
|
|
|
|
|
//遍历需要启动worker的数量,依此启动
|
|
|
|
|
|
for i:= 0; i < int(mh.WorkerPoolSize); i++ { |
|
|
|
|
|
//一个worker被启动
|
|
|
|
|
|
//给当前worker对应的任务队列开辟空间
|
|
|
|
|
|
mh.TaskQueue[i] = make(chan ziface.IRequest, utils.GlobalObject.MaxWorkerTaskLen) |
|
|
|
|
|
//启动当前Worker,阻塞的等待对应的任务队列是否有消息传递进来
|
|
|
|
|
|
go mh.StartOneWorker(i, mh.TaskQueue[i]) |
|
|
|
|
|
} |
|
|
|
|
|
} |