Skip to content

Commit

Permalink
mutex lock on clear
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Sep 23, 2024
1 parent f951fe2 commit fd7f087
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions proxy/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {
for {
select {
case <-ctx.Done():
b.mu.Lock()
if ch, exists := b.clients[clientID]; exists {
close(*ch) // close the channel when unsubscribing
delete(b.clients, clientID)
delete(b.clientPositions, clientID)
}
if len(b.clients) == 0 {
b.Clear()
b.data = nil // Reset the buffer to empty
b.data = []byte{}
}

b.mu.Unlock()

return
default:
b.mu.Lock()
Expand Down Expand Up @@ -106,11 +110,3 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {

return &ch
}

func (b *Buffer) Clear() {
b.mu.Lock()
defer b.mu.Unlock()

b.data = nil // Reset the buffer to empty
b.data = []byte{}
}

0 comments on commit fd7f087

Please sign in to comment.