mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
ringbuffer: consume buffer before closing
This commit is contained in:
@@ -54,13 +54,12 @@ func (r *RingBuffer) Push(data interface{}) {
|
||||
// Pull pulls some data from the beginning of the buffer.
|
||||
func (r *RingBuffer) Pull() (interface{}, bool) {
|
||||
for {
|
||||
if atomic.SwapInt64(&r.closed, 0) == 1 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
i := r.readIndex % r.bufferSize
|
||||
res := (*interface{})(atomic.SwapPointer(&r.buffer[i], nil))
|
||||
if res == nil {
|
||||
if atomic.SwapInt64(&r.closed, 0) == 1 {
|
||||
return nil, false
|
||||
}
|
||||
r.event.wait()
|
||||
continue
|
||||
}
|
||||
|
@@ -46,11 +46,15 @@ func TestClose(t *testing.T) {
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
|
||||
_, ok := r.Pull()
|
||||
require.Equal(t, true, ok)
|
||||
|
||||
_, ok = r.Pull()
|
||||
require.Equal(t, false, ok)
|
||||
}()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
r.Push([]byte{0x01, 0x02, 0x03, 0x04})
|
||||
|
||||
r.Close()
|
||||
<-done
|
||||
|
Reference in New Issue
Block a user