Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add-connect-timeout-support #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const agent = new HttpProxyAgent({
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: 'http://localhost:8080'
proxy: 'http://localhost:8080',
// proxy connect timeout in milliseconds
timeout: 1000,
})

http.get('http://localhost:9200', { agent })
Expand Down
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class HttpProxyAgent extends http.Agent {
path: `${options.host}:${options.port}`,
setHost: false,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` },
agent: false
agent: false,
timeout: options.timeout,
}

if (this.proxy.username || this.proxy.password) {
Expand All @@ -40,6 +41,12 @@ class HttpProxyAgent extends http.Agent {
}
})

request.once('timeout', () => {
const e = new Error(`connect proxy ${this.proxy.toString()} timeout`)
e.code = 'EPROXYTIMEOUT'
request.destroy(e);
})

request.once('error', err => {
request.removeAllListeners()
callback(err, null)
Expand All @@ -66,7 +73,8 @@ class HttpsProxyAgent extends https.Agent {
path: `${options.host}:${options.port}`,
setHost: false,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` },
agent: false
agent: false,
timeout: options.timeout,
}

if (this.proxy.username || this.proxy.password) {
Expand All @@ -86,6 +94,12 @@ class HttpsProxyAgent extends https.Agent {
}
})

request.once('timeout', () => {
const e = new Error(`connect proxy ${this.proxy.toString()} timeout`)
e.code = 'EPROXYTIMEOUT'
request.destroy(e);
})

request.once('error', err => {
request.removeAllListeners()
callback(err, null)
Expand Down
25 changes: 25 additions & 0 deletions test/got.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,28 @@ test('https to https', async t => {
server.close()
proxy.close()
})

test('timeout', async t => {
const server = await createSecureServer()
server.on('request', (req, res) => res.end('ok'))

try {
await got(`https://${server.address().address}:${server.address().port}`, {
agent: {
https: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://1.1.1.1:1111`,
timeout: 100,
})
}
})
} catch (e) {
t.is(e.code, 'EPROXYTIMEOUT')
}

server.close()
})