2015-09-18 10:06

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

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答

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

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dtgj8529 dtgj8529 6年前

    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.

    点赞 评论 复制链接分享