summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2021-08-16 17:44:43 +0900
committerGitHub <noreply@github.com>2021-08-16 17:44:43 +0900
commitdef32107af9e34f162cfe586e68c4379d61c4a22 (patch)
treeb13fdf0e7d3270c9f268443310b695032d0a1218 /src
parentchore: yarn.lockがおかしかったらCIでコケるように (#7634) (diff)
downloadmisskey-def32107af9e34f162cfe586e68c4379d61c4a22.tar.gz
misskey-def32107af9e34f162cfe586e68c4379d61c4a22.tar.bz2
misskey-def32107af9e34f162cfe586e68c4379d61c4a22.zip
perf: Improve network request performance (#7636)
* perf: Improve fetch * CHANGELOG * lifo
Diffstat (limited to 'src')
-rw-r--r--src/@types/lookup-dns-cache.d.ts9
-rw-r--r--src/misc/fetch.ts33
2 files changed, 27 insertions, 15 deletions
diff --git a/src/@types/lookup-dns-cache.d.ts b/src/@types/lookup-dns-cache.d.ts
deleted file mode 100644
index bae9df5faf..0000000000
--- a/src/@types/lookup-dns-cache.d.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-declare module 'lookup-dns-cache' {
- import { LookupOneOptions, LookupAllOptions, LookupOptions, LookupAddress } from 'dns';
-
- function lookup(hostname: string, family: number, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void): void;
- function lookup(hostname: string, options: LookupOneOptions, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void): void;
- function lookup(hostname: string, options: LookupAllOptions, callback: (err: NodeJS.ErrnoException | null, addresses: LookupAddress[]) => void): void;
- function lookup(hostname: string, options: LookupOptions, callback: (err: NodeJS.ErrnoException | null, address: string | LookupAddress[], family: number) => void): void;
- function lookup(hostname: string, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void): void;
-}
diff --git a/src/misc/fetch.ts b/src/misc/fetch.ts
index 37f8203543..f7756f9256 100644
--- a/src/misc/fetch.ts
+++ b/src/misc/fetch.ts
@@ -1,9 +1,8 @@
import * as http from 'http';
import * as https from 'https';
-import * as cache from 'lookup-dns-cache';
+import CacheableLookup from 'cacheable-lookup';
import fetch, { HeadersInit } from 'node-fetch';
-import { HttpProxyAgent } from 'http-proxy-agent';
-import { HttpsProxyAgent } from 'https-proxy-agent';
+import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import config from '@/config';
import { URL } from 'url';
@@ -49,6 +48,12 @@ export async function getHtml(url: string, accept = 'text/html, */*', timeout =
return await res.text();
}
+const cache = new CacheableLookup({
+ maxTtl: 3600, // 1hours
+ errorTtl: 30, // 30secs
+ lookup: false, // nativeのdns.lookupにfallbackしない
+});
+
/**
* Get http non-proxy agent
*/
@@ -65,20 +70,36 @@ const _https = new https.Agent({
keepAlive: true,
keepAliveMsecs: 30 * 1000,
lookup: cache.lookup,
-});
+} as https.AgentOptions);
+
+const maxSockets = Math.max(256, config.deliverJobConcurrency || 128);
/**
* Get http proxy or non-proxy agent
*/
export const httpAgent = config.proxy
- ? new HttpProxyAgent(config.proxy)
+ ? new HttpProxyAgent({
+ keepAlive: true,
+ keepAliveMsecs: 30 * 1000,
+ maxSockets,
+ maxFreeSockets: 256,
+ scheduling: 'lifo',
+ proxy: config.proxy
+ })
: _http;
/**
* Get https proxy or non-proxy agent
*/
export const httpsAgent = config.proxy
- ? new HttpsProxyAgent(config.proxy)
+ ? new HttpsProxyAgent({
+ keepAlive: true,
+ keepAliveMsecs: 30 * 1000,
+ maxSockets,
+ maxFreeSockets: 256,
+ scheduling: 'lifo',
+ proxy: config.proxy
+ })
: _https;
/**