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

fix: executeWithRetry, save the request body and write the body to th… #296

Merged
merged 5 commits into from
Feb 22, 2023

Conversation

JingmaoYou
Copy link
Contributor

@JingmaoYou JingmaoYou commented Feb 7, 2023

…e request before we process it

Description

#293
The req.Body could only be read once, so when we retry the query, the body is empty, which will lead to a 400 error.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

Checklist

  • Linter passes correctly
  • Add tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary

Does this introduce a breaking change?

  • Yes
  • No

Further comments

numRetry := 0
for {
// update body
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't you need to close this req.Body at some point in the loop? (If not, why doing the req.Body.Close() line 208?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right, I need to close the body of the request. I change the code and use defer req.Body.Close(). The request Body needs to be closed immediately after reading it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you should defer it because you're in the infinite loop that can override multiple times req.Body

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, since the defer is executed when the surrounding function returns, it will not execute when the body is updated. I will close the req.Body after reading or updating it.

@JingmaoYou JingmaoYou requested a review from mga-chka February 8, 2023 15:09
@JingmaoYou JingmaoYou requested review from gontarzpawel and removed request for mga-chka February 9, 2023 09:15
Copy link
Collaborator

@mga-chka mga-chka left a comment

Choose a reason for hiding this comment

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

it might be worth doing a simple test sending 100ks http queries in a short amount of time to chproxy to be sure there are no side effects

@JingmaoYou
Copy link
Contributor Author

it might be worth doing a simple test sending 100ks http queries in a short amount of time to chproxy to be sure there are no side effects

I will add one test in the main_test.go file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants