From 2e36cadce4635e2c1fe19e7965be86db4a54724f Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:25:31 -0400 Subject: [PATCH] fix: Add jitter when Outlook requests are throttled (#16484) --- .../office365calendar/lib/CalendarService.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/app-store/office365calendar/lib/CalendarService.ts b/packages/app-store/office365calendar/lib/CalendarService.ts index 359a6ad14a..47e6ed5b0a 100644 --- a/packages/app-store/office365calendar/lib/CalendarService.ts +++ b/packages/app-store/office365calendar/lib/CalendarService.ts @@ -174,10 +174,13 @@ export default class Office365CalendarService implements Calendar { const calendarSelectParams = "$select=showAs,start,end"; try { - const selectedCalendarIds = selectedCalendars - .filter((e) => e.integration === this.integrationName) - .map((e) => e.externalId) - .filter(Boolean); + const selectedCalendarIds = selectedCalendars.reduce((calendarIds, calendar) => { + if (calendar.integration === this.integrationName && calendar.externalId) + calendarIds.push(calendar.externalId); + + return calendarIds; + }, [] as string[]); + if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) { // Only calendars of other integrations selected return Promise.resolve([]); @@ -373,6 +376,7 @@ export default class Office365CalendarService implements Calendar { maxRetries: number, retryCount = 0 ): Promise => { + const getRandomness = () => Number(Math.random().toFixed(3)); let retryAfterTimeout = 0; if (retryCount >= maxRetries) { return { responses: settledPromises }; @@ -394,7 +398,7 @@ export default class Office365CalendarService implements Calendar { } // Await certain time from retry-after header - await new Promise((r) => setTimeout(r, retryAfterTimeout)); + await new Promise((r) => setTimeout(r, retryAfterTimeout + getRandomness())); const newResponses = await this.apiGraphBatchCall(failedRequest); let newResponseBody = await handleErrorsJson(newResponses);