ringbuf: add UnsafeReader for zero-copy reads with deferred commit#1981
Closed
orishuss wants to merge 1 commit into
Closed
ringbuf: add UnsafeReader for zero-copy reads with deferred commit#1981orishuss wants to merge 1 commit into
orishuss wants to merge 1 commit into
Conversation
Signed-off-by: Ori Shussman <orishuss@gmail.com>
0490c85 to
14483fb
Compare
Contributor
|
Closing as duplicate of #1979. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Supersedes #1968. Implements the separate
UnsafeReadertype as recommended by @ti-mo and @florianl in the review of #1968, rather than adding zero-copy methods directly toReader.Changes
UnsafeReader— new type for zero-copy ring buffer reads. ReturnsUnsafeRecordwithRawSamplepointing directly into the mmap'd ring buffer region. Separate type fromReaderso the two APIs can't be mixed accidentally.UnsafeRecord— wrapsRecordvia embedding. Compile-time type safety: you can't pass*UnsafeRecordtoReader.ReadIntoor*RecordtoUnsafeReader.Read.CommitToken— opaque token (unexportedconsPosfield) returned byRead. User can't inspect or forge it.Commit(token)— per-token commit with ordered advancement. Consumer position only advances when all preceding tokens are also committed (e.g. commit B,C then A → advances past C).CommitAll()— batch commit, advances consumer to the latest read position.ReadFunc(func([]byte))— callback-based read with implicit auto-commit after the callback returns.Architecture
Addresses @ti-mo and @florianl's feedback from #1968:
UnsafeReaderis distinct fromReader. No mixing of safe and unsafe APIs on the same type.readerBasewhich provides Close, SetDeadline, BufferSize, Flush, AvailableBytes, and the poll loop (readWaitLocked).eventRinginterface — only exposes primitives both readers use:readRecordUnsafe,advanceTo,pendingPosition. NoreadRecordon the ring — that logic lives insafeRing, a small wrapper used only byReader.safeRing— wrapseventRingto providereadRecord(readRecordUnsafe + copy + advanceTo). KeepsReader's API and behavior identical to today.ReadFuncreceives[]byte(notRecord-driven), auto-commits after the callback.RawSamplevalidity after commit is the caller's responsibility.File layout
ring.goeventRinginterface,ringbufHeader,ringReader(readRecordUnsafe, advanceTo, pendingPosition)reader_base.gopoller,Record,readerBase+ shared methods,initReaderBasereader.gosafeRing,Reader, NewReader, Read, ReadIntounsafe_reader.goUnsafeRecord,CommitToken,UnsafeReader, NewUnsafeReader, Read, ReadFunc, Commit, CommitAllunsafe_reader_test.goNo changes to
ring_other.go,ring_windows.go,reader_test.go, or platform files.