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.

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

报告相同问题?