维护一个开放的 WebSocket 连接列表的最佳方法是什么?

huangapple go评论92阅读模式
英文:

What's the best way to maintain a list of open websocket connections?

问题

我想要通过 WebSocket 向连接到我服务器的每个客户端发送一系列消息(在 Go 中称为流或通道)。大约每 100 毫秒就会有一条新消息要发送。在同时需要对连接进行打开和关闭的情况下,如何维护一个打开连接的列表,并且需要对该列表进行迭代以向超过 50,000 个连接发送消息?目前我是将连接存储在一个与单个互斥锁同步的映射中,但我不确定这种方法是否可扩展。

英文:

I want to send a stream (channel in Go) of messages to every client that connects to me through a websocket. There's a new message to send every ~100ms. How do I maintain a list of open connections with connections being opened and closed all the time while having to do some sort of iteration over that list to send out the messages for >50,000 connections? Right now I'm storing connections in a map that synchronizes with a single mutex, but I'm not sure if that can scale.

答案1

得分: 1

最好的方法是根本不维护一个列表。

每个连接应该连接到一个带有给定标识符(例如用户ID、连接ID等)的中心,订阅该标识符的消息并发布带有该标识符的消息。如果需要,还可以订阅全局消息和组消息。

更新:

要扩展到50K+个连接,你需要采用更分布式的方法,而不是简单的带有互斥锁的映射。例如:https://github.com/bitly/nsq(用GO编写)"NSQ是一个实时分布式消息平台,旨在处理每天数十亿条消息。"

英文:

The best way is not maintaining a list at all.

Each connection should connect to a hub with a given identifier (eg:user id, connection id, etc...), subscribe for messages for such identifier and publish messages with that identifier. Also subscribe for global messages and group messages if you want.

UPDATE:

To scale to 50K+ connections you are going to need a more distributed approach rather than a simple map with a mutex. For example: https://github.com/bitly/nsq (made in GO) "NSQ is a realtime distributed messaging platform designed to operate at scale, handling billions of messages per day."

答案2

得分: 1

这个问题可以更一般地陈述:你有一个值的集合。你需要在集合上进行迭代,以高并发地添加和删除集合中的元素。

我会从你提出的方案开始:使用一个由互斥锁保护的映射(map)。

如果你发现互斥锁上存在很高的争用,那么可以考虑使用锁分段(lock striping)。这就是使用N个互斥锁和映射(map)对,其中选择的对是由键的哈希值确定的。

英文:

This problem can be stated more generally: You have a collection of values. You need to iterate over the collection, add to the collection and remove from the collection with high concurrency.

I would start with what you propose: use a map protected by a mutex.

If you find that there's high contention on the mutex, then consider lock striping. This is where you use N mutex and map pairs where the pair is selected by a hash of the key.

huangapple
  • 本文由 发表于 2014年12月9日 09:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/27370217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定