fix ws_hub.go getUsers() crash bug

This commit is contained in:
Azareal 2021-05-05 16:24:09 +10:00
parent 99ac23dfd3
commit 3da67241e6
1 changed files with 6 additions and 4 deletions

View File

@ -362,19 +362,21 @@ func (h *WsHubImpl) getUser(uid int) (wsUser *WSUser, err error) {
} }
// Warning: For efficiency, some of the *WSUsers may be nil pointers, DO NOT EXPORT // Warning: For efficiency, some of the *WSUsers may be nil pointers, DO NOT EXPORT
// TODO: Write tests for this
func (h *WsHubImpl) getUsers(uids []int) (wsUsers []*WSUser, err error) { func (h *WsHubImpl) getUsers(uids []int) (wsUsers []*WSUser, err error) {
if len(uids) == 0 { if len(uids) == 0 {
return nil, errWsNouser return nil, errWsNouser
} }
wsUsers = make([]*WSUser, len(uids)) //wsUsers = make([]*WSUser, len(uids))
i := 0 //i := 0
appender := func(l *sync.RWMutex, users map[int]*WSUser) { appender := func(l *sync.RWMutex, users map[int]*WSUser) {
l.RLock() l.RLock()
defer l.RUnlock() defer l.RUnlock()
// We don't want to keep a lock on this for too long, so we'll accept some nil pointers // We don't want to keep a lock on this for too long, so we'll accept some nil pointers
for _, uid := range uids { for _, uid := range uids {
wsUsers[i] = users[uid] //wsUsers[i] = users[uid]
i++ wsUsers = append(wsUsers, users[uid])
//i++
} }
} }
appender(&h.evenUserLock, h.evenOnlineUsers) appender(&h.evenUserLock, h.evenOnlineUsers)