Repository navigation
feat: declare the rate-limit policy of the HubSpot, Salesforce and Pipedrive modules #102
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
Open
d-klotz
wants to merge
3
commits into
next
Choose a base branch
from
feat/rate-limit-policies
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+859
−13
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| const { FetchError, RateLimitError } = require('@friggframework/core'); | ||
| const { Api } = require('../api'); | ||
|
|
||
| const ONE_HOUR_MS = 3_600_000; | ||
|
|
||
| const burstLimitBody = { | ||
| status: 'error', | ||
| message: 'You have reached your ten_secondly_rolling limit.', | ||
| errorType: 'RATE_LIMIT', | ||
| correlationId: '00000000-0000-4000-8000-000000000000', | ||
| policyName: 'TEN_SECONDLY_ROLLING', | ||
| }; | ||
|
|
||
| const dailyLimitBody = { | ||
| status: 'error', | ||
| message: 'You have reached your daily limit.', | ||
| errorType: 'RATE_LIMIT', | ||
| correlationId: '00000000-0000-4000-8000-000000000000', | ||
| policyName: 'DAILY', | ||
| }; | ||
|
|
||
| const burstHeaders = { | ||
| 'X-HubSpot-RateLimit-Max': '110', | ||
| 'X-HubSpot-RateLimit-Remaining': '0', | ||
| 'X-HubSpot-RateLimit-Interval-Milliseconds': '10000', | ||
| }; | ||
|
|
||
| function throttledFetch(responses) { | ||
| const queue = [...responses]; | ||
| return jest.fn(async () => { | ||
| const next = queue.shift(); | ||
| if (!next) throw new Error('unexpected fetch call'); | ||
| const body = next.body ?? {}; | ||
| return { | ||
| status: next.status, | ||
| bodyUsed: false, | ||
| headers: new Map( | ||
| Object.entries({ | ||
| 'Content-Type': next.contentType ?? 'application/json', | ||
| ...next.headers, | ||
| }) | ||
| ), | ||
| json: async () => body, | ||
| text: jest.fn(async () => next.text ?? JSON.stringify(body)), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| const limited = (body, headers) => ({ status: 429, body, headers }); | ||
| const ok = { status: 200, body: { results: [] } }; | ||
|
|
||
| describe('HubSpot rate limits', () => { | ||
| let delays; | ||
|
|
||
| beforeEach(() => { | ||
| delays = []; | ||
| const realSetTimeout = global.setTimeout; | ||
| jest.spyOn(global, 'setTimeout').mockImplementation( | ||
| (fn, delay, ...args) => { | ||
| delays.push(delay); | ||
| return realSetTimeout(fn, 0, ...args); | ||
| } | ||
| ); | ||
| jest.spyOn(Math, 'random').mockReturnValue(0); | ||
| }); | ||
|
|
||
| afterEach(() => jest.restoreAllMocks()); | ||
|
|
||
| const makeApi = (fetch, ApiClass = Api) => | ||
| new ApiClass({ | ||
| fetch, | ||
| requestTimeoutMs: 0, | ||
| delegate: { name: 'hubspot', entity: { id: 7 } }, | ||
| }); | ||
|
|
||
| describe('a burst 429', () => { | ||
| it('waits as long as Retry-After says, then retries', async () => { | ||
| const fetch = throttledFetch([ | ||
| limited(burstLimitBody, { | ||
| ...burstHeaders, | ||
| 'Retry-After': '2', | ||
| }), | ||
| ok, | ||
| ]); | ||
|
|
||
| const result = await makeApi(fetch).listCompanies(); | ||
|
|
||
| expect(result).toEqual({ results: [] }); | ||
| expect(delays).toEqual([2_000]); | ||
| expect(fetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('waits the 10 second burst window when the response has no Retry-After', async () => { | ||
| const fetch = throttledFetch([ | ||
| limited(burstLimitBody, burstHeaders), | ||
| ok, | ||
| ]); | ||
|
|
||
| const result = await makeApi(fetch).listCompanies(); | ||
|
|
||
| expect(result).toEqual({ results: [] }); | ||
| expect(delays).toEqual([10_000]); | ||
| expect(fetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('keeps the burst reason and the policy name when a long Retry-After makes it throw', async () => { | ||
| const fetch = throttledFetch([ | ||
| limited(burstLimitBody, { 'Retry-After': '400' }), | ||
| ok, | ||
| ]); | ||
|
|
||
| const error = await makeApi(fetch) | ||
| .listCompanies() | ||
| .catch((e) => e); | ||
|
|
||
| expect(error).toBeInstanceOf(RateLimitError); | ||
| expect(error).toMatchObject({ | ||
| reason: 'burst', | ||
| policy: 'TEN_SECONDLY_ROLLING', | ||
| source: 'header', | ||
| waitMs: 400_000, | ||
| }); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('waits the burst window when the body is not JSON', async () => { | ||
| const fetch = throttledFetch([ | ||
| { | ||
| status: 429, | ||
| contentType: 'text/html', | ||
| text: '<html>Too Many Requests</html>', | ||
| }, | ||
| ok, | ||
| ]); | ||
|
|
||
| await makeApi(fetch).listCompanies(); | ||
|
|
||
| expect(delays).toEqual([10_000]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('a DAILY 429', () => { | ||
| it('throws a RateLimitError that probes again in one hour', async () => { | ||
| const fetch = throttledFetch([limited(dailyLimitBody), ok]); | ||
| const before = Date.now(); | ||
|
|
||
| const error = await makeApi(fetch) | ||
| .listCompanies() | ||
| .catch((e) => e); | ||
|
|
||
| expect(error).toBeInstanceOf(RateLimitError); | ||
| expect(error).toBeInstanceOf(FetchError); | ||
| expect(error).toMatchObject({ | ||
| isRateLimited: true, | ||
| statusCode: 429, | ||
| reason: 'daily', | ||
| policy: 'DAILY', | ||
| source: 'static', | ||
| waitMs: ONE_HOUR_MS, | ||
| module: 'hubspot', | ||
| scopeKey: 'hubspot:entity:7', | ||
| }); | ||
| expect(error.retryAt.getTime()).toBeGreaterThanOrEqual( | ||
| before + ONE_HOUR_MS | ||
| ); | ||
| expect(error.retryAt.getTime()).toBeLessThanOrEqual( | ||
| Date.now() + ONE_HOUR_MS | ||
| ); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| expect(delays).toEqual([]); | ||
| }); | ||
|
|
||
| it('sleeps the probe interval and retries when the in-process budget allows it', async () => { | ||
| class PatientApi extends Api { | ||
| static rateLimit = { | ||
| ...Api.rateLimit, | ||
| maxInProcessWaitMs: ONE_HOUR_MS + 60_000, | ||
| }; | ||
| } | ||
| const fetch = throttledFetch([limited(dailyLimitBody), ok]); | ||
|
|
||
| const result = await makeApi(fetch, PatientApi).listCompanies(); | ||
|
|
||
| expect(result).toEqual({ results: [] }); | ||
| expect(delays).toEqual([ONE_HOUR_MS]); | ||
| expect(fetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('links the daily reason to the HubSpot usage guidelines', () => { | ||
| const { links } = Api.rateLimit.userHints.daily; | ||
|
|
||
| expect(links).toEqual([ | ||
| { | ||
| label: expect.any(String), | ||
| url: 'https://developers.hubspot.com/docs/developer-tooling/platform/usage-guidelines', | ||
| }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('other failures', () => { | ||
| it('does not treat a 403 as a limit', async () => { | ||
| const fetch = throttledFetch([ | ||
| { | ||
| status: 403, | ||
| body: { status: 'error', category: 'MISSING_SCOPES' }, | ||
| }, | ||
| ok, | ||
| ]); | ||
|
|
||
| const error = await makeApi(fetch) | ||
| .listCompanies() | ||
| .catch((e) => e); | ||
|
|
||
| expect(error).toBeInstanceOf(FetchError); | ||
| expect(error).not.toBeInstanceOf(RateLimitError); | ||
| expect(error.statusCode).toBe(403); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| expect(delays).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it('declares its limits', () => { | ||
| expect(Api.rateLimit).toMatchObject({ | ||
| scope: 'entity', | ||
| windows: [{ name: 'burst', limit: 110, perMs: 10_000 }], | ||
| maxConcurrency: 10, | ||
| parsers: ['retryAfter', 'resetHeaders'], | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For calls such as
companySearch,searchDeals, and the other/searchmethods in this class, HubSpot applies a separate limit of five requests per second per account. Declaring only the general 110-per-10-second window permits ten concurrent search requests and uses the 10-second general fallback after the resulting 429, so search-heavy integrations will still be predictably throttled despite this policy. Add a search-specific limiter or otherwise enforce the five-per-second window for these endpoints.Useful? React with 👍 / 👎.