mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
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:
parent
9db3230e97
commit
8a15040f04
1 changed files with 37 additions and 36 deletions
|
@ -15,26 +15,19 @@ const MIN_SOCKET_LEAK_TIME =
|
|||
(parseInt(process.env.LEAKED_SOCKET_AGE_THRESHOLD, 10) || 15) * 60 * 1000
|
||||
|
||||
// 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 = [
|
||||
'http.client.request.start',
|
||||
'http.client.response.finish',
|
||||
'http.server.request.start',
|
||||
'http.server.response.finish',
|
||||
]
|
||||
|
||||
for (const channel of channels) {
|
||||
diagnosticsChannel.subscribe(channel, handler(channel))
|
||||
function handleRequest({ request: req }) {
|
||||
const socket = req?.socket
|
||||
if (socket) {
|
||||
recordRequest(req, socket)
|
||||
}
|
||||
}
|
||||
|
||||
function handler(channel) {
|
||||
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) {
|
||||
function recordRequest(req, socket) {
|
||||
const { method, protocol, path, url, rawHeaders, _header } = req
|
||||
socket._ol_debug = {
|
||||
method,
|
||||
|
@ -42,8 +35,18 @@ function handler(channel) {
|
|||
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
|
||||
}
|
||||
|
||||
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: {
|
||||
|
@ -54,8 +57,6 @@ function handler(channel) {
|
|||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Additional functions to log request headers with sensitive information redacted
|
||||
|
||||
|
|
Loading…
Reference in a new issue