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

[node-headless-proxy] Connection pooling to layout service #430

Merged
merged 1 commit into from
Aug 20, 2020
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
33 changes: 32 additions & 1 deletion samples/node-headless-ssr-proxy/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const keepAlive = require("agentkeepalive");
const ipaddr = require('ipaddr.js');
const fetch = require('node-fetch');
const NodeCache = require('node-cache');
Expand All @@ -19,8 +20,36 @@ const bundlePath = process.env.SITECORE_JSS_SERVER_BUNDLE || `./dist/${appName}/

const serverBundle = require(bundlePath);

const apiHost = process.env.SITECORE_API_HOST || 'http://my.sitecore.host'

appName = appName || serverBundle.appName;

/**
* Enable connection pooling. Adds `connection: keep-alive` header
* @param {string} apiHost sitecore api host
*/
const keepAliveAgent = (apiHost) => {
const keepAliveConfig = {
maxSockets: 100,
maxFreeSockets: 10,
timeout: 240 * 1000,
freeSocketTimeout: 240 * 1000,
};

if (!apiHost) {
throw new Error("[KEEP-ALIVE-CONFIG] SITECORE_API_HOST value is required, but was undefined")
}

if (!apiHost.indexOf("http://")) return new keepAlive(keepAliveConfig);

if (!apiHost.indexOf("https://")) return new keepAlive.HttpsAgent(keepAliveConfig);

throw new Error(
"[KEEP-ALIVE-CONFIG] Unexpected SITECORE_API_HOST value, expected http:// or https://, but was " +
apiHost
);
}

/**
* @type {ProxyConfig}
*/
Expand All @@ -34,7 +63,7 @@ const config = {
* Should be https for production. Must be https to use SSC auth service,
* if supporting Sitecore authentication.
*/
apiHost: process.env.SITECORE_API_HOST || 'http://my.sitecore.host',
apiHost,
/**
* layoutServiceRoot: The path to layout service for the JSS application.
* Some apps, like advanced samples, use a custom LS configuration,
Expand Down Expand Up @@ -82,6 +111,8 @@ const config = {
* Options object for http-proxy-middleware. Consult its docs.
*/
proxyOptions: {
// Enable connection pooling
agent: keepAliveAgent(apiHost),
// Setting this to false will disable SSL certificate validation
// when proxying to a SSL Sitecore instance.
// This is a major security issue, so NEVER EVER set this to false
Expand Down
145 changes: 145 additions & 0 deletions samples/node-headless-ssr-proxy/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 samples/node-headless-ssr-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "Apache-2.0",
"dependencies": {
"@sitecore-jss/sitecore-jss-proxy": "^14.0.1",
"agentkeepalive": "^4.1.3",
"compression": "~1.7.3",
"express": "~4.16.4",
"ipaddr.js": "~1.8.1",
Expand Down