From 01fb9f487247eda94ade314733dcd936db0c5705 Mon Sep 17 00:00:00 2001 From: jsetton Date: Sun, 1 Sep 2019 17:22:34 -0400 Subject: [PATCH] Improve keep-alive socket handling * changed request http agent to use agentkeepalive module * added control-cache request header to enforce no-caching on responses Signed-off-by: jsetton --- lambda/smarthome/lib/rest.js | 45 ++++++++++++++++++++---------- lambda/smarthome/package-lock.json | 35 +++++++++++++++++++---- lambda/smarthome/package.json | 1 + 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/lambda/smarthome/lib/rest.js b/lambda/smarthome/lib/rest.js index 7785a2f3..628613a8 100644 --- a/lambda/smarthome/lib/rest.js +++ b/lambda/smarthome/lib/rest.js @@ -12,11 +12,8 @@ */ const fs = require('fs'); -// Import request module and set default options -const request = require('request-promise-native').defaults({ - forever: true, // Connection: keep-alive - gzip: true, // Accept-Encoding: gzip -}); +const request = require('request-promise-native'); +const Agent = require('agentkeepalive'); /** * Defines configuration settings object @@ -73,10 +70,10 @@ function getConfigFileSettings() { function ohAuthenticationSettings(token, options = {}) { if (config.openhab.cert) { // SSL Certificate Authentication - options.agentOptions = { + options.agentOptions = Object.assign({}, options.agentOptions, { pfx: config.openhab.cert, passphrase: config.openhab.certPass - }; + }); } else { options.headers = Object.assign({}, options.headers, { 'Authorization': config.openhab.userpass ? @@ -127,10 +124,9 @@ function getItemOrItems(token, timeout, itemName, parameters) { method: 'GET', uri: `${config.openhab.baseURL}/items/${itemName || ''}`, qs: parameters, - json: true, - timeout: parseInt(timeout) + json: true }); - return request(options); + return handleRequest(options, timeout); } /** @@ -143,10 +139,9 @@ function getRegionalSettings(token, timeout) { const options = ohAuthenticationSettings(token, { method: 'GET', uri: `${config.openhab.baseURL}/services/org.eclipse.smarthome.core.i18nprovider/config`, - json: true, - timeout: parseInt(timeout) + json: true }); - return request(options); + return handleRequest(options, timeout); } /** @@ -164,7 +159,29 @@ function postItemCommand(token, timeout, itemName, value) { headers: { 'Content-Type': 'text/plain' }, - body: value.toString(), + body: value.toString() + }); + return handleRequest(options, timeout); +} + +/** + * Handles http request + * @param {Object} options + * @param {Number} timeout + * @return {Promise} + */ +function handleRequest(options, timeout) { + // Add default request options + Object.assign(options, { + agentClass: options.uri.startsWith('https') ? Agent.HttpsAgent : Agent, + agentOptions: Object.assign({}, options.agentOptions, { + // Set keep-alive free socket to timeout after 45s of inactivity + freeSocketTimeout: 45000 + }), + headers: Object.assign({}, options.headers, { + 'Cache-Control': 'no-cache' + }), + gzip: true, timeout: parseInt(timeout) }); return request(options); diff --git a/lambda/smarthome/package-lock.json b/lambda/smarthome/package-lock.json index 8a25b3a4..0f292cd1 100644 --- a/lambda/smarthome/package-lock.json +++ b/lambda/smarthome/package-lock.json @@ -36,6 +36,16 @@ "integrity": "sha512-HJ7CfNHrfJLlNTzIEUTj43LNWGkqpRLxm3YjAlcD0ACydk9XynzYsCBHxut+iqt+1aBXkx9UP/w/ZqMr13XIzg==", "dev": true }, + "agentkeepalive": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-4.0.2.tgz", + "integrity": "sha512-A5gSniD4xMCYtSD4ilUHpQRB9ZbNjtIPittKUv7bA0j0UCwbT3EJBUYLKPJ/dtmaXRYWI2mG4/O90xbi7oahNw==", + "requires": { + "debug": "^4.1.0", + "depd": "^1.1.2", + "humanize-ms": "^1.2.1" + } + }, "ajv": { "version": "6.10.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.0.tgz", @@ -331,7 +341,6 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "dev": true, "requires": { "ms": "^2.1.1" } @@ -364,6 +373,11 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, "diagnostics": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/diagnostics/-/diagnostics-1.1.1.tgz", @@ -510,10 +524,13 @@ } }, "eslint-utils": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.3.1.tgz", - "integrity": "sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==", - "dev": true + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.2.tgz", + "integrity": "sha512-eAZS2sEUMlIeCjBeubdj45dmBHQwPHWyBcT1VSYB7o9x9WRRqKxyUoiXlRjyAwzN7YEzHJlYg0NmzDRWx6GP4Q==", + "dev": true, + "requires": { + "eslint-visitor-keys": "^1.0.0" + } }, "eslint-visitor-keys": { "version": "1.0.0", @@ -753,6 +770,14 @@ "sshpk": "^1.7.0" } }, + "humanize-ms": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", + "integrity": "sha1-xG4xWaKT9riW2ikxbYtv6Lt5u+0=", + "requires": { + "ms": "^2.0.0" + } + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/lambda/smarthome/package.json b/lambda/smarthome/package.json index d6b5262b..d404c040 100644 --- a/lambda/smarthome/package.json +++ b/lambda/smarthome/package.json @@ -19,6 +19,7 @@ "test": "NODE_ENV=test mocha" }, "dependencies": { + "agentkeepalive": "^4.0.2", "camelcase": "^5.0.0", "decamelize": "^2.0.0", "module-alias": "^2.2.0",