-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up socket in case of error (#98)
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const http = require('http') | ||
const { createServer, SERVER_HOSTNAME } = require('../utils') | ||
const { HttpProxyAgent } = require('../../') | ||
|
||
function request (opts) { | ||
return new Promise((resolve, reject) => { | ||
const req = http.request(opts, resolve) | ||
req.on('error', reject) | ||
req.end(opts.body) | ||
}) | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
console.log('The http agent is not cleaning up hanging sockets') | ||
process.exit(1) | ||
}, 5000) | ||
|
||
async function run () { | ||
const server = await createServer() | ||
server.on('connect', (request, socket, head) => { | ||
socket.on('end', () => { | ||
clearTimeout(timeout) | ||
}) | ||
const lines = [ | ||
'HTTP/1.1 403 FORBIDDEN', | ||
'', | ||
'Forbidden' | ||
] | ||
socket.write(lines.join('\r\n')) | ||
}) | ||
|
||
const agent = new HttpProxyAgent({ | ||
keepAlive: true, | ||
keepAliveMsecs: 1000, | ||
maxSockets: 256, | ||
maxFreeSockets: 256, | ||
scheduling: 'lifo', | ||
timeout: 500, | ||
proxy: `http://${SERVER_HOSTNAME}:${server.address().port}` | ||
}) | ||
|
||
try { | ||
await request({ | ||
method: 'GET', | ||
hostname: 'www.example.com', | ||
port: '', | ||
path: '/', | ||
agent | ||
}) | ||
console.error(new Error('Should throw')) | ||
process.exit(1) | ||
} catch (err) { | ||
if (err.message !== 'Bad response: 403') { | ||
console.error(new Error('Expected a different error')) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
server.close() | ||
} | ||
|
||
run().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const https = require('https') | ||
const { createSecureServer, SERVER_HOSTNAME } = require('../utils') | ||
const { HttpsProxyAgent } = require('../../') | ||
|
||
function request (opts) { | ||
return new Promise((resolve, reject) => { | ||
const req = https.request(opts, resolve) | ||
req.on('error', reject) | ||
req.end(opts.body) | ||
}) | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
console.log('The https agent is not cleaning up hanging sockets') | ||
process.exit(1) | ||
}, 5000) | ||
|
||
async function run () { | ||
const server = await createSecureServer() | ||
server.on('connect', (request, socket, head) => { | ||
socket.on('end', () => { | ||
clearTimeout(timeout) | ||
}) | ||
const lines = [ | ||
'HTTP/1.1 403 FORBIDDEN', | ||
'', | ||
'Forbidden' | ||
] | ||
socket.write(lines.join('\r\n')) | ||
}) | ||
|
||
const agent = new HttpsProxyAgent({ | ||
keepAlive: true, | ||
keepAliveMsecs: 1000, | ||
maxSockets: 256, | ||
maxFreeSockets: 256, | ||
scheduling: 'lifo', | ||
timeout: 500, | ||
proxy: `https://${SERVER_HOSTNAME}:${server.address().port}` | ||
}) | ||
|
||
try { | ||
await request({ | ||
method: 'GET', | ||
hostname: 'www.example.com', | ||
port: '', | ||
path: '/', | ||
agent | ||
}) | ||
console.error(new Error('Should throw')) | ||
process.exit(1) | ||
} catch (err) { | ||
if (err.message !== 'Bad response: 403') { | ||
console.error(new Error('Expected a different error, got:', err.message)) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
server.close() | ||
} | ||
|
||
run().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
node test/hang-socket/http.js | ||
node test/hang-socket/https.js |