Open Owen-Zhang opened 1 year ago
你提出的新想法中: type API struct { clients map[uint][]client //我个人感觉这个还可以优化一些 lock sync.RWMutex pingPeriod time.Duration pongTimeout time.Duration upgrader websocket.Upgrader } //个人想法 type API struct { clients map[uint]userInfo lock sync.RWMutex pingPeriod time.Duration pongTimeout time.Duration upgrader websocket.Upgrader }
type userInfo struct { userId uint clientLock sync.RWMutex allClients []*client //还可以保留一些其它信息 }
//这样的好处是 外层的锁只会做很少的事 //内层加锁是不影响到其他用户的,而且slice是有datarace问题的,不加锁会有问题
你提出的新想法中: type API struct { clients map[uint][]client //我个人感觉这个还可以优化一些 lock sync.RWMutex pingPeriod time.Duration pongTimeout time.Duration upgrader websocket.Upgrader } //个人想法 type API struct { clients map[uint]userInfo lock sync.RWMutex pingPeriod time.Duration pongTimeout time.Duration upgrader websocket.Upgrader }
type userInfo struct { userId uint clientLock sync.RWMutex allClients []*client //还可以保留一些其它信息 }
//这样的好处是 外层的锁只会做很少的事 //内层加锁是不影响到其他用户的,而且slice是有datarace问题的,不加锁会有问题