Skip to content

Commit 0976488

Browse files
committed
remove useless mutex
1 parent 16c1b44 commit 0976488

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

localsubscriber.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ type LocalSubscriber struct {
1515

1616
disconnected atomic.Uint32
1717
out chan *Update
18-
outMutex sync.Mutex
18+
mutex sync.Mutex
1919
responseLastEventID chan string
2020
ready atomic.Uint32
2121
liveQueue []*Update
22-
liveMutex sync.RWMutex
2322
}
2423

2524
const outBufferLength = 1000
@@ -44,45 +43,39 @@ func NewLocalSubscriber(lastEventID string, logger Logger, topicSelectorStore *T
4443
// Security checks must (topics matching) be done before calling Dispatch,
4544
// for instance by calling Match.
4645
func (s *LocalSubscriber) Dispatch(u *Update, fromHistory bool) bool {
47-
s.outMutex.Lock()
46+
s.mutex.Lock()
4847

4948
if s.disconnected.Load() > 0 {
50-
s.outMutex.Unlock()
49+
s.mutex.Unlock()
5150

5251
return false
5352
}
5453

5554
if !fromHistory && s.ready.Load() < 1 {
56-
s.liveMutex.Lock()
5755
s.liveQueue = append(s.liveQueue, u)
58-
s.liveMutex.Unlock()
59-
60-
s.outMutex.Unlock()
56+
s.mutex.Unlock()
6157

6258
return true
6359
}
6460

6561
select {
6662
case s.out <- u:
67-
s.outMutex.Unlock()
63+
s.mutex.Unlock()
64+
65+
return true
6866
default:
6967
s.handleFullChan()
7068

7169
return false
7270
}
73-
74-
return true
7571
}
7672

7773
// Ready flips the ready flag to true and flushes queued live updates returning number of events flushed.
7874
func (s *LocalSubscriber) Ready() (n int) {
79-
s.outMutex.Lock()
80-
81-
s.liveMutex.RLock()
82-
defer s.liveMutex.RUnlock()
75+
s.mutex.Lock()
8376

8477
if s.disconnected.Load() > 0 || s.ready.Load() > 0 {
85-
s.outMutex.Unlock()
78+
s.mutex.Unlock()
8679

8780
return 0
8881
}
@@ -103,7 +96,7 @@ func (s *LocalSubscriber) Ready() (n int) {
10396
s.ready.Store(1)
10497
s.liveQueue = nil
10598

106-
s.outMutex.Unlock()
99+
s.mutex.Unlock()
107100

108101
return n
109102
}
@@ -120,8 +113,8 @@ func (s *LocalSubscriber) HistoryDispatched(responseLastEventID string) {
120113

121114
// Disconnect disconnects the subscriber.
122115
func (s *LocalSubscriber) Disconnect() {
123-
s.outMutex.Lock()
124-
defer s.outMutex.Unlock()
116+
s.mutex.Lock()
117+
defer s.mutex.Unlock()
125118

126119
if s.disconnected.Load() > 0 {
127120
return // already disconnected
@@ -133,7 +126,7 @@ func (s *LocalSubscriber) Disconnect() {
133126

134127
// handleFullChan disconnects the subscriber when the out channel is full.
135128
func (s *LocalSubscriber) handleFullChan() {
136-
defer s.outMutex.Unlock()
129+
defer s.mutex.Unlock()
137130
if s.disconnected.Load() > 0 {
138131
return // already disconnected
139132
}

0 commit comments

Comments
 (0)