Skip to content
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

Reuse the byte buffer from GRPC response in the frontend. #4377

Merged
merged 3 commits into from
Aug 26, 2021

Conversation

cyriltovena
Copy link
Contributor

This PR allow to reuse the GRPC byte buffer casted as a io.Reader in the http roundtripper of the frontend.
This way we don't have to copy the content from the reader into another buffer when reading the http response.

I've been testing this with in Loki and this shows good memory saving (~ 1.2GB).

Signed-off-by: Cyril Tovena cyril.tovena@gmail.com

This PR allow to reuse the GRPC byte buffer casted as a `io.Reader` in the http rountripper of the frontend.
This way we don't have to copy the content from the reader into another buffer when reading the http response.

I've been testing this with in Loki and this shows good memory saving (around 1GB).

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
Copy link
Contributor

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, but I think it could be made easier to read.

Comment on lines 287 to 288
func bodyBuffer(res *http.Response) ([]byte, error) {
if buffer, ok := res.Body.(Buffer); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put a comment on here, as this cast seems to be the key part.

@@ -70,3 +70,7 @@ func DoRequests(ctx context.Context, downstream Handler, reqs []Request, limits

return resps, firstErr
}

type Buffer interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Buffer such a long way away from where it gets used?
Do you even need a type? interface { Bytes() []byte } is pretty straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can move it but I think it's nice to have the type so, Loki uses this package and can be used as documentation.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
@cyriltovena
Copy link
Contributor Author

Thanks @bboreham I've made the change let me know .

Copy link
Contributor

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@pracucci pracucci merged commit e92f96f into cortexproject:master Aug 26, 2021
alvinlin123 pushed a commit to ac1214/cortex that referenced this pull request Jan 14, 2022
…ect#4377)

* Reuse the byte buffer from GRPC response in the frontend.

This PR allow to reuse the GRPC byte buffer casted as a `io.Reader` in the http rountripper of the frontend.
This way we don't have to copy the content from the reader into another buffer when reading the http response.

I've been testing this with in Loki and this shows good memory saving (around 1GB).

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Use safe cast

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Review feedback.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
Signed-off-by: Alvin Lin <alvinlin@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants