英文:
What's the best way to store websocket connection in golang
问题
我写了一个使用Websockets的Web聊天应用程序。它使用每个页面一个连接来向在线用户推送新消息。
所以,有很多websocket.Conn
需要管理。我目前正在使用一个map。
onlineUser = map[int]*websocket.Conn
当有1,000,000个页面打开时,我非常担心这个map。
有没有更好的方法来存储所有的websocket.Conn
?
可以使用Erlang的内部数据库来存储Erlang套接字。
对于Go语言,我考虑使用"encoding/gob"将套接字缓存到memcached或redis中。但是在使用websocket.Conn
之前,需要对其进行GOB解码,这将消耗太多的CPU。
英文:
I wrote a web chat app, using websockets. It uses one connection per page to push new messages to online users.
So, there are a lot of websocket.Conn
to manage. I'm currently using a map.
onlineUser = map[int] *websocket.Conn
I am very worried about the map when 1,000,000 pages are open.
There is a better way to store all the websocket.Conn
?
Erlang's inner database can be used to store an erlang socket.
For Go, I had considered using "encoding/gob" to cache the socket in memcached or redis. But then before using the websocket.Conn
it but be GOB decoded and that will consume too much CPU.
答案1
得分: 3
1,000,000并发用户是一个很好的问题。
你的主要问题是websocket规范要求连接保持打开状态。这意味着你不能对其进行序列化和缓存。
然而,如果你可以对它们进行GOB编码,并且每个用户每秒发送一条消息(在我看来是不现实的高频率),每秒解码一百万个GOB并不会对你的负载产生主要影响。
另外,一个map可以轻松处理一百万个条目。
如果你真的担心未来的扩展性,可以考虑如何让两个应用实例共同处理负载。然后将实例数量增加到N个。
英文:
1,000,000 concurrent users is a nice problem to have.
Your primary problem is that the websocket spec requires the connection to be maintained open. This means that you can't serialize and cache them.
However, if you could GOB encode them, and every user sent one message a second (unrealistically high IMO) a million GOB decodes per second isn't going to dominate your load.
Also a map can easily handle a million entries.
If you really want to worry about future scaling, figure out how to get two instances of your application to work together on the load. Then increase that to N instances.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论