在golang中存储websocket连接的最佳方式是什么? [英] What's the best way to store websocket connection in golang

查看:889
本文介绍了在golang中存储websocket连接的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用websockets编写了一个网络聊天应用程序。它使用每页一个连接将新消息推送给在线用户。



因此,有很多 websocket.Conn 管理。

  onlineUser = map [int] * websocket.Conn 

我打开1,000,000个网页时非常担心地图。



是一种更好的方式来存储所有 websocket.Conn






Erlang的内部数据库可用于存储erlang套接字。



对于Go,我曾考虑使用encoding / gob将套接字缓存在memcached或redis中。但是,在使用 websocket.Conn 之前,它会被GOB解码,并且会消耗太多的CPU。

解决方案

1,000,000个并发用户是一个很好的问题。 :)

您的主要问题是websocket规范要求连接保持打开状态。这意味着你不能序列化和缓存它们。然而,如果你可以对它们进行GOB编码,并且每个用户每秒发送一条消息(不现实的高IMO)一个每秒百万GOB解码不会占据你的负担。



另外一张地图可以轻松处理一百万条记录。



如果您真的想担心未来扩展,请弄清楚如何让您的应用程序的两个实例在负载上一起工作。然后将其增加到N个实例。


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,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.

这篇关于在golang中存储websocket连接的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆