Skip to content

Commit

Permalink
Close connections at server closure
Browse files Browse the repository at this point in the history
  • Loading branch information
yassernasc committed Jul 18, 2024
1 parent 11eceb2 commit f8951fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = class HTTPAgent {

socket.on('free', () => this._onfree(socket, name))
socket.on('close', () => this._onremove(socket, name))
socket.on('end', () => socket.destroy())
}

let sockets = this._sockets.get(name)
Expand Down
11 changes: 9 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ module.exports = class HTTPServer extends TCPServer {

setTimeout (ms = 0, ontimeout) {
if (ontimeout) this.on('timeout', ontimeout)

this._timeout = ms

return this
}

close () {
this._closeConnections()
super.close()
}

_closeConnections () {
for (const socket of this._connections) socket.end()
}
}
26 changes: 11 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ test('basic', async function (t) {
host: server.address().address,
port: server.address().port,
path: '/something/?key1=value1&key2=value2&enabled',
headers: { 'Content-Length': 12 },
agent: false
headers: { 'Content-Length': 12 }
}, (req) => {
req.write('body message')
req.end()
Expand Down Expand Up @@ -193,8 +192,7 @@ test('write head', async function (t) {
method: 'GET',
host: server.address().address,
port: server.address().port,
path: '/',
agent: false
path: '/'
})

t.absent(reply.error)
Expand Down Expand Up @@ -232,8 +230,7 @@ test('write head with headers', async function (t) {
method: 'GET',
host: server.address().address,
port: server.address().port,
path: '/',
agent: false
path: '/'
})

t.absent(reply.error)
Expand Down Expand Up @@ -278,8 +275,7 @@ test('chunked', async function (t) {
method: 'POST',
host: server.address().address,
port: server.address().port,
path: '/',
agent: false
path: '/'
}, (req) => {
req.write('request body part 1 + ')
setImmediate(() => { req.end('request body part 2') })
Expand Down Expand Up @@ -488,11 +484,11 @@ test('make requests using url', async function (t) {
const url = `http://localhost:${server.address().port}/path`
const expectedBuf = Buffer.from('response')

http.request(url, { agent: false }, res => {
http.request(url, res => {
res.on('data', (data) => rqts.alike(data, expectedBuf, 'url as string'))
}).end()

http.request(new URL(url), { agent: false }, res => {
http.request(new URL(url), res => {
res.on('data', (data) => rqts.alike(data, expectedBuf, 'url instance'))
}).end()

Expand All @@ -514,7 +510,7 @@ test('custom request headers', async function (t) {
})

const { port } = server.address()
http.request({ port, headers: { 'custom-header': 'value' }, agent: false }).end()
http.request({ port, headers: { 'custom-header': 'value' } }).end()

await ht

Expand All @@ -532,7 +528,7 @@ test('request timeout', async function (t) {

await waitForServer(server)

const client = http.request({ port: server.address().port, agent: false }).end()
const client = http.request({ port: server.address().port }).end()

client.setTimeout(100, () => sub.pass('callback'))
client.on('timeout', () => sub.pass('event'))
Expand Down Expand Up @@ -562,7 +558,7 @@ test('server timeout', async function (t) {
await waitForServer(server)

const { port } = server.address()
const req = http.request({ port, agent: false })
const req = http.request({ port })

await sub

Expand Down Expand Up @@ -601,7 +597,7 @@ test('do not close the server at timeout if a handler is found', async function

await waitForServer(server)

http.request({ port: server.address().port, agent: false }).end()
http.request({ port: server.address().port }).end()
})

test('server response timeout', async function (t) {
Expand All @@ -618,7 +614,7 @@ test('server response timeout', async function (t) {

await waitForServer(server)

http.request({ port: server.address().port, agent: false }).end()
http.request({ port: server.address().port }).end()

await sub

Expand Down

0 comments on commit f8951fd

Please sign in to comment.