mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-11 04:31:20 -07:00
Enhancement: Cache and reuse keep-alive HTTP(S) agents (#6536)
Some checks failed
Release Drafter / Auto Label PR (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Some checks failed
Release Drafter / Auto Label PR (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
This commit is contained in:
@@ -224,23 +224,43 @@ function homepageDNSLookupFn() {
|
||||
};
|
||||
}
|
||||
|
||||
const homepageLookup = homepageDNSLookupFn();
|
||||
const agentCache = new Map();
|
||||
|
||||
function getAgent(protocol, disableIpv6) {
|
||||
const cacheKey = `${protocol}:${disableIpv6 ? "ipv4" : "auto"}`;
|
||||
const cachedAgent = agentCache.get(cacheKey);
|
||||
if (cachedAgent) {
|
||||
return cachedAgent;
|
||||
}
|
||||
|
||||
const agentOptions = {
|
||||
keepAlive: true,
|
||||
...(disableIpv6 ? { family: 4, autoSelectFamily: false } : { autoSelectFamilyAttemptTimeout: 500 }),
|
||||
lookup: homepageLookup,
|
||||
};
|
||||
|
||||
const agent =
|
||||
protocol === "https:"
|
||||
? new https.Agent({ ...agentOptions, rejectUnauthorized: false })
|
||||
: new http.Agent(agentOptions);
|
||||
|
||||
agentCache.set(cacheKey, agent);
|
||||
return agent;
|
||||
}
|
||||
|
||||
export async function httpProxy(url, params = {}) {
|
||||
const constructedUrl = new URL(url);
|
||||
const disableIpv6 = process.env.HOMEPAGE_PROXY_DISABLE_IPV6 === "true";
|
||||
const agentOptions = {
|
||||
...(disableIpv6 ? { family: 4, autoSelectFamily: false } : { autoSelectFamilyAttemptTimeout: 500 }),
|
||||
lookup: homepageDNSLookupFn(),
|
||||
};
|
||||
|
||||
let request = null;
|
||||
if (constructedUrl.protocol === "https:") {
|
||||
request = httpsRequest(constructedUrl, {
|
||||
agent: new https.Agent({ ...agentOptions, rejectUnauthorized: false }),
|
||||
agent: getAgent(constructedUrl.protocol, disableIpv6),
|
||||
...params,
|
||||
});
|
||||
} else {
|
||||
request = httpRequest(constructedUrl, {
|
||||
agent: new http.Agent(agentOptions),
|
||||
agent: getAgent(constructedUrl.protocol, disableIpv6),
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user