-
Notifications
You must be signed in to change notification settings - Fork 4.6k
mem: allow using io.WriterTo with a io.LimitedReader #8697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GiedriusS
wants to merge
1
commit into
grpc:master
Choose a base branch
from
GiedriusS:membuffer_limited
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would circumvent the limit of the
LimitedReader, wouldn't it? By directly accessing the underlying reader we are bypassing the limiter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but I'm not sure how to do a "LimitedWriter" if we could say it that way. Should we add something like https://github.com/nanmu42/limitio/blob/master/limitio.go to grpc-go code (as a library or our own impl)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @GiedriusS, one solution I can think of is to create your own wrapper struct that wraps a
io.LimitReaderand also implementsio.WriteTo. This would allow your reader to control the size of the temporary buffer being used. Here's a example implementation could work.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The core issue is that
grpc-godoes an assertion (and it wraps io.Reader inside of a io.LimitedReader itself) whether it's aio.Readerandio.LimitedReaderis not aio.Readerso I think this path would never be hit.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I overlooked the line in the PR description: "This happens if some max message size is set."
gRPC controls the the reader type and not external code. Let me think about it a little more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the simplest solution I came up with:
LimitWriter: Create a wrapper aroundio.Writerthat restricts the number of bytes written. If a write exceeds the limit, it returns a specific sentinel error (e.g.,ErrLimitExhausted).ReadAll: InReadAll, we check if the reader is an*io.LimitedReaderand if the underlyingio.Readerimplementsio.WriterTo. If so, we create a new writer usingNewWriter(&result, pool), wrap it in ourLimitWriter, and callWriteToon the underlying reader. This effectively transfers the limit constraint from the reader to the writer.To keep this optimization transparent to callers,
ReadAllmust trap the error returned byLimitWriter. It should translate that error into a successful return (nil error) and update theNfield on the*io.LimitedReaderto reflect the bytes actually consumed.Here are some snippets to explain this:
The Helper Type
The Updated
ReadAllFunctionHere is how the logic fits into your existing context.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice. Only one thought that comes to mind - it would be cool to refactor this function & how wrapping is done a little bit so that it wouldn't be needed to do another type assertion. IIRC, each type assertion translates to a new allocation on the heap so it would be nice to avoid this since this is a hot path. In our case, this function is called millions of times so it would mean a lot of extra allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think heap allocs are caused due to type assertions, instead they're caused when a new
LimitedWriterobject is created and passed to another function. The trick to avoid this extra heap allocation is to store theLimitReaderand theLimitWriteras a single object without any pointer fields.To do this, we will need to move this optimization to
rpc_util.gowhere theLimitReaderis getting created. The following snippet shows a struct that does this.In the callsite, we can do a type assertion to check if the
ReaderimplementsWriterTo, if yes, we wrap it in the newLimitReader, else we continue to wrap it in anio.LimitReader.The following playground demonstrates this: https://go.dev/play/p/vd7V_fXm_in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the
WriterTointerface insidemem.ReadAllshould cause an extra copy: 1 copy to a temporary buffer + 1 copy in the call toWrite().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 32KB allocation issue was also mentioned here: cockroachdb/cockroach#136278 (comment)
Since the
WriterTointerface incurs and extra copy, it doesn't look like a perfect solution. Maybe we can have compressors implement an optional interface that specified the buffer size to use for copies. I'll discuss this with other maintainers.