@@ -15,7 +15,7 @@ import {
1515} from "./common/constants.mjs" ;
1616import Bottleneck from "bottleneck" ;
1717const limiter = new Bottleneck ( {
18- minTime : 250 , // 4 requests per second
18+ minTime : 500 , // 2 requests per second
1919 maxConcurrent : 1 ,
2020} ) ;
2121const 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 ( {
0 commit comments