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

querier: exhaust response body before closing #4429

Merged
merged 1 commit into from
Aug 20, 2021

Conversation

GiedriusS
Copy link
Contributor

What this PR does:

Exhaust the whole body of the response before closing to facilitate
re-use of keep-alive connections as per the documentation here:
https://pkg.go.dev/net/http#Client.Get

Caller should close resp.Body when done reading from it.

Also some discussion here:
https://groups.google.com/g/golang-nuts/c/148riho42sU
#4428

Which issue(s) this PR fixes:
It fixes a part of #4428.

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Not sure if any CHANGELOG changes are needed 🤔

@pstibrany
Copy link
Contributor

Reading entire body may take a while or simply never finish, if malicious client keeps sending data (one byte per second :)). It might be good idea to impose some time or size limit on this reading.

@GiedriusS
Copy link
Contributor Author

Reading entire body may take a while or simply never finish, if malicious client keeps sending data (one byte per second :)). It might be good idea to impose some time or size limit on this reading.

Aren't requests time-bound via deadlines set on contexts? If not, could you please suggest something specific that I could use here? On the Thanos side, we've been using such a construct for a very long time without any problems: https://github.com/thanos-io/thanos/blob/main/pkg/runutil/runutil.go#L45-L49

@pstibrany
Copy link
Contributor

pstibrany commented Aug 18, 2021

Aren't requests time-bound via deadlines set on contexts? If not, could you please suggest something specific that I could use here? On the Thanos side, we've been using such a construct for a very long time without any problems: https://github.com/thanos-io/thanos/blob/main/pkg/runutil/runutil.go#L45-L49

There may or may not be timeout. Still, I would suggest to read say max 1 KB of remaining body – that should cover most cases. If there is more data in the body, just close it, and drop the connection:

		_ = io.Copy(ioutil.Discard, io.LimitReader(response.Body, 1024))
		_ = response.Body.Close()

Exhaust the whole body of the response before closing to facilitate
re-use of keep-alive connections as per the documentation here:
https://pkg.go.dev/net/http#Client.Get

```
Caller should close resp.Body when done reading from it.
```

Also some discussion here:
https://groups.google.com/g/golang-nuts/c/148riho42sU
cortexproject#4428

Signed-off-by: Giedrius Statkevičius <[email protected]>
@GiedriusS GiedriusS force-pushed the feature/exhaust_body branch from 2787ddd to 514f93d Compare August 19, 2021 10:56
@GiedriusS
Copy link
Contributor Author

@pstibrany thanks for the suggestion, I have applied it and tested that with this connections are still properly re-used.

Copy link
Contributor

@pstibrany pstibrany left a comment

Choose a reason for hiding this comment

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

Thank you!

@pstibrany
Copy link
Contributor

Note that Cortex uses Codec implementation that always reads response body until EOF, so this isn't a problem. Thanos codec could do the same.

@pstibrany pstibrany merged commit 70dddb6 into cortexproject:master Aug 20, 2021
@GiedriusS GiedriusS deleted the feature/exhaust_body branch August 20, 2021 08:18
alvinlin123 pushed a commit to ac1214/cortex that referenced this pull request Jan 14, 2022
Exhaust the whole body of the response before closing to facilitate
re-use of keep-alive connections as per the documentation here:
https://pkg.go.dev/net/http#Client.Get

```
Caller should close resp.Body when done reading from it.
```

Also some discussion here:
https://groups.google.com/g/golang-nuts/c/148riho42sU
cortexproject#4428

Signed-off-by: Giedrius Statkevičius <[email protected]>
Signed-off-by: Alvin Lin <[email protected]>
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