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: WithContext had no effect #1337

Merged
merged 4 commits into from
Mar 9, 2023
Merged

Conversation

armsnyder
Copy link
Contributor

Fixes #1336

Copy link
Collaborator

@FedorLap2006 FedorLap2006 left a comment

Choose a reason for hiding this comment

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

Hi, thanks for outlining the problem. Haven't noticed it myself when implementing the options.
However I'm not sure this solution would be correct one, as it's not entirely consistent with how other options are implemented.

Looking through the code of RequestWithLockedBucket, it uses cfg.Client.Do(req) for making a request. If that line would be replaced by cfg.Client.Do(cfg.Request) this problem would be solved.

@armsnyder
Copy link
Contributor Author

@FedorLap2006 I still prefer my first version, but I changed it to reassign req in the caller function instead. I didn't want to simply replace cfg.Client.Do(req) with cfg.Client.Do(cfg.Request) since there are more places that req is used.

@FedorLap2006 FedorLap2006 added this to the v0.28.0 milestone Mar 1, 2023
@FedorLap2006
Copy link
Collaborator

FedorLap2006 commented Mar 1, 2023

I didn't want to simply replace cfg.Client.Do(req) with cfg.Client.Do(cfg.Request) since there are more places that req is used.

My mistake, yes, there were a few other places that it was used in.
In that case, all occurences should be replaced by cfg.Request then. Reassignment is fine, but might be confusing when reading the function from the point and forward, the cfg.Request on other hand clearly communicates we're using the request modified by the With functions.

@armsnyder
Copy link
Contributor Author

@FedorLap2006 Hi, I followed your suggestion and updated the code. ☺️

My two cents are the first version with *cfg.Request = *cfg.Request.WithContext(ctx) is the cleaner and more consistent option.

More consistent: All the other RequestOption's are mutating the original request. It seems that WithContext should also mutate it.

Cleaner: It's better to have only one request in-scope in the RequestWithLockedBucket function, instead of keeping a reference to the old req before applying options. It is a long function (111 lines!), so it may not be obvious for future code changes whether req or cfg.Request should be used.

You have final say, of course. 😅

@FedorLap2006 FedorLap2006 modified the milestones: v0.28.0, v0.27.1 Mar 7, 2023
@FedorLap2006 FedorLap2006 changed the title fix: WithContext had no effect feat(RequestConfig): add Context field Mar 9, 2023
@FedorLap2006 FedorLap2006 changed the title feat(RequestConfig): add Context field fix: WithContext had no effect Mar 9, 2023
@FedorLap2006
Copy link
Collaborator

FedorLap2006 commented Mar 9, 2023

Sorry for taking matter into my own hands. After thinking for a while, the second solution (with reassigning req), does not affect readability that much. And has the least amount of changes. Therefore I've reverted the replacement of cfg.Request

P.S. Another way this could have been implemented was to use a Context field in RequestConfig and then reassign the req to req.WithContext(cfg.Request) (which is what I originally wanted to do), but req = cfg.Request would just be a simpler version of this.

restapi_test.go Outdated Show resolved Hide resolved
@FedorLap2006 FedorLap2006 added the fix Pull requests and issues related to bug fixes and structural inconsistencies label Mar 9, 2023
restapi_test.go Outdated Show resolved Hide resolved
@FedorLap2006
Copy link
Collaborator

FedorLap2006 commented Mar 9, 2023

Sorry for commiting right on the branch. Since the release is planned for today, I've decided to not prolong the things even more and apply all the edits myself.

Thank you for the contribution!

@FedorLap2006 FedorLap2006 merged commit b1d5d2e into bwmarrin:master Mar 9, 2023
FedorLap2006 added a commit that referenced this pull request Mar 9, 2023
Fix usage of an outdated request in RequestWithLockedBucket after applying WithContext option

---------

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
@armsnyder
Copy link
Contributor Author

@FedorLap2006 No worries! Ultimately what matters most is the bug is fixed. 😄 Thanks for the review.

@armsnyder armsnyder deleted the context branch March 9, 2023 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Pull requests and issues related to bug fixes and structural inconsistencies
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WithContext has no effect
2 participants