Skip to content

Commit

Permalink
Add http keepalive setting for more efficient local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Sep 10, 2023
1 parent a006532 commit b13faa6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/client-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function clientFactory (config, creds, region) {
async function client (params = {}) {
let selectedRegion = params.region || region
try {
return await request(params, creds, selectedRegion)
return await request(params, creds, selectedRegion, config)
}
catch (err) {
errorHandler(err)
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = function clientFactory (config, creds, region) {
let result = await method.request(params)
let metadata = { service, name }
try {
return await request({ ...params, ...result }, creds, selectedRegion, metadata)
return await request({ ...params, ...result }, creds, selectedRegion, config, metadata)
}
catch (err) {
if (method.error) {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let clientFactory = require('./client-factory')
* @param {string} [config.region] AWS service region (e.g. `us-west-1`); can be overridden per request
* @param {string} [config.profile] Credential profile name to use
* @param {boolean} [config.autoloadPlugins=true] Attempt to automatically load aws-lite plugins
* @param {boolean} [config.keepAlive=true] Disable Node.js's connection keepalive, helpful for local-only testing
*
* @returns {Promise<function>} Client async function
*/
Expand Down
19 changes: 18 additions & 1 deletion src/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let https = require('https')
let aws4 = require('aws4')

module.exports = function request (params, creds, region, metadata) {
module.exports = function request (params, creds, region, config, metadata) {
return new Promise((resolve, reject) => {
params.path = params.path || params.endpoint

Expand All @@ -17,6 +17,10 @@ module.exports = function request (params, creds, region, metadata) {
}

let options = aws4.sign({ ...params, region }, creds)

// Disable keep-alive locally (or wait Node's default 5s for sockets to time out)
options.agent = new https.Agent({ keepAlive: config.keepAlive ?? useAWS() })

let req = https.request(options, res => {
let data = []
let { headers = {}, statusCode } = res
Expand All @@ -37,3 +41,16 @@ module.exports = function request (params, creds, region, metadata) {
req.end(options.body || '')
})
}

// Probably this is going to need some refactoring in Arc 11
// Certainly it is not reliable in !Arc local Lambda emulation
let nonLocalEnvs = [ 'staging', 'production' ]
function useAWS () {
let { ARC_ENV, ARC_LOCAL, ARC_SANDBOX } = process.env
// Testing is always local
if (ARC_ENV === 'testing') return false
// Local, but using AWS resources
if (nonLocalEnvs.includes(ARC_ENV) && ARC_SANDBOX && !ARC_LOCAL) return false
// Assumed to be AWS
return true
}

0 comments on commit b13faa6

Please sign in to comment.