1
1
import crypto from 'crypto'
2
+ import { Agent } from 'https'
3
+
2
4
import got from 'got'
5
+
3
6
import statsd from '../lib/statsd.js'
4
7
import FailBot from '../lib/failbot.js'
5
8
@@ -12,6 +15,21 @@ const TIME_OUT_TEXT = 'ms has passed since batch creation'
12
15
// linger within the thread.
13
16
const POST_TIMEOUT_MS = 3000
14
17
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
+
15
33
export default class Hydro {
16
34
constructor ( { secret, endpoint } = { } ) {
17
35
this . secret = secret || process . env . HYDRO_SECRET
@@ -51,6 +69,8 @@ export default class Hydro {
51
69
} )
52
70
const token = this . generatePayloadHmac ( body )
53
71
72
+ const agent = getHttpsAgent ( )
73
+
54
74
const doPost = ( ) =>
55
75
got ( this . endpoint , {
56
76
method : 'POST' ,
@@ -64,6 +84,11 @@ export default class Hydro {
64
84
throwHttpErrors : false ,
65
85
// The default is no timeout.
66
86
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
+ } ,
67
92
} )
68
93
69
94
const res = await statsd . asyncTimer ( doPost , 'hydro.response_time' ) ( )
0 commit comments