Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何保证数据有序性? #833

Closed
blueskydata opened this issue Dec 25, 2023 · 4 comments
Closed

请问如何保证数据有序性? #833

blueskydata opened this issue Dec 25, 2023 · 4 comments

Comments

@blueskydata
Copy link

在func (s *Server) serveConn(conn net.Conn) {

 获取请求
 if s.pool != nil {
		s.pool.Submit(func() {
			s.processOneRequest(ctx, req, conn)
		})
	} else {
		go s.processOneRequest(ctx, req, conn)
	}

}
而在processOneRequest中,包含了发送相应的过程,即s.sendResponse(ctx, conn, err, req, res),在多协程(可能处于不同的线程环境中)情况下,对同一个socket发送数据,如何保证数据的一致性,比如协程A发送了几个字节,协程B发送了几个字节,然后协程A再发送几个字节。

@smallnest
Copy link
Owner

Go的net.Conn是并发安全的,看官方文档。你保证你发送的数据一次调用发送就行。

@blueskydata
Copy link
Author

非常感谢作者及时耐心解答,祝您万事顺心!

@chaoedu
Copy link

chaoedu commented May 20, 2024

游戏业务中有类似请求保序的场景,需要针对请求中同一个 player_id 的数据做顺序串行处理,相当于一个玩家一个队列,对于这样的场景大佬有什么建议吗,当前框架的 WorkerPool 好像不太好做这样的扩展(没法指定 key)。 @smallnest

@dickens7
Copy link
Contributor

@chaoedu 我加了一个支持传递 req message 的 WorkerPool, 你可以参考一下

#853

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants