Skip to content

Conversation

newhouse
Copy link
Contributor

@newhouse newhouse commented May 25, 2021

Description of the change

So that the rate limiting in the Node Client tries to do a good job, I've done 2 things here:
1. Allow the User to pass a keyType option to the constructor that will determine what rate limiting options it will try to do
2. Leverage the response headers from the server to adjust the rate limiter if necessary, so in the end, the User doesn't really need to know/do anything.

This 2nd one above deserves some discussion/thought:

  • It basically chooses the first request as a "golden request" to go first, and then sets the Rate Limiter for the Client based on the information the Server gives back in the response headers.
  • While this "golden request" is in flight, any other requests that come through will all have to wait for it to complete...but then they will all go as fast at the Rate Limiter will let them once the "golden request" has done its thing.
  • This only happens once per instance of a Client.
  • In my experimentation, it appears to lead to comparable-if-not-better average performance.

An additional benefit of this is that if we ever change our rate limits, or want to change them per-client/api key, the code will "just work"™️ for them in the future.

If we like this, perhaps I should actually rip all the stuff where the User can even specify rate limit stuff (requestLimit, requestLimitMS, keyType), and let it all come from the server?
☝️ we did this

Type of change

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

Related issues

Relates to https://github.com/anvilco/anvil/issues/2220

Checklists

Development

  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development
  • No previous tests unrelated to the changed code fail in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached.
  • At least one reviewer has been requested
  • Changes have been reviewed by at least one other engineer
  • The relevant project board has been selected in Projects to auto-link to this pull request

@newhouse newhouse requested a review from benogle May 25, 2021 22:01
@newhouse newhouse force-pushed the newhouse/2220/rate-limiting-improvements branch from f689c65 to f9c775b Compare May 26, 2021 13:48
Copy link
Contributor

@benogle benogle left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@@ -53,7 +53,7 @@
"abort-controller": "^3.0.0",
"extract-files": "^6",
"form-data": "^3.0.0",
"limiter": "^1.1.5",
"limiter": "^2.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +509 to +518
const remainingRequests = await this.limiter.removeTokens(1)
if (remainingRequests < 1) {
await sleep(this.requestLimitMS + failBufferMS)
}
const retry = async (ms) => {
await sleep(ms)
return this._throttle(fn)
}

return fn(retry)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nicer as async / await

@newhouse newhouse merged commit 3b84903 into master May 27, 2021
@newhouse newhouse deleted the newhouse/2220/rate-limiting-improvements branch May 27, 2021 00:47
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.

2 participants