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

Skip to content

Commit a68f96b

Browse files
committed
尽早释放锁
1 parent c10d212 commit a68f96b

File tree

1 file changed

+6
-6
lines changed
  • websites/code2/studygolang/src/logic

1 file changed

+6
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ func (this *UserData) MessageQueue(serverId int) chan *Message {
5959
}
6060

6161
func (this *UserData) Remove(serverId int) {
62-
this.rwMutex.RLock()
63-
defer this.rwMutex.RUnlock()
62+
this.rwMutex.Lock()
63+
defer this.rwMutex.Unlock()
6464
delete(this.serverMsgQueue, serverId)
6565
}
6666

6767
func (this *UserData) InitMessageQueue(serverId int) {
6868
this.rwMutex.Lock()
6969
defer this.rwMutex.Unlock()
70-
this.serverMsgQueue[serverId] = make(chan *Message, 1)
70+
this.serverMsgQueue[serverId] = make(chan *Message, 4)
7171
}
7272

7373
func (this *UserData) SendMessage(message *Message) {
74-
this.rwMutex.Lock()
75-
defer this.rwMutex.Unlock()
74+
this.rwMutex.RLock()
75+
defer this.rwMutex.RUnlock()
7676

7777
for _, messageQueue := range this.serverMsgQueue {
7878
messageQueue <- message
@@ -100,7 +100,7 @@ func (this *book) AddUser(user, serverId int) *UserData {
100100
userData.lastAccessTime = time.Now()
101101
} else {
102102
userData = &UserData{
103-
serverMsgQueue: map[int]chan *Message{serverId: make(chan *Message, 1)},
103+
serverMsgQueue: map[int]chan *Message{serverId: make(chan *Message, 4)},
104104
lastAccessTime: time.Now(),
105105
}
106106
this.users[user] = userData

0 commit comments

Comments
 (0)