Skip to content

v0.3.0

Latest

Choose a tag to compare

@smallnest smallnest released this 26 Nov 10:17
3bd16e4

What's new

Overwrite mode (SetOverwrite(true)) now behaves like a traditional circular buffer for writes that do not fit: the buffer keeps the last Capacity() bytes and the write is reported as fully accepted. A blocking overwrite write no longer waits for a reader, for writes of any size. Nothing else changes.

Breaking changes

  • A non-blocking Write that does not fit no longer returns ErrTooMuchDataToWrite with n < len(p), and no longer leaves the head of the write in the buffer. It now returns n == len(p), err == nil and keeps the tail, matching blocking mode (#40).

    rb := ringbuffer.New(4).SetOverwrite(true)
    rb.Write([]byte("abcdef"))
    // before: n=4, ErrTooMuchDataToWrite, buffer "abcd"
    // now:    n=6, nil,                    buffer "cdef"

    Callers that used n or that error to detect dropped data should check rb.Length() / rb.Bytes() instead.

  • TryWrite in overwrite mode keeps the tail too, so it no longer differs from Write.

Fixes

  • A blocking overwrite Write larger than the buffer no longer waits for a reader: no more deadlock when nothing is reading, and no more hang instead of returning (by @bardrop, #39).
  • An overwrite write larger than the buffer is now trimmed to the bytes that survive before copying, instead of copying the whole write and overwriting it chunk by chunk.

Docs

  • SetOverwrite, SetBlocking, Write and ErrTooMuchDataToWrite document the overwrite-mode behaviour, and the README gained an "Overwrite mode" section.

Compatibility

  • Non-overwrite mode is untouched: ErrTooMuchDataToWrite, ErrIsFull and the blocking semantics are unchanged.
  • ReadFrom still waits for a reader when the buffer is full in overwrite mode; unchanged in this release.