Skip to content

Commit

Permalink
fix(connection): Do not wait for subconnections when setting up the c…
Browse files Browse the repository at this point in the history
…onnection
  • Loading branch information
andris9 committed Mar 20, 2024
1 parent c5c5981 commit d8daff8
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class IMAPConnection extends BaseClient {
const connectionClient = await this.getCommandConnection(reason);
return connectionClient && connectionClient.usable ? connectionClient : this.imapClient;
} catch (err) {
this.logger.error({ msg: 'Failed to acquire command connection', reason, err });
return this.imapClient;
}
}
Expand All @@ -188,11 +189,13 @@ class IMAPConnection extends BaseClient {
let lockKey = ['commandCient', this.account].join(':');

try {
this.logger.debug({ msg: 'Acquiring connection lock', lockKey });
connectLock = await lock.waitAcquireLock(lockKey, 5 * 60 * 1000, 1 * 60 * 1000);
if (!connectLock.success) {
this.logger.error({ msg: 'Failed to get lock', lockKey });
throw new Error('Failed to get connection lock');
}
this.logger.debug({ msg: 'Acquired connection lock', lockKey, index: connectLock.index });
} catch (err) {
this.logger.error({ msg: 'Failed to get lock', lockKey, err });
throw err;
Expand All @@ -206,6 +209,11 @@ class IMAPConnection extends BaseClient {
return null;
}

if (this.commandClient && this.commandClient.usable) {
// use existing command channel created during the lock
return this.commandClient;
}

const commandCid = `${this.cid}:c:${this.connectionCount++}`;

let imapConfig = await this.getImapConfig(accountData);
Expand Down Expand Up @@ -255,7 +263,9 @@ class IMAPConnection extends BaseClient {

return commandClient;
} finally {
this.logger.debug({ msg: 'Releasing connection lock', lockKey, index: connectLock.index });
await lock.releaseLock(connectLock);
this.logger.debug({ msg: 'Released connection lock', lockKey, index: connectLock.index });
}
}

Expand Down Expand Up @@ -1337,7 +1347,13 @@ class IMAPConnection extends BaseClient {
this.logger.info({ msg: 'Successful login with a previous active session', account: this.account, isiInitial, prevActive: true });
}

await this.setupSubConnections();
this.setupSubConnections()
.then(result => {
this.logger.info({ msg: 'Set up subconnections', account: this.account, result });
})
.catch(err => {
this.logger.error({ msg: 'Failed to set up subconnections', account: this.account, err });
});
} catch (err) {
if (err.oauthError && err.oauthError.status === 'invalid_request') {
// access token is invalid, clear it
Expand Down Expand Up @@ -3560,7 +3576,7 @@ class IMAPConnection extends BaseClient {

if (!accountData.subconnections?.length && !this.subconnections.length) {
// Nothing to do here
return;
return null;
}

const mailboxes = [];
Expand Down Expand Up @@ -3686,6 +3702,8 @@ class IMAPConnection extends BaseClient {

await subconnection.init();
}

return this.subconnections.length;
}

closeSubconnections() {
Expand Down

0 comments on commit d8daff8

Please sign in to comment.