-
Notifications
You must be signed in to change notification settings - Fork 1
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
Handle Concurrent API Auth Failures #515
Conversation
2f3b0e5
to
b39d330
Compare
Pull Request Test Coverage Report for Build 7507674600
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me. Were we able to validate it does work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirming: the idea here is that we just don't want two concurrent calls to refreshAuthToken
, right?
If that's right, I think we could have a simpler implementation by just https://www.npmjs.com/package/p-limit
import pLimit from 'p-limit';
const limited = pLimit(1);
// ...
await limited(async () => {
const refreshResult = await _refreshHandler(_authResult);
await storeAuthResult({
...refreshResult,
// Careful not to lose `refreshToken`, which may not be in `refreshResult`
refreshToken: refreshResult.refreshToken || _authResult.refreshToken,
});
// ...
})
I think that'd allow us to avoid refs, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is what we want. If I am understanding p-limit
correctly, N calls to refresh with still result in N refresh attempts. While this would probably work. It seems wasteful to make unneeded refresh calls if the first one succeeds. Similarly, if the first one fails, the others will also fail.
The code I have right now caches the first promise and returns it to all subsequent calls until the first promise finishes.
Is there a p-X library that would do that? p-throttle
looks close but the time requirement doesn't match with my use-case.
Example: I want to achieve all 1's since it is the first call but this prints 1, 2, 3
import pLimit from 'p-limit';
const limit = pLimit(1);
const doIt = (a) =>
limit(() => new Promise((done: any) => setTimeout(done(a), 5000)));
doIt(1).then(console.log);
doIt(2).then(console.log);
doIt(3).then(console.log);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautifully simple solution
🎉 This PR is included in version 11.11.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Changes
Reasoning
invalid_grant
and caused the app to log out.Notes
loading
state value which I think was lagging behind when two requests failed simultaneously. This uses a ref so it shouldn't lag behind because I ensured the logic is synchronous up to the point of where the ref value is set.