Merge pull request #13295 from overleaf/em-socket-leak-detection-pools

Record the last request in socket leak detection

GitOrigin-RevId: 376757e608358a7299e7ad7e327ed4fbac960d83
This commit is contained in:
Eric Mc Sween 2023-06-05 08:03:51 -04:00 committed by Copybot
parent 9db3230e97
commit 8a15040f04

View file

@ -15,48 +15,49 @@ const MIN_SOCKET_LEAK_TIME =
(parseInt(process.env.LEAKED_SOCKET_AGE_THRESHOLD, 10) || 15) * 60 * 1000 (parseInt(process.env.LEAKED_SOCKET_AGE_THRESHOLD, 10) || 15) * 60 * 1000
// Record HTTP events using diagnostics_channel // Record HTTP events using diagnostics_channel
diagnosticsChannel.subscribe('http.client.request.start', handleRequest)
diagnosticsChannel.subscribe('http.server.request.start', handleRequest)
diagnosticsChannel.subscribe('http.client.response.finish', handleResponse)
diagnosticsChannel.subscribe('http.server.response.finish', handleResponse)
const channels = [ function handleRequest({ request: req }) {
'http.client.request.start', const socket = req?.socket
'http.client.response.finish', if (socket) {
'http.server.request.start', recordRequest(req, socket)
'http.server.response.finish', }
]
for (const channel of channels) {
diagnosticsChannel.subscribe(channel, handler(channel))
} }
function handler(channel) { function recordRequest(req, socket) {
return function ({ request: req, response: res }) { const { method, protocol, path, url, rawHeaders, _header } = req
const socket = req?.socket || res?.socket socket._ol_debug = {
if (!socket) { method,
return protocol,
} url: url ?? path,
// If we haven't seen this socket before, add a debug object from the request request: { headers: rawHeaders ?? _header, ts: new Date() },
if (!socket._ol_debug && req) {
const { method, protocol, path, url, rawHeaders, _header } = req
socket._ol_debug = {
method,
protocol,
url: url ?? path,
request: { headers: rawHeaders ?? _header, ts: new Date() },
}
} else if (socket._ol_debug && res) {
// We've already seen the request, now add debug info from the response
const { statusCode, statusMessage, headers, _header } = res
Object.assign(socket._ol_debug, {
response: {
statusCode,
statusMessage,
headers: headers ?? _header,
ts: new Date(),
},
})
}
} }
} }
function handleResponse({ request: req, response: res }) {
const socket = req?.socket || res?.socket
if (!socket || !res) {
return
}
if (!socket._ol_debug) {
// I don't know if this will ever happen, but if we missed the request,
// record it here.
recordRequest(req, socket)
}
const { statusCode, statusMessage, headers, _header } = res
Object.assign(socket._ol_debug, {
response: {
statusCode,
statusMessage,
headers: headers ?? _header,
ts: new Date(),
},
})
}
// Additional functions to log request headers with sensitive information redacted // Additional functions to log request headers with sensitive information redacted
function flattenHeaders(rawHeaders) { function flattenHeaders(rawHeaders) {