Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

[GraphQL-Client][Feature]Add the retries feature to the client #1017

Merged
merged 2 commits into from
Oct 20, 2023

Conversation

melissaluu
Copy link
Contributor

@melissaluu melissaluu commented Oct 19, 2023

WHY are these changes introduced?

We are adding the retries feature to the graphql-client to enable auto retrying of failed HTTP requests in both the fetch and request function. The HTTP request retries only occurs in the following conditions:

  • Request was abandoned
  • Server responded with a 429 - Too Many Request
  • Server responded with a 503 - Service Unavailable

A 1s delay is used between each network retries.

Client users are able to set the number of retries in 2 ways:

  1. Provide a retries config field during client initialization - this value will be applied to all HTTP requests by the client
  2. Provide a retries value in the RequestOptions config per request

By default, the client's retries value is set to 0 if none was passed in the initialization configuration.

WHAT is this pull request doing?

This PR adds the retries settings and functionalities to the graphql-client.

Type of change

  • Patch: Bug (non-breaking change which fixes an issue)
  • Minor: New feature (non-breaking change which adds functionality)
  • Major: Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have used yarn changeset to create a draft changelog entry (do NOT update the CHANGELOG.md file manually)
  • I have added/updated tests for this change
  • I have documented new APIs/updated the documentation for modified APIs (for public APIs)

@melissaluu melissaluu requested a review from a team as a code owner October 19, 2023 19:41
await sleep(resetTime);
return httpFetch(params, nextCount, maxTries);
}

Copy link
Contributor Author

@melissaluu melissaluu Oct 19, 2023

Choose a reason for hiding this comment

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

Note: This error only occurs when the request was abandoned (eg. no network).

If the server has responded, the client will return the last server response (as per the previous block).

@melissaluu melissaluu changed the title [WIP][Feature][graphql-client] Add the retries feature to the client [WIP][graphql-client] [Feature]Add the retries feature to the client Oct 19, 2023
@melissaluu melissaluu changed the title [WIP][graphql-client] [Feature]Add the retries feature to the client [WIP][graphql-client][Feature]Add the retries feature to the client Oct 19, 2023
@melissaluu melissaluu changed the title [WIP][graphql-client][Feature]Add the retries feature to the client [WIP][GraphQL-Client][Feature]Add the retries feature to the client Oct 19, 2023
Copy link
Contributor

@scottdixon scottdixon left a comment

Choose a reason for hiding this comment

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

Love it @melissaluu! We were only discussing this yesterday.

LGTM - will defer to @paulomarg for the ✅

packages/graphql-client/src/graphql-client.ts Outdated Show resolved Hide resolved

return response;
} catch (error) {
if (nextCount <= maxTries) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I forget where we landed on logging - would it be worth letting them know when a retry occurs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't implemented logging yet - that's my next task 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

You're a wizard. :TIL: x 100

packages/graphql-client/src/tests/graphql-client.test.ts Outdated Show resolved Hide resolved
@melissaluu melissaluu force-pushed the ml-graphql-client-retries branch from 60caf87 to 171cb94 Compare October 19, 2023 21:02
Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

This all LGTM!

| headers? | `{[key: string]: string}` | Additional and/or replacement headers to be used in the request |
| retries? | `number` | Alternative number of retries for the request. Retries only occur for requests that were abandoned or if the server responds with a `Too Many Request (429)` or `Service Unavailable (503)` response. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to do this for any 5XX error? I guess we don't want to end up flooding the server with retries if there's an actual outage going on?

Copy link
Contributor Author

@melissaluu melissaluu Oct 20, 2023

Choose a reason for hiding this comment

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

Yeah - the last thing we want to do is to flood the server when there's an actual outage - let's stick with only retrying for 503s for now (since this is a status code that does support retries - it's 1 of 3 status' that can return a retry-after header) and then we can see if devs gives other feedback and we can reevaluate.

@melissaluu
Copy link
Contributor Author

@paulomarg - do you think we should make an upper limit on how many retries a dev can set? The last thing we want is for someone to set a ridiculous # for retries 🤔 😨

I'm thinking 5 or 10? thoughts?

@paulomarg
Copy link
Contributor

@paulomarg - do you think we should make an upper limit on how many retries a dev can set? The last thing we want is for someone to set a ridiculous # for retries 🤔 😨

I'm thinking 5 or 10? thoughts?

Definitely a good call. I would even be a little stricter, I wouldn't go past 5 - this is meant to be a way to recover for certain niche cases like an idempotent job or some such, not a way to brute force requests until they work.

@melissaluu
Copy link
Contributor Author

Definitely a good call. I would even be a little stricter, I wouldn't go past 5 - this is meant to be a way to recover for certain niche cases like an idempotent job or some such, not a way to brute force requests until they work.

@paulomarg - if we're good with being stricter, I'd go for max 3 for now. For storefront builds, we definitely want to surface any issues sooner rather than later - especially things that affect UI and we don't want to display an infinite spinner 😅

@melissaluu melissaluu force-pushed the ml-graphql-client-retries branch from a1e52ef to efc66ea Compare October 20, 2023 16:10
@melissaluu melissaluu changed the title [WIP][GraphQL-Client][Feature]Add the retries feature to the client [GraphQL-Client][Feature]Add the retries feature to the client Oct 20, 2023
@melissaluu melissaluu merged commit 6553ba7 into main Oct 20, 2023
10 checks passed
@melissaluu melissaluu deleted the ml-graphql-client-retries branch October 20, 2023 16:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants