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 race condition during request: Protect pre/post request hooks with RWMutex #597

Merged
merged 2 commits into from Mar 8, 2023

Conversation

jameshoulahan
Copy link
Contributor

@jameshoulahan jameshoulahan commented Nov 4, 2022

Firstly, thanks for the wonderful library! It's an integral part of one of my team's projects and it works beautifully.

In our project, we have one resty client that performs all API requests. It is shared among multiple "users" that each inject their own auth tokens into each request via pre request hooks.

The issue is, if one user is performing requests at the same time as another user is being created (which involves setting up its pre-request hooks), our CI reports a data race.

Given that an API client such as resty is intended to be used in a highly parallel way, potentially by many different places in the code, it makes sense to me that user-modifiable things such as pre- and post-request hooks should be protected from data races. As such, I have added an RWMutex for each user-modifiable hook slice.

@jameshoulahan jameshoulahan changed the title Protect pre/post request hooks with RWMutex to mitigate race conditions Fix race condition during request: Protect pre/post request hooks with RWMutex Nov 4, 2022
@jeevatkm
Copy link
Member

jeevatkm commented Jan 5, 2023

@jameshoulahan Thanks for your compliment and the PR contribution.

I agree, if dynamically the middleware is getting added/removed from resty then RWMutex is needed!

I just noticed codecov reports are not coming in, so currently, many PR are blocked. Let me figure out that, then I will merge this PR once PR validation is done.

@jeevatkm
Copy link
Member

jeevatkm commented Jan 5, 2023

@jameshoulahan I'm sorry for the late response from my end.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

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

@jameshoulahan My bad, I messed up the gofmt while resolving the code conflict with web editor. Could you please revise the PR?

FYI, I have fixed the codecov and PR validation issues.

@codecov
Copy link

codecov bot commented Mar 6, 2023

Codecov Report

Merging #597 (f29e773) into master (9e2a9b7) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #597      +/-   ##
==========================================
+ Coverage   95.94%   95.97%   +0.02%     
==========================================
  Files          10       10              
  Lines        1357     1365       +8     
==========================================
+ Hits         1302     1310       +8     
  Misses         34       34              
  Partials       21       21              
Impacted Files Coverage Δ
client.go 97.81% <100.00%> (+0.04%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

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

Thanks @jameshoulahan, I took care at my end.

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

Successfully merging this pull request may close these issues.

None yet

2 participants