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