diagnostics_channel: add diagnostics channels for web locks · nodejs/node@231521e · GitHub
Skip to content

Commit 231521e

Browse files
IlyasShabiaduh95
authored andcommitted
diagnostics_channel: add diagnostics channels for web locks
PR-URL: #62123 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent a9703d1 commit 231521e

3 files changed

Lines changed: 270 additions & 3 deletions

File tree

doc/api/diagnostics_channel.md

Lines changed: 46 additions & 0 deletions

lib/internal/locks.js

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ const {
2929
createEnumConverter,
3030
createDictionaryConverter,
3131
} = require('internal/webidl');
32+
const dc = require('diagnostics_channel');
3233

3334
const locks = internalBinding('locks');
35+
const lockRequestStartChannel = dc.channel('locks.request.start');
36+
const lockRequestGrantChannel = dc.channel('locks.request.grant');
37+
const lockRequestMissChannel = dc.channel('locks.request.miss');
38+
const lockRequestEndChannel = dc.channel('locks.request.end');
3439

3540
const kName = Symbol('kName');
3641
const kMode = Symbol('kMode');
@@ -113,6 +118,30 @@ function convertLockError(error) {
113118
return error;
114119
}
115120

121+
function publishLockRequestStart(name, mode) {
122+
if (lockRequestStartChannel.hasSubscribers) {
123+
lockRequestStartChannel.publish({ name, mode });
124+
}
125+
}
126+
127+
function publishLockRequestGrant(name, mode) {
128+
if (lockRequestGrantChannel.hasSubscribers) {
129+
lockRequestGrantChannel.publish({ name, mode });
130+
}
131+
}
132+
133+
function publishLockRequestMiss(name, mode, ifAvailable) {
134+
if (ifAvailable && lockRequestMissChannel.hasSubscribers) {
135+
lockRequestMissChannel.publish({ name, mode });
136+
}
137+
}
138+
139+
function publishLockRequestEnd(name, mode, ifAvailable, steal, error) {
140+
if (lockRequestEndChannel.hasSubscribers) {
141+
lockRequestEndChannel.publish({ name, mode, ifAvailable, steal, error });
142+
}
143+
}
144+
116145
// https://w3c.github.io/web-locks/#api-lock-manager
117146
class LockManager {
118147
constructor(symbol = undefined) {
@@ -192,6 +221,7 @@ class LockManager {
192221
}
193222

194223
const clientId = `node-${process.pid}-${threadId}`;
224+
publishLockRequestStart(name, mode);
195225

196226
// Handle requests with AbortSignal
197227
if (signal) {
@@ -212,6 +242,8 @@ class LockManager {
212242
return undefined;
213243
}
214244
lockGranted = true;
245+
publishLockRequestGrant(name, mode);
246+
215247
return callback(createLock(lock));
216248
});
217249
};
@@ -228,27 +260,49 @@ class LockManager {
228260

229261
// When released promise settles, clean up listener and resolve main promise
230262
SafePromisePrototypeFinally(
231-
PromisePrototypeThen(released, resolve, (error) => reject(convertLockError(error))),
263+
PromisePrototypeThen(
264+
released,
265+
(result) => {
266+
publishLockRequestEnd(name, mode, ifAvailable, steal, undefined);
267+
resolve(result);
268+
},
269+
(error) => {
270+
const convertedError = convertLockError(error);
271+
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
272+
reject(convertedError);
273+
},
274+
),
232275
() => signal.removeEventListener('abort', abortListener),
233276
);
234277
} catch (error) {
235278
signal.removeEventListener('abort', abortListener);
236-
reject(convertLockError(error));
279+
const convertedError = convertLockError(error);
280+
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
281+
reject(convertedError);
237282
}
238283
});
239284
}
240285

241286
// When ifAvailable: true and lock is not available, C++ passes null to indicate no lock granted
242287
const wrapCallback = (internalLock) => {
288+
if (internalLock === null) {
289+
publishLockRequestMiss(name, mode, ifAvailable);
290+
} else {
291+
publishLockRequestGrant(name, mode);
292+
}
243293
const lock = createLock(internalLock);
244294
return callback(lock);
245295
};
246296

247297
// Standard request without signal
248298
try {
249-
return await locks.request(name, clientId, mode, steal, ifAvailable, wrapCallback);
299+
const result = await locks.request(name, clientId, mode, steal, ifAvailable, wrapCallback);
300+
publishLockRequestEnd(name, mode, ifAvailable, steal, undefined);
301+
302+
return result;
250303
} catch (error) {
251304
const convertedError = convertLockError(error);
305+
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
252306
throw convertedError;
253307
}
254308
}
Lines changed: 167 additions & 0 deletions

0 commit comments

Comments
 (0)