Skip to content

Commit bacabc9

Browse files
committed
updates to reduce rate limiting
1 parent 6e824a8 commit bacabc9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

components/google_calendar/google_calendar.app.mjs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,27 @@ export default {
296296
auth,
297297
});
298298
},
299+
retryWithExponentialBackoff(func, maxAttempts = 3, baseDelayS = 2) {
300+
let attempt = 0;
301+
302+
const execute = async () => {
303+
try {
304+
return await func();
305+
} catch (error) {
306+
if (attempt >= maxAttempts) {
307+
throw error;
308+
}
309+
310+
const delayMs = Math.pow(baseDelayS, attempt) * 1000;
311+
await new Promise((resolve) => setTimeout(resolve, delayMs));
312+
313+
attempt++;
314+
return execute();
315+
}
316+
};
317+
318+
return execute();
319+
},
299320
async requestHandler({
300321
api, method, args = {},
301322
}) {
@@ -305,7 +326,8 @@ export default {
305326
} = args;
306327
try {
307328
const calendar = this.client();
308-
const response = await calendar[api][method](otherArgs);
329+
const fn = () => calendar[api][method](otherArgs);
330+
const response = await this.retryWithExponentialBackoff(fn);
309331
return returnOnlyData
310332
? response.data
311333
: response;
@@ -454,6 +476,9 @@ export default {
454476
args,
455477
});
456478
},
479+
// Used to get the nextSyncToken. Since we don't need
480+
// to actually retrieve any events, we set "updatedMin"
481+
// to the current timestamp
457482
async fullSync(calendarId) {
458483
let nextSyncToken = null;
459484
let nextPageToken = null;
@@ -462,6 +487,8 @@ export default {
462487
await this.listEvents({
463488
calendarId,
464489
pageToken: nextPageToken,
490+
orderBy: "updated",
491+
updatedMin: new Date().toISOString(),
465492
});
466493
nextPageToken = syncResp?.nextPageToken;
467494
nextSyncToken = syncResp?.nextSyncToken;

components/google_calendar/sources/new-or-updated-event-instant/new-or-updated-event-instant.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export default {
265265
calendarId,
266266
syncToken,
267267
pageToken: nextPageToken,
268+
maxResults: 2500,
268269
});
269270
if (syncStatus === 410) {
270271
console.log("Sync token invalid, resyncing");

0 commit comments

Comments
 (0)