Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

feat(Rest): improve global rate limit and invalid request tracking #51

Merged
merged 1 commit into from
Oct 29, 2021

Conversation

ckohen
Copy link
Member

@ckohen ckohen commented Aug 11, 2021

Please describe the changes this PR makes and why it should be merged:

Ported from discordjs/discord.js#4711
The logic is all taken pretty much directly. This is the start of a series of PRs to catch up the REST module to discord.js v13s built-in REST implementation.

Status and versioning classification:

  • I know how to update typings and have done so, or typings don't need updating
  • This PR changes the library's interface (methods or parameters added)
  • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)

@codecov
Copy link

codecov bot commented Aug 11, 2021

Codecov Report

Merging #51 (674fdd7) into main (9eb6afb) will decrease coverage by 1.15%.
The diff coverage is 82.90%.

❗ Current head 674fdd7 differs from pull request most recent head c18f541. Consider uploading reports for the commit c18f541 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
- Coverage   89.98%   88.82%   -1.16%     
==========================================
  Files           8        8              
  Lines        1108     1342     +234     
  Branches      109      142      +33     
==========================================
+ Hits          997     1192     +195     
- Misses          3        9       +6     
- Partials      108      141      +33     
Impacted Files Coverage Δ
packages/rest/src/lib/utils/utils.ts 72.97% <70.83%> (-3.96%) ⬇️
...ackages/rest/src/lib/handlers/SequentialHandler.ts 82.83% <81.06%> (-1.29%) ⬇️
packages/rest/src/lib/RequestManager.ts 93.89% <92.30%> (+0.20%) ⬆️
packages/rest/src/lib/REST.ts 96.17% <100.00%> (+0.59%) ⬆️
packages/rest/src/lib/utils/constants.ts 100.00% <100.00%> (ø)

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 9eb6afb...c18f541. Read the comment docs.

@iCrawl iCrawl added this to In progress in REST via automation Aug 11, 2021
REST automation moved this from In progress to Review in progress Aug 11, 2021
@ckohen
Copy link
Member Author

ckohen commented Aug 12, 2021

Breaking Change in the last commit:
Sublimit requests will 429 on every request as there is no effective way to ensure the sublimit is not hit other than stalling the entire bucket for the entire duration of the sublimit

Note: they are retried after the timeout still

Edit: this will be hardcoded around for the only known sublimit in the next commit

packages/rest/src/lib/REST.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/REST.ts Show resolved Hide resolved
@ckohen
Copy link
Member Author

ckohen commented Aug 30, 2021

Okay, significant refactor to sublimit handling, here's how it should work now:

  • Normal requests get added to the normal queue
  • Potentially sublimited requests get added to the normal queue when there is no sublimit queue
  • If a sublimit is actually hit, a sublimit queue is constructed
  • When a sublimit queue exists, every request is checked (retroactively too) for whether it is sublimited before being sent out
  • If the request is sublimited:
    • Retroactively remove the request from the normal queue if it existed there
    • Wait for the sublimit queue instead
  • Otherwise wait for the standard queue and if the sublimit queue is being processed, wait for that
  • Remove the sublimit queue when it has no more requests pending

packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
@ckohen
Copy link
Member Author

ckohen commented Aug 31, 2021

Test have been added to cover everything that has changed!

iCrawl
iCrawl previously approved these changes Sep 3, 2021
@iCrawl iCrawl dismissed stale reviews from vladfrangu and kyranet September 3, 2021 20:40

Stale

packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
@ckohen
Copy link
Member Author

ckohen commented Oct 1, 2021

I totally forgot I still had this PR open 🤦 , addressed changes and rebased.

packages/rest/src/lib/REST.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/handlers/SequentialHandler.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/RequestManager.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
packages/rest/src/lib/utils/utils.ts Outdated Show resolved Hide resolved
vladfrangu
vladfrangu previously approved these changes Oct 19, 2021
vladfrangu
vladfrangu previously approved these changes Oct 25, 2021
iCrawl
iCrawl previously approved these changes Oct 25, 2021
REST automation moved this from Review in progress to Reviewer approved Oct 25, 2021
Ported from discordjs/discord.js#4711

refactor(SequentialHandler): use timers/promises

refactor(SequentialHandler): move global checks to each request

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

fix: separate queue for sub limits

refactor(SequentialHandler): address PR comments

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

refactor(SequentialHandler): better sublimit handling

refactor: address pr comments

Co-Authored-By: Vlad Frangu <kingdgrizzle@gmail.com>
Co-Authored-By: Antonio Román <kyradiscord@gmail.com>
Co-Authored-By: Noel <icrawltogo@gmail.com>

test: add sublimit tests

refactor(Util): only check route once

refactor: rename globalLimit

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

fix: address pr comments

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

refactor(Utils): use enum

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>

Update packages/rest/src/lib/handlers/SequentialHandler.ts
@ckohen ckohen dismissed stale reviews from iCrawl and vladfrangu via c18f541 October 26, 2021 05:03
REST automation moved this from Reviewer approved to Review in progress Oct 26, 2021
REST automation moved this from Review in progress to Reviewer approved Oct 26, 2021
@iCrawl iCrawl merged commit b73cc06 into discordjs:main Oct 29, 2021
REST automation moved this from Reviewer approved to Done Oct 29, 2021
@ckohen ckohen deleted the rest-global-invalid branch October 30, 2021 09:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
REST
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants