Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 536e9a6

Browse files
committed
race condition bugfix
1 parent f8e79bf commit 536e9a6

File tree

1 file changed

+7
-1
lines changed
  • websites/code2/studygolang/src/logic

1 file changed

+7
-1
lines changed

websites/code2/studygolang/src/logic/book.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func (this *UserData) MessageQueue(serverId int) chan *Message {
5858
return this.serverMsgQueue[serverId]
5959
}
6060

61+
func (this *UserData) InitMessageQueue(serverId int) {
62+
this.rwMutex.Lock()
63+
defer this.rwMutex.Unlock()
64+
this.serverMsgQueue[serverId] = make(chan *Message, 1)
65+
}
66+
6167
var Book = &book{users: make(map[int]*UserData)}
6268

6369
type book struct {
@@ -73,7 +79,7 @@ func (this *book) AddUser(user, serverId int) *UserData {
7379
this.rwMutex.Lock()
7480
defer this.rwMutex.Unlock()
7581
if userData, ok = this.users[user]; ok {
76-
userData.serverMsgQueue[serverId] = make(chan *Message, 1)
82+
userData.InitMessageQueue(serverId)
7783
userData.onlineDuartion += time.Now().Sub(userData.lastAccessTime)
7884
userData.lastAccessTime = time.Now()
7985
} else {

0 commit comments

Comments
 (0)