Skip to content

Commit

Permalink
fix(logs): Log mailbox lock description
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Mar 19, 2024
1 parent 3451ad2 commit ce87cf5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 46 deletions.
8 changes: 5 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let config = {
},

auth: {
user: 'agustin.huel47@ethereal.email',
pass: 'uyD6qzpstccxXNtTTa'
user: 'jamel8@ethereal.email',
pass: 'SXkR8knUzn6mw1adNd'
},

//proxy: 'socks://localhost:1080',
Expand Down Expand Up @@ -217,11 +217,13 @@ c.connect()
let deleted = await c.mailboxDelete(mboxName);
console.log(deleted);

await c.mailboxOpen('INBOX');
let lock = await c.getMailboxLock('INBOX', { description: 'SELECT' });

console.log('LIST ALL NEXT');
await listAll(c);
await listLast(c);

lock.release();
/*
c.messageFlagsAdd('1:3', ['foo'], { uid: true, useLabels: true });
Expand Down
32 changes: 25 additions & 7 deletions lib/imap-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ class ImapFlow extends EventEmitter {

await new Promise((resolve, reject) => {
this.connectTimeout = setTimeout(() => {
let err = new Error('Failed to established connection in required time');
let err = new Error('Failed to establish connection in required time');
err.code = 'CONNECT_TIMEOUT';
err.details = {
connectionTimeout: this.options.connectionTimeout || CONNECT_TIMEOUT
Expand Down Expand Up @@ -2840,17 +2840,35 @@ class ImapFlow extends EventEmitter {

async processLocks(force) {
if (!force && this.processingLock) {
this.log.trace({
msg: 'Mailbox locking queued',
path: this.mailbox && this.mailbox.path,
pending: this.locks.length,
idling: this.idling
});
return;
}

if (!this.locks.length) {
this.processingLock = false;
this.log.trace({
msg: 'Mailbox locking queue processed',
idling: this.idling
});
return;
}

this.processingLock = true;

const release = () => {
if (this.currentLockId) {
this.log.trace({ msg: 'Mailbox lock released', lockId: this.currentLockId, path: this.mailbox && this.mailbox.path });
this.log.trace({
msg: 'Mailbox lock released',
lockId: this.currentLockId,
path: this.mailbox && this.mailbox.path,
pending: this.locks.length,
idling: this.idling
});
this.currentLockId = 0;
}
this.processLocks(true).catch(err => this.log.error({ err, cid: this.id }));
Expand All @@ -2859,7 +2877,7 @@ class ImapFlow extends EventEmitter {
const { resolve, reject, path, options, lockId } = this.locks.shift();

if (!this.usable || this.socket.destroyed) {
this.log.trace({ msg: 'Failed to acquire mailbox lock', path, lockId });
this.log.trace({ msg: 'Failed to acquire mailbox lock', path, lockId, idling: this.idling });
// reject all
let error = new Error('Connection not available');
error.code = 'NoConnection';
Expand All @@ -2869,14 +2887,14 @@ class ImapFlow extends EventEmitter {

if (this.mailbox && this.mailbox.path === path && !!this.mailbox.readOnly === !!options.readOnly) {
// nothing to do here, already selected
this.log.trace({ msg: 'Mailbox lock acquired', path, lockId });
this.log.trace({ msg: 'Mailbox lock acquired', path, lockId, idling: this.idling });
this.currentLockId = lockId;
return resolve({ path, release });
} else {
try {
// Try to open. Throws if mailbox does not exists or can't open
await this.mailboxOpen(path, options);
this.log.trace({ msg: 'Lock acquired', path, lockId });
this.log.trace({ msg: 'Lock acquired', path, lockId, idling: this.idling, ...(options.description && { description: options.description }) });
this.currentLockId = lockId;
return resolve({ path, release });
} catch (err) {
Expand All @@ -2891,7 +2909,7 @@ class ImapFlow extends EventEmitter {
}
}

this.log.trace({ msg: 'Failed to acquire mailbox lock', path, lockId });
this.log.trace({ msg: 'Failed to acquire mailbox lock', path, lockId, idling: this.idling });
reject(err);
await this.processLocks(true);
}
Expand Down Expand Up @@ -2925,7 +2943,7 @@ class ImapFlow extends EventEmitter {
path = normalizePath(this, path);

let lockId = ++this.lockCounter;
this.log.trace({ msg: 'Requesting lock', path, lockId });
this.log.trace({ msg: 'Requesting lock', path, lockId, ...(options.description && { description: options.description }) });

return await new Promise((resolve, reject) => {
this.locks.push({ resolve, reject, path, options, lockId });
Expand Down
103 changes: 69 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@babel/eslint-plugin": "7.23.5",
"@babel/plugin-syntax-class-properties": "7.12.13",
"@babel/preset-env": "7.24.0",
"@types/node": "20.11.24",
"@types/node": "20.11.29",
"eslint": "8.57.0",
"eslint-config-nodemailer": "1.2.0",
"eslint-config-prettier": "9.1.0",
Expand All @@ -52,7 +52,7 @@
"libmime": "5.3.4",
"libqp": "2.1.0",
"mailsplit": "5.4.0",
"nodemailer": "6.9.11",
"nodemailer": "6.9.12",
"pino": "8.19.0",
"socks": "2.8.1"
}
Expand Down

0 comments on commit ce87cf5

Please sign in to comment.