Go语言实现长连接并发框架 - 任务执行流路由模块
文章目录
- 前言
- 接口
- 结构体
- 接口实现
- 项目地址
- 最后
前言
你好,我是醉墨居士,上篇博客中我们实现了任务执行流上下文部分,接下来我们实现一下任务执行流的路由模块,基于该模块可以实现将消息转发到相应注册的任务执行流中进行处理
接口
trait/router.go
type Router interface {Regist(id uint16, flow ...TaskFunc)RegistFlow(id uint16, flow TaskFlow)TaskFlow(id uint16) TaskFlow
}
结构体
gcore/router.go
// Router 任务执行流路由器
type Router struct {apis map[uint16]trait.TaskFlow
}// NewRouter 创建一个新的任务流路由器
func NewRouter() trait.Router {return &Router{apis: make(map[uint16]trait.TaskFlow),}
}
接口实现
gcore/router.go
// Regist 注册任务执行逻辑
func (r *Router) Regist(id uint16, flow ...trait.TaskFunc) {if _, ok := r.apis[id]; ok {r.apis[id].Extend(flow...)} else {r.apis[id] = NewTaskFlow(flow...)}
}// RegistFlow 注册一个任务执行执行流
func (r *Router) RegistFlow(id uint16, flow trait.TaskFlow) {r.apis[id] = flow
}// TaskFlow 根据消息ID获取任务执行流
func (r *Router) TaskFlow(id uint16) trait.TaskFlow {return r.apis[id]
}
项目地址
Github:https://github.com/zm50/gte
Giee:https://gitee.com/zm50/gte
最后
我是醉墨居士,我们这篇博客完成了任务执行流路由模块的代码实现