-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: upgrade dnscache_httpclient and enable dnscache_fetch #5731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -289,8 +289,14 @@ module.exports = appInfo => { | |
| /** | ||
| * The option for httpclient | ||
| * @member Config#httpclient | ||
| * @property {Boolean} enableDNSCache - Enable DNS lookup from local cache or not, default is false. | ||
| * @property {Boolean} dnsCacheLookupInterval - minimum interval of DNS query on the same hostname (default 10s). | ||
| * @property {Boolean} enableDNSCache - Enable DNS lookup cache using dns.lookup (old behavior), default is false. | ||
|
||
| * @property {Number} dnsCacheLookupInterval - DNS cache lookup interval in ms for old dns.lookup mode, default is 10000 ms. | ||
| * | ||
| * @property {Boolean} useDNSResolver - Enable DNS resolve cache using dns.resolve (new feature), default is false. | ||
| * @property {Array<String>} dnsServers - Custom DNS nameservers for DNS resolver cache, e.g. ['8.8.8.8', '1.1.1.1']. If not set, use system default. | ||
| * | ||
| * @property {Number} dnsCacheMaxLength - DNS cache max size, default is 1000. | ||
| * @property {Boolean} dnsAddressRotation - Enable address rotation for both lookup and resolve modes, default is true. | ||
| * | ||
| * @property {Number} request.timeout - httpclient request default timeout, default is 5000 ms. | ||
| * | ||
|
|
@@ -307,12 +313,21 @@ module.exports = appInfo => { | |
| * @property {Boolean} allowH2 - use urllib@4 HttpClient and enable H2, default is false. Only works on Node.js >= 18 | ||
| */ | ||
| config.httpclient = { | ||
| // Enable DNS cache mode using dns.lookup | ||
| enableDNSCache: false, | ||
| dnsCacheLookupInterval: 10000, | ||
| dnsCacheLookupInterval: 10000, // will not work if useDNSResolver is true | ||
|
|
||
| // DNS resolver mode using dns.resolve (new feature) | ||
| useDNSResolver: false, // will not work if enableDNSCache IS NOT true | ||
|
||
| dnsServers: undefined, // Use system default if not set | ||
|
|
||
| // Common dns options | ||
| dnsCacheMaxLength: 1000, | ||
| dnsAddressRotation: true, | ||
|
|
||
| request: { | ||
| timeout: 5000, | ||
| reset: false, // only works when useHttpClientNext is true | ||
| }, | ||
| httpAgent: { | ||
| keepAlive: true, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,19 @@ declare module 'egg' { | |
| new(ctx: Context): EggContextHttpClient; | ||
| } | ||
|
|
||
| export interface EggContextFetch { | ||
| ctx: Context; | ||
| app: Application; | ||
|
Comment on lines
+93
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这两个内部细节不应该对外透出了。 |
||
| fetch(url: string | URL, init?: RequestInit): Promise<Response>; | ||
| /** | ||
| * safeFetch request helper with SSRF protection | ||
| */ | ||
| safeFetch(url: string | URL, init?: RequestInit): Promise<Response>; | ||
| } | ||
| interface EggContextFetchConstructor { | ||
| new (ctx: Context): EggContextFetch; | ||
| } | ||
|
|
||
| /** | ||
| * BaseContextClass is a base class that can be extended, | ||
| * it's instantiated in context level, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class ContextFetch { | ||
| constructor(ctx) { | ||
| this.ctx = ctx; | ||
| this.app = ctx.app; | ||
| } | ||
|
|
||
| /** | ||
| * fetch request helper based on native fetch API or urllib4's FetchFactory | ||
| * Keep the same api with {@link Application#fetch}. | ||
| * | ||
| * @param {String|URL} url - request url address. | ||
| * @param {Object} [init] - fetch init options. | ||
| * @return {Promise<Response>} fetch response | ||
| */ | ||
| async fetch(url, init) { | ||
| init = init || {}; | ||
| init.ctx = this.ctx; | ||
| return await this.app.fetch(url, init); | ||
| } | ||
|
|
||
| /** | ||
| * safeFetch request helper with SSRF protection | ||
| * Keep the same api with {@link Application#safeFetch}. | ||
| * | ||
| * @param {String|URL} url - request url address. | ||
| * @param {Object} [init] - fetch init options. | ||
| * @return {Promise<Response>} fetch response | ||
| */ | ||
| async safeFetch(url, init) { | ||
| init = init || {}; | ||
| init.ctx = this.ctx; | ||
| return await this.app.safeFetch(url, init); | ||
| } | ||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| module.exports = ContextFetch; | ||
Uh oh!
There was an error while loading. Please reload this page.