Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve keep-alive socket handling #270

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
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
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