[英]force http.globalAgent.keepAlive = true
我有這個節點 CLI 工具,只要它運行,它就會消耗我所有網絡的 NAT(網絡地址轉換)地址端口,因為節點工具會為每個出站 http 請求創建一個連接。 該工具未完成工作,因為 NAT 網關在節點工具超過限制后阻止該工具打開任何更多出站端口。
我可以設置http.globalAgent.keepAlive
,但問題是這個節點 CLI 工具execs
到其他模塊,所以我必須在所有這些子模塊中設置http.globalAgent.keepAlive
。 有沒有辦法可以強制http.globalAgent.keepAlive
無處不在,而無需更改每個sub-execed
節點工具中的代碼?
node
命令支持--require
標志,該標志在執行傳遞給node
的文件之前預加載一個模塊(或幾個模塊)。
Because of the require
cache, if a module imports the http
module and sets http.globalAgent.keepAlive = true
then any other module that imports http
will be importing http
from the require
cache with http.globalAgent.keepAlive = true
.
因此,關鍵是覆蓋系統的node
命令為--require
一個設置http.globalAgent.keepAlive = true
的模塊。 每當 CLI 工具execs node
時,腳本就會改為運行並首先導入覆蓋模塊。 需要緩存將預先填充http
和該node
進程的所需設置。
像下面這樣的腳本將通過任何 arguments 到 node 並在調用node
時加載覆蓋模塊。
#! /bin/sh
exec node_bin --require /path/to/module "$@"
您還需要將原始node
二進制文件移動到類似node_bin
的位置,並將上述腳本作為新的"node"
二進制文件插入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.