Go语言实现长连接并发框架 - 请求分发器

文章目录

前言

你好,我是醉墨居士,我们上篇博客实现了任务管理器的功能,接下来这篇博客我们将要实现请求分发模块的开发

接口

trait/dispatcher.go

go 复制代码
type Dispatcher interface {
	Start()
	Dispatch(connQueue chan Connection)
	BatchDispatch(conn Connection) error
	SetHeaderDeadline(deadline time.Time)
	SetBodyDeadline(deadline time.Time)
	ChooseQueue(conn Connection) chan <- Connection
	Commit(conn Connection)
}

结构体

gcore/dispatcher.go

go 复制代码
// Dispatcher 请求分发模块,负责读取客户端连接的数据,并对数据进行拆包转换成消息格式,然后分发给下游的任务处理模块对消息进行业务处理
type Dispatcher struct {
	headerDeadline time.Time
	bodyDeadline time.Time

	connQueue []chan trait.Connection
	taskMgr trait.TaskMgr
}

// NewDispatcher 创建一个请求分发器
func NewDispatcher(taskMgr trait.TaskMgr) *Dispatcher {
	connQueue := make([]chan trait.Connection, global.Config.DispatcherQueues)
	for i := 0; i < len(connQueue); i++ {
		connQueue[i] = make(chan trait.Connection, global.Config.DispatcherQueueLen)
	}

	return &Dispatcher{
		connQueue: connQueue,
		taskMgr: taskMgr,
	}
}

接口实现

gcore/dispatcher.go

go 复制代码
// Start 启动请求分发模块
func (d *Dispatcher) Start() {
	for i := 0; i < len(d.connQueue); i++ {
		for j := 0; j < global.Config.DispatcherQueueLen; j++ {
			go d.Dispatch(d.connQueue[i])
		}
	}
}

// StartDispatcher 分发连接数据
func (d *Dispatcher) Dispatch(connQueue chan trait.Connection) {
	// 从conn中读取数据,并将数据提交给taskMgr处理
	for conn := range connQueue {
		d.BatchDispatch(conn)
	}
}

// BatchDispatch 批量读取连接中的数据,并封装成请求,然后分发请求
func (d *Dispatcher) BatchDispatch(conn trait.Connection) error {
	for time.Now().After(d.headerDeadline) {
		header := make([]byte, 4)

		// 设置header读取超时时间
		conn.SetReadDeadline(d.headerDeadline)

		_, err := io.ReadFull(conn, header)
		if err != nil {
			if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
				// 数据包读取超时
				return nil
			}

			return err
		}

		// 设置body读取超时时间
		conn.SetReadDeadline(d.bodyDeadline)

		// 读取长度
		dataLen := binary.BigEndian.Uint16(header[2:4])
		// 读取数据
		body := make([]byte, dataLen)	
		_, err = io.ReadFull(conn, body)
		if err != nil {
			return err
		}

		msg := gpack.Unpack(header, body)
		// 提交消息,处理数据
		
		request := NewRequest(conn, msg)

		d.taskMgr.Submit(request)
	}

	return nil
}

// SetHeaderDeadline 设置header读取超时时间
func (d *Dispatcher) SetHeaderDeadline(deadline time.Time) {
	d.headerDeadline = deadline
}

// SetBodyDeadline 设置body读取超时时间
func (d *Dispatcher) SetBodyDeadline(deadline time.Time) {
	d.bodyDeadline = deadline
}

// ChooseQueue 选择处理连接的队列
func (d *Dispatcher) ChooseQueue(conn trait.Connection) chan <- trait.Connection {
	// 负载均衡,选择队列
	return d.connQueue[conn.ID() % int32(len(d.connQueue))]
}

// Commit 提交连接到队列
func (d *Dispatcher) Commit(conn trait.Connection) {
	d.ChooseQueue(conn) <- conn
}

项目地址

Github:https://github.com/zm50/gte

Giee:https://gitee.com/zm50/gte

最后

我是醉墨居士,我们完成了基本的请求分发器的开发,希望对你有所帮助,也希望你有所收获

相关推荐
知白守黑26712 分钟前
Linux磁盘阵列
linux·运维·服务器
一百天成为python专家25 分钟前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
喪彪27 分钟前
MySQL新手教学
数据库·mysql·adb
Sunhen_Qiletian29 分钟前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
hqwest38 分钟前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
前路不黑暗@1 小时前
C语言:操作符详解(二)
c语言·开发语言·经验分享·笔记·学习·学习方法·visual studio
维尔切1 小时前
Linux中基于Centos7使用lamp架构搭建个人论坛(wordpress)
linux·运维·架构
他日若遂凌云志1 小时前
深入剖析 Fantasy 框架的消息设计与序列化机制:协同架构下的高效转换与场景适配
后端
深盾科技2 小时前
Kotlin Data Classes 快速上手
android·开发语言·kotlin