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

Add promise support for onRetryAttempt and shouldRetry #223

Merged
merged 7 commits into from
Jan 30, 2020
Merged

Add promise support for onRetryAttempt and shouldRetry #223

merged 7 commits into from
Jan 30, 2020

Conversation

atjeff
Copy link
Contributor

@atjeff atjeff commented Jan 22, 2020

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #144 🦕

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 22, 2020
@atjeff
Copy link
Contributor Author

atjeff commented Jan 23, 2020

I didn't see any harm in allowing promises in onRetryAttempt, as we're just notifying the user we're retrying, do you think it should await the completion of the onRetryAttempt promise?

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

This looks reasonable to me, but it would be good to also have @JustinBeckwith's thoughts.

Since retry is already async, I think you're right that this probably isn't a breaking change.

src/retry.ts Outdated
@@ -55,7 +55,8 @@ export async function getRetryConfig(err: GaxiosError) {

// Determine if we should retry the request
const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
if (!shouldRetryFn(err)) {
const shouldRetryFnPromise = Promise.resolve(shouldRetryFn(err));
Copy link
Contributor

Choose a reason for hiding this comment

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

is this Promise.resolve necessary, await false would just resolve false right?

@@ -219,6 +218,32 @@ describe('🛸 retry & exponential backoff', () => {
scopes.forEach(s => s.done());
});

it('should notify on retry attempts, including promises', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be tempted to make this assertion something like:

accepts async onRetryAttempt handler

@atjeff
Copy link
Contributor Author

atjeff commented Jan 24, 2020

Thanks for the review @bcoe! I was seeing a lot of tests failing without the resolve, I'll hopefully find some time to look into why tomorrow. I renamed the test

@atjeff
Copy link
Contributor Author

atjeff commented Jan 27, 2020

Found any time @JustinBeckwith ?

Copy link
Contributor

@JustinBeckwith JustinBeckwith left a comment

Choose a reason for hiding this comment

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

LGTM. Want to make sure @bcoe is happy too :)

@JustinBeckwith JustinBeckwith added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 27, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 27, 2020
@codecov
Copy link

codecov bot commented Jan 27, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@f0539b7). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #223   +/-   ##
=========================================
  Coverage          ?   95.84%           
=========================================
  Files             ?        6           
  Lines             ?      602           
  Branches          ?       99           
=========================================
  Hits              ?      577           
  Misses            ?       24           
  Partials          ?        1
Impacted Files Coverage Δ
src/retry.ts 98.68% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f0539b7...dd3eead. Read the comment docs.

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

Hey @atjeff testing out your branch, this approach worked great for me:

   // Determine if we should retry the request
   const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
-  const shouldRetryFnPromise = Promise.resolve(shouldRetryFn(err));
-  if (!(await shouldRetryFnPromise)) {
+  if (!(await await shouldRetryFn(err))) {
     return {shouldRetry: false, config: err.config};
   }

What tests were failing for you prior to the resolving step? I wonder if it might have been something else biting you. If we can do without the Promise.resolve, I rather would 👌

@atjeff
Copy link
Contributor Author

atjeff commented Jan 30, 2020

Yep, it was me not thinking to await twice. Woops! Pushing that change now.

@atjeff
Copy link
Contributor Author

atjeff commented Jan 30, 2020

I'm not sure how I had failing tests, sorry about that. Awaiting shouldRetryFn(err) works fine. Pushed @bcoe

@bcoe bcoe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 30, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 30, 2020
@atjeff
Copy link
Contributor Author

atjeff commented Jan 30, 2020

Thanks for taking the time guys!

@bcoe bcoe merged commit 061afa3 into googleapis:master Jan 30, 2020
@wiktor-obrebski
Copy link

wiktor-obrebski commented Feb 7, 2020

sorry I dig it back, but from the discussion I expected that retry will wait for both: onRetryAttempt and shouldRetry functions.
but I clearly see that the code wait only for shouldRetry function, but not for onRetryAttempt.

gaxios/src/retry.ts

Lines 75 to 77 in dd3eead

if (config.onRetryAttempt) {
config.onRetryAttempt(err);
}

do I miss something?

yoshi-automation added a commit that referenced this pull request Apr 1, 2020
061afa3
commit 061afa3
Author: Jeff Hage <atjeff@users.noreply.github.com>
Date:   Thu Jan 30 18:59:37 2020 -0500

    feat: add promise support for onRetryAttempt and shouldRetry (#223)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for Promise on onRetryAttempt()
6 participants