Skip to content

Commit

Permalink
fix memory leak using keep-alive in nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
notanengineercom committed Nov 28, 2023
1 parent 2293e2d commit 90cc28b
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/gateway/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as url from 'url'
import * as http from 'http'
import * as https from 'https'
import { Socket } from 'net'

import { assign } from '../utils/index'
import { Gateway } from './gateway'
Expand Down Expand Up @@ -96,21 +97,13 @@ export class HTTP extends Gateway {
httpOptions.onRequestSocketAssigned(requestParams)
}

socket.on('lookup', () => {
if (httpOptions.onSocketLookup) {
httpOptions.onSocketLookup(requestParams)
}
})
socket.on('connect', () => {
if (httpOptions.onSocketConnect) {
httpOptions.onSocketConnect(requestParams)
}
})
socket.on('secureConnect', () => {
if (httpOptions.onSocketSecureConnect) {
httpOptions.onSocketSecureConnect(requestParams)
}
})
if (httpRequest.reusedSocket) {
return
}

this.assignSocketListener(socket, 'lookup', httpOptions.onSocketLookup?.bind(null, requestParams))
this.assignSocketListener(socket, 'connect', httpOptions.onSocketConnect?.bind(null, requestParams))
this.assignSocketListener(socket, 'secureConnect', httpOptions.onSocketSecureConnect?.bind(null, requestParams))
})

httpRequest.on('error', (e) => this.onError(e))
Expand All @@ -132,6 +125,12 @@ export class HTTP extends Gateway {
httpRequest.end()
}

assignSocketListener(socket: Socket, event: 'lookup' | 'connect' | 'secureConnect', listener: (() => void) | null | undefined) {
if (typeof listener === 'function') {
socket.on(event, () => listener())
}
}

onResponse(
httpResponse: http.IncomingMessage,
httpOptions: Partial<HTTPGatewayConfiguration>,
Expand Down

0 comments on commit 90cc28b

Please sign in to comment.