dongxing6802 2015-09-18 10:06
浏览 220
已采纳

运行Go应用程序的多个实例时的Gorilla Websocket连接

To save Gorilla websocket connections I could something like this where the key could be a userId.

connections := make(map[int]*connection)

I am using something called supervisord

http://supervisord.org/

which is a process control system so that I am able to run the go application in the background as a daemon. It looks like several instances are being spawned up.

enter image description here

Does those instances know how to access the same connections variable if I make it global?

var connections map[int]*connection

Or will there be an issue?

Also since map is not thread safe, should I create a struct and add sync.RWMutex and do RLock()/Lock() and RUnlock()/Unlock() before checking whether a key exists or when removing a key from the map?

  • 写回答

1条回答 默认 最新

  • dtgj8529 2015-09-18 10:10
    关注

    First of all, those aren't processes but threads, so they will indeed share the same global state. htop shows threads as if they were processes.

    Whenever you need concurrent access to a map, you need to synchronise it. You can indeed do this with a mutex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度