Skip to content

Commit

Permalink
Improve keep-alive socket handling
Browse files Browse the repository at this point in the history
* changed request http agent to use agentkeepalive module
* added control-cache request header to enforce no-caching on responses

Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton committed Sep 3, 2019
1 parent ac1a452 commit 01fb9f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
45 changes: 31 additions & 14 deletions lambda/smarthome/lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ?
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand Down
35 changes: 30 additions & 5 deletions lambda/smarthome/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lambda/smarthome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 01fb9f4

Please sign in to comment.