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,26 +15,19 @@ 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 socket = req?.socket || res?.socket
if (!socket) {
return
}
// If we haven't seen this socket before, add a debug object from the request
if (!socket._ol_debug && req) {
const { method, protocol, path, url, rawHeaders, _header } = req const { method, protocol, path, url, rawHeaders, _header } = req
socket._ol_debug = { socket._ol_debug = {
method, method,
@ -42,8 +35,18 @@ function handler(channel) {
url: url ?? path, url: url ?? path,
request: { headers: rawHeaders ?? _header, ts: new Date() }, 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
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 const { statusCode, statusMessage, headers, _header } = res
Object.assign(socket._ol_debug, { Object.assign(socket._ol_debug, {
response: { response: {
@ -54,8 +57,6 @@ function handler(channel) {
}, },
}) })
} }
}
}
// Additional functions to log request headers with sensitive information redacted // Additional functions to log request headers with sensitive information redacted