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

[NoQA] Increase the octokit gh throttling times #22210

Merged
merged 7 commits into from
Jul 7, 2023

Conversation

mountiny
Copy link
Contributor

@mountiny mountiny commented Jul 4, 2023

Details

In case when there was many PRs in the deploy, we have experience rate limitting as we dont wait between the issue comment requests. This PR is adding 1s sleep between the create comment requests to avoid this happening in future.

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/293688
PROPOSAL: GH_LINK_ISSUE(COMMENT)

Tests

N/A

  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

Ensure the deploy works correctly and all the PRs deployed got a message posted on them.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

No need for screenshots because the changes are only in CI/CD

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@mountiny mountiny requested a review from a team as a code owner July 4, 2023 18:34
@mountiny mountiny self-assigned this Jul 4, 2023
@melvin-bot melvin-bot bot requested review from abdulrahuman5196 and removed request for a team July 4, 2023 18:34
@melvin-bot
Copy link

melvin-bot bot commented Jul 4, 2023

@abdulrahuman5196 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@@ -67,6 +67,7 @@ function getDeployMessage(deployer, deployVerb, prTitle) {
*/
function commentPR(PR, message) {
return GithubUtils.createComment(context.repo.repo, PR, message)
.then(ActionUtils.sleep(1000))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main place we are commenting on many issues/ PRs from here so I think tis hould be fine added only here to start with as maybe the rate limiting wont be such problem in future anymore as we should not have such big deploys as we had when this issue occurred.

Working with JS Promises is not my strength so just wanna highlight this to make sure its correctly implemented

Copy link
Contributor

@abdulrahuman5196 abdulrahuman5196 Jul 5, 2023

Choose a reason for hiding this comment

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

I think we have to provide () => ActionUtils.sleep(1000), I created a small mock of the code and it was not working and the mentioned change made it work - https://jsfiddle.net/abdulrahuman5196/Loupsxwc/ . Let me know if there is something wrong with the mock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! that makes sense, update the code 🙇

@abdulrahuman5196
Copy link
Contributor

Reviewing the PR now

@@ -67,6 +67,7 @@ function getDeployMessage(deployer, deployVerb, prTitle) {
*/
function commentPR(PR, message) {
return GithubUtils.createComment(context.repo.repo, PR, message)
.then(ActionUtils.sleep(1000))
Copy link
Contributor

@abdulrahuman5196 abdulrahuman5196 Jul 5, 2023

Choose a reason for hiding this comment

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

I think we have to provide () => ActionUtils.sleep(1000), I created a small mock of the code and it was not working and the mentioned change made it work - https://jsfiddle.net/abdulrahuman5196/Loupsxwc/ . Let me know if there is something wrong with the mock.

@abdulrahuman5196
Copy link
Contributor

@mountiny With the last change, this seems to be working in Fiddle with the mock code. Is there any different way i can test this out? not sure on this since it essentially adds comments to PR

thienlnam
thienlnam previously approved these changes Jul 6, 2023
Copy link
Contributor

@thienlnam thienlnam left a comment

Choose a reason for hiding this comment

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

Code looks good but throwing this out there if we wanted to switch to an async method hah

async function commentPR(PR, message) {
    try {
        await GithubUtils.createComment(context.repo.repo, PR, message);
        await new Promise(r => setTimeout(r, 1000));
        console.log(`Comment created on #${PR} successfully 🎉`);
    } catch (err) {
        console.log(`Unable to write comment on #${PR} 😞`);
        core.setFailed(err.message);
    }
}

AndrewGable
AndrewGable previously approved these changes Jul 6, 2023
Copy link
Contributor

@roryabraham roryabraham 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 pretty simple, but I personally do not think we should merge this as-is, because we already have a throttling plugin for all GitHub requests. Documentation is light but available here and here. This plugin has separate limits for reads, writes, and search, accounts for rate limiting headers in the GitHub API response, and I think will end up being more robust than this.

  • I think the first thing I would try would be to update this to retry more than once, let's say 5 times
  • Also add onSecondaryRateLimit that logs a warning and returns true. This should pause for 60 seconds to address rate limiting.
  • If we still have issues, we can pass a higher value for options.retryAfterBaseValue to the plugin. It defaults to 1000ms, we could bump that to 1500 or 2000ms.
  • We could also pass a higher value for options.fallbackSecondaryRateRetryAfter to wait for more than 60 seconds when we reach a secondary rate limit

@AndrewGable
Copy link
Contributor

AndrewGable commented Jul 6, 2023

Also add onSecondaryRateLimit that logs a warning and returns true. This should pause for 60 seconds to address rate limiting.

Once you are secondary rate limited the sleep(60) doesn't help for an hour, you cannot create content for an hour.

The one second sleep is the advised fix in the GitHub documentation here:

If you're making a large number of POST, PATCH, PUT, or DELETE requests for a single user or client ID, wait at least one second between each request.

@mountiny
Copy link
Contributor Author

mountiny commented Jul 6, 2023

Not sure if there is a good way tot test this now either @abdulrahuman5196 thanks for testing with the fiddle!

@roryabraham thanks for the links! I have assumed what Andrew mentioned, once we hit this rate limit we cant easily just wait to retry later, so I think that 1 second wait is better at this point. Let me know what you think, thanks!

@roryabraham
Copy link
Contributor

The plugin already does 1, 2, or 3 second sleep between every request, depending on the type of request link

@roryabraham
Copy link
Contributor

From the octokit docs:

In order to automatically throttle requests as recommended in GitHub’s best practices for integrators, we recommend you install the @octokit/plugin-throttling plugin.

@AndrewGable
Copy link
Contributor

Seems like comments should be rate limited then: https://github.com/octokit/plugin-throttling.js/blob/main/src/generated/triggers-notification-paths.ts#L9C2-L9C2

I like the idea of just increasing retryAfterBaseValue by 2x, I think it will allow us to scale slightly more.

Long term however, we are still using a Personal Access Token based bot, correct? I think we might need to look at moving over to an GitHub App bot e.g. Mevlin-Bot[Bot] like we have in other repos, from my testing previously I think you can create ~4x the amount of triggersNotificationPaths style API calls.

@roryabraham
Copy link
Contributor

Once you are secondary rate limited the sleep(60) doesn't help for an hour, you cannot create content for an hour.

Got a source?

Regardless, we could still do the other two things I suggested as well to increase the amount of throttling we do in general:

  • update this to retry more than once, let's say 5 times
  • pass a higher value for options.retryAfterBaseValue to the plugin. It defaults to 1000ms, we could bump that to 1500 or 2000ms.

Ultimately the throttling we get from the plugin is more sophisticated than a basic 1-second throttle between requests + accounts for the retry-after headers we get from the API, including extra throttling that can occur during GitHub incidents. Increasing the options.retryAfterBaseValue would have the same effect as this but would be applied to all requests, not just the deploy comments.

@roryabraham
Copy link
Contributor

Long term however, we are still using a Personal Access Token based bot, correct? I think we might need to look at moving over to an GitHub App bot e.g. Mevlin-Bot[Bot] like we have in other repos, from my https://github.com/Expensify/Expensify/issues/200920#issuecomment-1064663941 the amount of triggersNotificationPaths style API calls.

Definitely down to open an issue for this, seems like a good solution to allow us to scale up 👍🏼

@AndrewGable
Copy link
Contributor

Got a source?

No, just experience from my testing in the past. We should also rename the function to fix this:

[@octokit/plugin-throttling] onAbuseLimit() is deprecated and will be removed in a future release of @octokit/plugin-throttling, please use the onSecondaryRateLimit handler instead

@mountiny mountiny dismissed stale reviews from AndrewGable and thienlnam via 7e82b0d July 7, 2023 22:50
@mountiny mountiny changed the title [NoQA] Add sleep to github comment function to avoid rate limiting [NoQA] Increase the octokit gh throttling times Jul 7, 2023
@mountiny
Copy link
Contributor Author

mountiny commented Jul 7, 2023

@roryabraham @AndrewGable Updated with the suggestions, thanks for patience

I agree using github app bot would be best here to also align with our other github flows.

@roryabraham
Copy link
Contributor

roryabraham commented Jul 7, 2023

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@roryabraham
Copy link
Contributor

Going to merge this since we agreed on the approach in the convo above.

@roryabraham roryabraham merged commit 3475812 into main Jul 7, 2023
12 checks passed
@roryabraham roryabraham deleted the vit-addSleepToCommentingOnPRs branch July 7, 2023 23:40
@OSBotify
Copy link
Contributor

OSBotify commented Jul 7, 2023

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/roryabraham in version: 1.3.39-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/francoisl in version: 1.3.39-11 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

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

Successfully merging this pull request may close these issues.

6 participants