Skip to content

Commit 38e2e18

Browse files
committed
fix: bug when releasing connection
1 parent 95b28e2 commit 38e2e18

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

micro-services/emails-fetcher/src/services/imap/ImapEmailsFetcher.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,9 @@ export default class ImapEmailsFetcher {
170170
err.code === 'bad_jwt' ||
171171
err.name === 'AuthApiError';
172172

173-
// 3. Check for connection errors that might require re-authentication
174-
const isConnectionErrorRequiringAuth =
175-
err.code === 'ECONNRESET' ||
176-
err.code === 'NoConnection' ||
177-
(typeof err.message === 'string' &&
178-
err.message.includes('Connection not available'));
179-
180-
return isImapAuthError || isTokenExpired || isConnectionErrorRequiringAuth;
173+
return isImapAuthError || isTokenExpired;
181174
}
182175

183-
private setOAuthRefreshCooldown(seconds = 120) {
184-
this.isRefreshingOAuthToken = true;
185-
setTimeout(() => {
186-
this.isRefreshingOAuthToken = false;
187-
logger.debug('OAuth error flag reset - ready for future auth checks');
188-
}, seconds * 1000);
189-
}
190176

191177
/**
192178
* Fetches the total number of messages across the specified folders on an IMAP server.
@@ -488,12 +474,13 @@ export default class ImapEmailsFetcher {
488474
`[${this.miningId}:${folder}:${range}:${connection?.id}]: Closed folder for range ${range}`
489475
);
490476

491-
if (this.isRefreshingOAuthToken) {
477+
if (this.isRefreshingOAuthToken || !connection.usable) {
492478
// A pending task that is completed (success or failure) , while we're refreshing token.
493479
// Destroy the connection, since it’s using an expired OAuth token.
494480
await this.imapConnectionProvider.destroyConnection(connection);
481+
} else {
482+
await this.imapConnectionProvider.releaseConnection(connection);
495483
}
496-
await this.imapConnectionProvider.releaseConnection(connection);
497484
} catch (error) {
498485
logger.error(
499486
`[${this.miningId}:${folder}:${range}:${connection?.id}]: ${(error as Error).message}`,
@@ -527,8 +514,7 @@ export default class ImapEmailsFetcher {
527514
// Reset total errors
528515
this.FETCHING_TOTAL_ERRORS = 0;
529516

530-
// to avoid refreshing pool on every connection
531-
this.setOAuthRefreshCooldown();
517+
this.isRefreshingOAuthToken = true;
532518

533519
logger.warn(`Has Auth Error & is Refreshing OAuth token at ${range}`);
534520

@@ -540,7 +526,13 @@ export default class ImapEmailsFetcher {
540526
await new Promise((r) => setTimeout(r, 50));
541527
}
542528
const success = await this.imapConnectionProvider.refreshOauth();
543-
this.isCanceled = !success; // cancel task if it fails to refresh
529+
530+
if (!success) {
531+
this.isCanceled = !success; // cancel task if it fails to refresh
532+
} else {
533+
// to avoid refreshing pool on every connection
534+
this.isRefreshingOAuthToken = false;
535+
}
544536
this.emailsQueue.start();
545537
}
546538
}

0 commit comments

Comments
 (0)