Skip to content
Merged
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
25 changes: 25 additions & 0 deletions lib/hydro.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import crypto from 'crypto'
import { Agent } from 'https'

import got from 'got'

import statsd from '../lib/statsd.js'
import FailBot from '../lib/failbot.js'

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

let _agent
function getHttpsAgent() {
if (!_agent) {
const agentOptions = {
// The most important option. This is false by default.
keepAlive: true,
// 32 because it's what's recommended here
// 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
maxSockets: 32,
}
_agent = new Agent(agentOptions)
}
return _agent
}

export default class Hydro {
constructor({ secret, endpoint } = {}) {
this.secret = secret || process.env.HYDRO_SECRET
Expand Down Expand Up @@ -51,6 +69,8 @@ export default class Hydro {
})
const token = this.generatePayloadHmac(body)

const agent = getHttpsAgent()

const doPost = () =>
got(this.endpoint, {
method: 'POST',
Expand All @@ -64,6 +84,11 @@ export default class Hydro {
throwHttpErrors: false,
// The default is no timeout.
timeout: POST_TIMEOUT_MS,
agent: {
// Deliberately not setting up a `http` or `http2` agent
// because it won't be used for this particular `got` request.
https: agent,
},
})

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