Skip to content

Commit 6b81f8c

Browse files
committed
Adding retrying on 429 response to search endpoint
1 parent d6a9fa1 commit 6b81f8c

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

components/hubspot/hubspot.app.mjs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "./common/constants.mjs";
1616
import Bottleneck from "bottleneck";
1717
const limiter = new Bottleneck({
18-
minTime: 250, // 4 requests per second
18+
minTime: 500, // 2 requests per second
1919
maxConcurrent: 1,
2020
});
2121
const axiosRateLimiter = limiter.wrap(axios);
@@ -694,15 +694,35 @@ export default {
694694
value: unit.id,
695695
})) || [];
696696
},
697-
searchCRM({
697+
async searchCRM({
698698
object, ...opts
699699
}) {
700-
return this.makeRequest({
701-
api: API_PATH.CRMV3,
702-
method: "POST",
703-
endpoint: `/objects/${object}/search`,
704-
...opts,
705-
});
700+
// Adding retry logic here since the search endpoint specifically has a per-second rate limit
701+
const MAX_RETRIES = 5;
702+
const BASE_RETRY_DELAY = 500;
703+
let success = false;
704+
let retries = 0;
705+
while (!success) {
706+
try {
707+
const response = await this.makeRequest({
708+
api: API_PATH.CRMV3,
709+
method: "POST",
710+
endpoint: `/objects/${object}/search`,
711+
...opts,
712+
});
713+
return response;
714+
} catch (error) {
715+
if (error.status === 429 && ++retries < MAX_RETRIES) {
716+
// Retry delays basically amount to:
717+
// 1000-1500 ms, 2000-2500 ms, 4000-4500 ms, 8000-8500 ms
718+
const randomDelay = Math.floor(Math.random() * BASE_RETRY_DELAY);
719+
const delay = BASE_RETRY_DELAY * (2 ** retries) + randomDelay;
720+
await new Promise((resolve) => setTimeout(resolve, delay));
721+
} else {
722+
throw error;
723+
}
724+
}
725+
}
706726
},
707727
getBlogPosts(opts = {}) {
708728
return this.makeRequest({

components/hubspot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/hubspot",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "Pipedream Hubspot Components",
55
"main": "hubspot.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)