Skip to content

Commit

Permalink
fix: panic due to send on closed channel (close #5729)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jan 5, 2024
1 parent 9d5fb7f commit 8020d42
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/net/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,19 @@ type Buf struct {
ctx context.Context
off int
rw sync.RWMutex
notify chan struct{}
//notify chan struct{}
}

// NewBuf is a buffer that can have 1 read & 1 write at the same time.
// when read is faster write, immediately feed data to read after written
func NewBuf(ctx context.Context, maxSize int, id int) *Buf {
d := make([]byte, 0, maxSize)
return &Buf{ctx: ctx, buffer: bytes.NewBuffer(d), size: maxSize, notify: make(chan struct{})}

return &Buf{
ctx: ctx,
buffer: bytes.NewBuffer(d),
size: maxSize,
//notify: make(chan struct{}),
}
}
func (br *Buf) Reset(size int) {
br.buffer.Reset()
Expand Down Expand Up @@ -495,8 +499,8 @@ func (br *Buf) Read(p []byte) (n int, err error) {
select {
case <-br.ctx.Done():
return 0, br.ctx.Err()
case <-br.notify:
return 0, nil
//case <-br.notify:
// return 0, nil
case <-time.After(time.Millisecond * 200):
return 0, nil
}
Expand All @@ -510,12 +514,12 @@ func (br *Buf) Write(p []byte) (n int, err error) {
defer br.rw.Unlock()
n, err = br.buffer.Write(p)
select {
case br.notify <- struct{}{}:
//case br.notify <- struct{}{}:
default:
}
return
}

func (br *Buf) Close() {
close(br.notify)
//close(br.notify)
}

0 comments on commit 8020d42

Please sign in to comment.