Skip to content

Commit

Permalink
fix one more potential lock
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Apr 19, 2023
1 parent a338cae commit 7abf0a4
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions subscriber.go
Expand Up @@ -82,31 +82,35 @@ func (s *Subscriber) Dispatch(u *Update, fromHistory bool) bool {
case s.out <- u:
s.outMutex.Unlock()
default:
// Channel is full, disconnect the subscriber
atomic.StoreInt32(&s.disconnected, 1)
s.outMutex.Unlock()
s.handleFullChan()

if c := s.logger.Check(zap.ErrorLevel, "subscriber unable to receive updates fast enough"); c != nil {
c.Write(zap.Object("subscriber", s))
}
return false
}

return true
}

// Ready flips the ready flag to true and flushes queued live updates returning number of events flushed.
func (s *Subscriber) Ready() int {
func (s *Subscriber) Ready() (n int) {
s.liveMutex.Lock()
defer s.liveMutex.Unlock()
s.outMutex.Lock()
defer s.outMutex.Unlock()

n := len(s.liveQueue)
for _, u := range s.liveQueue {
s.out <- u
select {
case s.out <- u:
n++
default:
s.handleFullChan()
s.liveMutex.Unlock()

return n
}
}
atomic.StoreInt32(&s.ready, 1)

s.outMutex.Unlock()
s.liveMutex.Unlock()

return n
}

Expand Down Expand Up @@ -265,3 +269,13 @@ func (s *Subscriber) MarshalLogObject(enc zapcore.ObjectEncoder) error {

return nil
}

// handleFullChan disconnects the subscriber when the out channel is full

Check failure on line 273 in subscriber.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)

Check failure on line 273 in subscriber.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
func (s *Subscriber) handleFullChan() {
atomic.StoreInt32(&s.disconnected, 1)
s.outMutex.Unlock()

if c := s.logger.Check(zap.ErrorLevel, "subscriber unable to receive updates fast enough"); c != nil {
c.Write(zap.Object("subscriber", s))
}
}

0 comments on commit 7abf0a4

Please sign in to comment.