Skip to content

Commit 3f7dda1

Browse files
committed
Server: minor fixes to previous commits
1 parent 96de86f commit 3f7dda1

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

server/internal/keyRWMutex.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,43 @@ func NewKeyRWMutex() *KeyRWMutex {
1414
}
1515

1616
func (kl *KeyRWMutex) Lock(key interface{}) {
17-
ok, lock := kl.lock(key)
17+
ok, lock := kl.rlock(key)
1818
if !ok {
1919
lock = &sync.RWMutex{}
20-
kl.mutexes[key] = lock
20+
func() {
21+
kl.mu.Lock()
22+
defer kl.mu.Unlock()
23+
kl.mutexes[key] = lock
24+
}()
2125
}
2226
lock.Lock()
2327
}
2428

25-
func (kl *KeyRWMutex) lock(key interface{}) (ok bool, lock *sync.RWMutex) {
29+
func (kl *KeyRWMutex) rlock(key interface{}) (ok bool, lock *sync.RWMutex) {
2630
kl.mu.RLock()
2731
defer kl.mu.RUnlock()
2832
lock, ok = kl.mutexes[key]
2933
return
3034
}
3135

3236
func (kl *KeyRWMutex) RLock(key interface{}) {
33-
ok, lock := kl.lock(key)
37+
ok, lock := kl.rlock(key)
3438

3539
if ok {
3640
lock.RLock()
3741
}
3842
}
3943

4044
func (kl *KeyRWMutex) Unlock(key interface{}) {
41-
ok, lock := kl.lock(key)
45+
ok, lock := kl.rlock(key)
4246

4347
if ok {
4448
lock.Unlock()
4549
}
4650
}
4751

4852
func (kl *KeyRWMutex) RUnlock(key interface{}) {
49-
ok, lock := kl.lock(key)
53+
ok, lock := kl.rlock(key)
5054

5155
if ok {
5256
lock.RUnlock()

server/internal/models/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ func (sess *Session) AddMessage(message internal.A) {
6161
sess.messagesLock.RLock()
6262
defer sess.messagesLock.RUnlock()
6363
if sess.messagesIndex == uint8(len(sess.messages)) {
64-
time.Sleep(time.Millisecond * 100)
6564
wait = true
6665
}
6766
}()
6867
if wait {
68+
time.Sleep(time.Millisecond * 100)
6969
continue
7070
}
7171
func() {

0 commit comments

Comments
 (0)