[英]Does keep alive only matter with outbound requests?
如果我的客戶可能會在相對較短的時間內多次向http Google Cloud功能請求,我該如何使用keep-alive? 讓客戶端發送連接保持活動標頭足夠嗎?
我在Google文檔中看到了這一點: https : //cloud.google.com/functions/docs/bestpractices/networking
const http = require('http');
const agent = new http.Agent({keepAlive: true});
/**
* HTTP Cloud Function that caches an HTTP agent to pool HTTP connections.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.connectionPooling = (req, res) => {
req = http.request(
{
host: '',
port: 80,
path: '',
method: 'GET',
agent: agent,
},
resInner => {
let rawData = '';
resInner.setEncoding('utf8');
resInner.on('data', chunk => {
rawData += chunk;
});
resInner.on('end', () => {
res.status(200).send(`Data: ${rawData}`);
});
}
);
req.on('error', e => {
res.status(500).send(`Error: ${e.message}`);
});
req.end();
};
但是,這只適用於從雲功能發出出站請求嗎?
此處還有關於全球(實例范圍)范圍的內容: https : //cloud.google.com/functions/docs/bestpractices/tips
在重組用戶發送的請求上重用連接是否需要做些什么?
在函數的全局作用域中定義agent
時,只有在運行它的任何給定服務器實例時才會保留該agent
。 因此,雖然您的連接可能在該實例中保持活動狀態,但它不會與您的函數負載增加時分配的其他實例共享任何連接。 您無法直接控制Cloud Functions何時啟動新實例以處理新負載,或何時將解除分配該實例。 您必須接受它們隨着時間的推移而來,以及全局范圍對象保持活動的任何HTTP連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.