fix: panic due to send on closed channel (close #5729)

This commit is contained in:
Andy Hsu 2024-01-05 11:40:44 +08:00
parent 9d5fb7f595
commit 8020d42b10

View File

@ -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()
@ -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
}
@ -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)
}