node/lib/internal/http.js
Vinicius Lourenço ac75b2eb19
src,lib: expose getCategoryEnabledBuffer to use on node.http
Instead call the C++ code every time we need to check for a
trace category, now we get the C++ pointer to the flag that
holds the info if the trace is enabled and return this pointer
inside a buffer that we can use to call/check if the value is
enabled. With this change, no C++ call is made and the access
to the info happens in JS side, which has no perf penalty.

PR-URL: https://github.com/nodejs/node/pull/53602
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2024-07-16 18:40:22 +00:00

63 lines
1.1 KiB
JavaScript

'use strict';
const {
Date,
Symbol,
} = primordials;
const { setUnrefTimeout } = require('internal/timers');
const { getCategoryEnabledBuffer, trace } = internalBinding('trace_events');
const {
CHAR_LOWERCASE_B,
CHAR_LOWERCASE_E,
} = require('internal/constants');
let utcCache;
function utcDate() {
if (!utcCache) cache();
return utcCache;
}
function cache() {
const d = new Date();
utcCache = d.toUTCString();
setUnrefTimeout(resetCache, 1000 - d.getMilliseconds());
}
function resetCache() {
utcCache = undefined;
}
let traceEventId = 0;
function getNextTraceEventId() {
return ++traceEventId;
}
const httpEnabled = getCategoryEnabledBuffer('node.http');
function isTraceHTTPEnabled() {
return httpEnabled[0] > 0;
}
const traceEventCategory = 'node,node.http';
function traceBegin(...args) {
trace(CHAR_LOWERCASE_B, traceEventCategory, ...args);
}
function traceEnd(...args) {
trace(CHAR_LOWERCASE_E, traceEventCategory, ...args);
}
module.exports = {
kOutHeaders: Symbol('kOutHeaders'),
kNeedDrain: Symbol('kNeedDrain'),
utcDate,
traceBegin,
traceEnd,
getNextTraceEventId,
isTraceHTTPEnabled,
};