-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[React][Vue][Angular] Ensure keep-alive agent is used throughout samp…
…le apps for server-side web requests (#463)
- Loading branch information
1 parent
c5571d8
commit e6e556a
Showing
7 changed files
with
100 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const keepAlive = require("agentkeepalive"); | ||
const http = require("http"); | ||
const https = require("https"); | ||
|
||
const keepAliveConfig = { | ||
maxSockets: 200, | ||
maxFreeSockets: 20, | ||
timeout: 240 * 1000, | ||
freeSocketTimeout: 240 * 1000, | ||
}; | ||
|
||
const httpAgent = new keepAlive(keepAliveConfig); | ||
const httpsAgent = new keepAlive.HttpsAgent(keepAliveConfig); | ||
|
||
module.exports = { | ||
setUpDefaultAgents: (serverBundle) => { | ||
http.globalAgent = httpAgent; | ||
https.globalAgent = httpsAgent; | ||
|
||
if (serverBundle.setUpDefaultAgents) { | ||
serverBundle.setUpDefaultAgents(httpAgent, httpsAgent); | ||
} | ||
}, | ||
/** | ||
* Enable connection pooling. Adds `connection: keep-alive` header | ||
* @param {string} url api host | ||
*/ | ||
getAgent: (url) => { | ||
if (!url) { | ||
throw new Error("[KEEP-ALIVE-CONFIG] SITECORE_API_HOST value is required, but was undefined") | ||
} | ||
|
||
if (!url.indexOf("http://")) return httpAgent; | ||
|
||
if (!url.indexOf("https://")) return httpsAgent; | ||
|
||
throw new Error( | ||
"[KEEP-ALIVE-CONFIG] Unexpected SITECORE_API_HOST value, expected http:// or https://, but was " + | ||
url | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters