add PeekAt(): read at an offset past the read pointer - #31
Merged
Conversation
Author
|
Thank you! |
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.
Problem
Peekalways starts at the read pointer. That is the right default, but itleaves out a consumer that has to run ahead of what it can release.
Where bytes stay buffered until something confirms them — a retransmission
window, a stream that may have to be replayed after a reconnect — the read
pointer sits wherever confirmation has reached, which is not where reading
should resume. The two are separated by everything in flight.
Such a consumer keeps its own cursor, and today the only way to read at it is
Peek(p)withlen(p) >= cursor + n, then discard the firstcursorbytes. Sothe destination buffer has to be as large as the entire unreleased span, and
every read copies that whole prefix again. The cost grows with whatever is
unacknowledged, which is exactly the quantity that grows when a consumer falls
behind.
In our case (buffering a server's output so a browser that loses its WebSocket
can resume the byte stream exactly where it left off) this put a ceiling on
delivery: once the unacknowledged span exceeded the read buffer, the same prefix
came back every time and the stream stopped advancing.
API
Reads up to
len(p)bytes startingoffbytes past the read pointer, withoutmoving it.
PeekAt(0, p)isPeek(p).Semantics
offat or past the end of the buffered data returns(0, nil). Not anerror, and deliberately not
ErrIsEmpty. It means the consumer has takeneverything there is — an ordinary thing to ask, and something a caught-up
consumer asks on every poll.
ErrIsEmpty, matchingPeek.Otherwise: short reads at the end return what is there; the wrap is handled with
the same two-copy split as
copyFromBuffer;off < 0is treated like an emptyp.PeekAttakes onlyr.muand touches only committed-region state (length(),r,size). It never takeswriteMu, so it is consistent with the rule #29established — the read/inspection side stays independent of the write side, and
there is no lock-order interaction to get wrong.
Tests
peekat_test.go:TestPeekAtReadsFromTheOffset— the basic contract, and that the read pointer does not moveTestPeekAtShortAtTheEnd— fewer bytes available than requestedTestPeekAtPastTheEndIsNotAnError— the(0, nil)case aboveTestPeekAtAcrossTheWrap— offset and span straddling the end of the backing arrayTestPeekAtOnAFullBuffer— the boundary wherer == wmeans full, not emptyTestPeekAtEmptyAndDegenerate— empty buffer, emptyp, negative offsetTestPeekAtAgreesWithPeek— differential:PeekAt(0, p)matchesPeek(p)across sizesgofmt -l .empty,go vet ./...clean,go test -race -count=1 ./...greenagainst current master (go1.26.3), including #29's rewritten locking.