Skip to content

Commit f820885

Browse files
authored
Merge pull request github#18289 from github/repo-sync
repo sync
2 parents 68680e4 + d371a19 commit f820885

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/hydro.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import crypto from 'crypto'
2+
import { Agent } from 'https'
3+
24
import got from 'got'
5+
36
import statsd from '../lib/statsd.js'
47
import FailBot from '../lib/failbot.js'
58

@@ -12,6 +15,21 @@ const TIME_OUT_TEXT = 'ms has passed since batch creation'
1215
// linger within the thread.
1316
const POST_TIMEOUT_MS = 3000
1417

18+
let _agent
19+
function getHttpsAgent() {
20+
if (!_agent) {
21+
const agentOptions = {
22+
// The most important option. This is false by default.
23+
keepAlive: true,
24+
// 32 because it's what's recommended here
25+
// https://docs.microsoft.com/en-us/azure/app-service/app-service-web-nodejs-best-practices-and-troubleshoot-guide#my-node-application-is-making-excessive-outbound-calls
26+
maxSockets: 32,
27+
}
28+
_agent = new Agent(agentOptions)
29+
}
30+
return _agent
31+
}
32+
1533
export default class Hydro {
1634
constructor({ secret, endpoint } = {}) {
1735
this.secret = secret || process.env.HYDRO_SECRET
@@ -51,6 +69,8 @@ export default class Hydro {
5169
})
5270
const token = this.generatePayloadHmac(body)
5371

72+
const agent = getHttpsAgent()
73+
5474
const doPost = () =>
5575
got(this.endpoint, {
5676
method: 'POST',
@@ -64,6 +84,11 @@ export default class Hydro {
6484
throwHttpErrors: false,
6585
// The default is no timeout.
6686
timeout: POST_TIMEOUT_MS,
87+
agent: {
88+
// Deliberately not setting up a `http` or `http2` agent
89+
// because it won't be used for this particular `got` request.
90+
https: agent,
91+
},
6792
})
6893

6994
const res = await statsd.asyncTimer(doPost, 'hydro.response_time')()

0 commit comments

Comments
 (0)