Skip to content

Commit

Permalink
git add .
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Sep 23, 2024
1 parent dbb2591 commit f951fe2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions proxy/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (b *Buffer) Write(data []byte) {
}

func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {
b.mu.Lock()

clientID := b.clientNextId
b.clientNextId++

Expand All @@ -53,6 +55,8 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {

maxBufferSize := 100 * 1024 * 1024

b.mu.Unlock()

go func() {
for {
select {
Expand All @@ -68,7 +72,9 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {

return
default:
b.mu.Lock()
pos, ok := b.clientPositions[clientID]

if !ok {
pos = 0
}
Expand All @@ -78,7 +84,7 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {
*b.clients[clientID] <- chunk
b.clientPositions[clientID] += bufferSize

if len(b.data) > maxBufferSize && b.mu.TryLock() {
if len(b.data) > maxBufferSize {
trimSize := len(b.data) - maxBufferSize
b.data = b.data[trimSize:]

Expand All @@ -90,9 +96,10 @@ func (b *Buffer) Subscribe(ctx context.Context) *chan []byte {
}
}

b.mu.Unlock()
}
}

b.mu.Unlock()
}
}
}()
Expand Down

0 comments on commit f951fe2

Please sign in to comment.