自学内容网 自学内容网

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

最后

我是醉墨居士,我们这篇博客完成了任务执行流路由模块的代码实现


原文地址:https://blog.csdn.net/qq_67733273/article/details/142691981

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!