Skip to content

Commit

Permalink
feat: turn on keepalive for requesting remote resources
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Apr 24, 2020
1 parent bee0c12 commit 5cdfeaf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lib/utils/http-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import got from 'got';
import HttpAgent, { HttpsAgent } from 'agentkeepalive';

import { NETWORK_TIMEOUT } from './constant';

const httpClient = got.extend({
timeout: NETWORK_TIMEOUT,
agent: {
http: new HttpAgent(),
https: new HttpsAgent()
},
});

export default httpClient;
8 changes: 4 additions & 4 deletions lib/utils/remote-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import detectNewline from 'detect-newline';
import { RemoteSnippet, RemoteSnippetConfig } from '../types';
import { NETWORK_CONCURRENCY, NETWORK_TIMEOUT, REMOTE_SNIPPET_CACHE_MAXAGE } from './constant';
import { ConfigCache } from './cache';
import httpClient from './http-client';
import { createTmpFactory } from './tmp-helper';

export const addProxyToSurgeRuleSet = (str: string, proxyName: string): string => {
Expand Down Expand Up @@ -47,17 +48,16 @@ export const addProxyToSurgeRuleSet = (str: string, proxyName: string): string =

export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSnippetConfig>): Promise<ReadonlyArray<RemoteSnippet>> => {
function load(url: string): Promise<string> {
logger.info(`正在下载远程片段: ${url}`);
// logger.info(`正在下载远程片段: ${url}`);

return got.get(url, {
timeout: NETWORK_TIMEOUT,
})
return httpClient.get(url)
.then(data => {
logger.info(`远程片段下载成功: ${url}`);
return data.body;
})
.catch(err => {
logger.error(`远程片段下载失败: ${url}`);
console.log(err.response)
throw err;
});
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"dependencies": {
"@hapi/joi": "^16.1.8",
"@surgio/logger": "^1.1.0",
"agentkeepalive": "^4.1.0",
"ali-oss": "^6.7.0",
"bluebird": "^3.7.2",
"bytes": "^3.1.0",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,15 @@ agentkeepalive@^3.4.1:
dependencies:
humanize-ms "^1.2.1"

agentkeepalive@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.1.0.tgz#a48e040ed16745dd29ce923675f60c9c90f39ee0"
integrity sha512-CW/n1wxF8RpEuuiq6Vbn9S8m0VSYDMnZESqaJ6F2cWN9fY8rei2qaxweIaRgq+ek8TqfoFIsUjaGNKGGEHElSg==
dependencies:
debug "^4.1.0"
depd "^1.1.2"
humanize-ms "^1.2.1"

aggregate-error@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/aggregate-error/-/aggregate-error-3.0.1.tgz#db2fe7246e536f40d9b5442a39e117d7dd6a24e0"
Expand Down

0 comments on commit 5cdfeaf

Please sign in to comment.