Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type RingBuffer struct {
overwrite bool // when true, overwrite old data when buffer is full
rTimeout time.Duration // Applies to writes (waits for the read condition)
wTimeout time.Duration // Applies to read (wait for the write condition)
noClOnTo bool // do not set buffer in error on a timeout
mu sync.Mutex
wg sync.WaitGroup
readCond *sync.Cond // Signaled when data has been read.
Expand Down Expand Up @@ -151,6 +152,18 @@ func (r *RingBuffer) WithWriteTimeout(d time.Duration) *RingBuffer {
return r
}

// WithNoCloseOnTimeout will set the ringbuffer to not close on timeout.
// By default, the ringbuffer will close on timeout and return context.DeadlineExceeded.
// When this option is set, the ringbuffer will not close on timeout (but still return
// context.DeadlineExceeded) and will continue to operate.
// This is useful when you want to handle timeouts without closing the ringbuffer.
func (r *RingBuffer) WithNoCloseOnTimeout() *RingBuffer {
r.mu.Lock()
r.noClOnTo = true
r.mu.Unlock()
return r
}

func (r *RingBuffer) setErr(err error, locked bool) error {
if !locked {
r.mu.Lock()
Expand Down Expand Up @@ -319,7 +332,9 @@ func (r *RingBuffer) waitRead() (ok bool) {

r.readCond.Wait()
if time.Since(start) >= r.rTimeout {
r.setErr(context.DeadlineExceeded, true) //nolint errcheck
if !r.noClOnTo {
r.setErr(context.DeadlineExceeded, true) //nolint errcheck
}
return false
}
return true
Expand Down Expand Up @@ -440,7 +455,9 @@ func (r *RingBuffer) waitWrite() (ok bool) {

r.writeCond.Wait()
if time.Since(start) >= r.wTimeout {
r.setErr(context.DeadlineExceeded, true) //nolint errcheck
if !r.noClOnTo {
r.setErr(context.DeadlineExceeded, true) //nolint errcheck
}
return false
}
return true
Expand Down