From 89e82d88fa1a192cb2b5828246564f2191c47fc7 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 1 Feb 2021 20:06:49 +0800 Subject: [PATCH] feat: got request timeout --- docs/en/install/README.md | 2 ++ lib/config.js | 1 + lib/utils/got.js | 1 + test/utils/got.js | 21 +++++++++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/docs/en/install/README.md b/docs/en/install/README.md index fd66bea4c2..b0ec4ea209 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -421,6 +421,8 @@ See the relation between access key/code and white/blacklisting. `REQUEST_RETRY`: retries allowed for failed requests, default to `2` +`REQUEST_TIMEOUT`: milliseconds to wait for the server to end the response before aborting the request with error, default to `3000` + `DEBUG_INFO`: display route information on homepage for debugging purpose. When set to neither `true` nor `false`, use parameter `debug` to enable display, eg: . Default to `true` `NODE_ENV`: display error message on pages for authentication failing, default to `production` (i.e. no display) diff --git a/lib/config.js b/lib/config.js index 98e3a19cf4..0ebad15129 100644 --- a/lib/config.js +++ b/lib/config.js @@ -39,6 +39,7 @@ const calculateValue = () => { ua: envs.UA || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', listenInaddrAny: envs.LISTEN_INADDR_ANY || 1, // 是否允许公网连接,取值 0 1 requestRetry: parseInt(envs.REQUEST_RETRY) || 2, // 请求失败重试次数 + requestTimeout: parseInt(envs.REQUEST_TIMEOUT) || 30000, // Milliseconds to wait for the server to end the response before aborting the request // 是否显示 Debug 信息,取值 'true' 'false' 'some_string' ,取值为 'true' 时永久显示,取值为 'false' 时永远隐藏,取值为 'some_string' 时请求带上 '?debug=some_string' 显示 debugInfo: envs.DEBUG_INFO || 'true', disallowRobot: envs.DISALLOW_ROBOT !== '0' && envs.DISALLOW_ROBOT !== 'false', diff --git a/lib/utils/got.js b/lib/utils/got.js index 544140166b..39b2d2b094 100644 --- a/lib/utils/got.js +++ b/lib/utils/got.js @@ -33,6 +33,7 @@ const custom = got.extend({ headers: { 'user-agent': config.ua, }, + timeout: config.requestTimeout, }); custom.all = (list) => Promise.all(list); diff --git a/test/utils/got.js b/test/utils/got.js index 1c8e26e895..ef53103b43 100644 --- a/test/utils/got.js +++ b/test/utils/got.js @@ -1,7 +1,12 @@ +process.env.REQUEST_TIMEOUT = '500'; const got = require('../../lib/utils/got'); const config = require('../../lib/config').value; const nock = require('nock'); +afterAll(() => { + delete process.env.REQUEST_TIMEOUT; +}); + describe('got', () => { it('headers', async () => { nock('http://rsshub.test') @@ -51,4 +56,20 @@ describe('got', () => { expect(response1.body).toBe('{"code": 0}'); expect(response1.data.code).toBe(0); }); + + it('timeout', async () => { + nock('http://rsshub.test') + .get('/timeout') + .delay(600) + .reply(function () { + return [200, '{"code": 0}']; + }); + + try { + await got.get('http://rsshub.test/timeout'); + throw Error('Timeout Invalid'); + } catch (error) { + expect(error.name).toBe('RequestError'); + } + }); });