Skip to content

Commit 65928cd

Browse files
committed
Add timeout to requests
1 parent a3d780a commit 65928cd

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ nconf
2020
"MAX_CONSECUTIVE_ERRORS": 3,
2121
"MAX_RESOURCE_SIZE": 256000,
2222
"CT_SECS_RESOURCE_EXPIRE": 90000,
23-
"MIN_SECS_BETWEEN_PINGS": 0
23+
"MIN_SECS_BETWEEN_PINGS": 0,
24+
"REQUEST_TIMEOUT": 4000
2425
});
2526

2627
module.exports = {
@@ -32,5 +33,6 @@ module.exports = {
3233
maxConsecutiveErrors: nconf.get('MAX_CONSECUTIVE_ERRORS'),
3334
maxResourceSize: nconf.get('MAX_RESOURCE_SIZE'),
3435
ctSecsResourceExpire: nconf.get('CT_SECS_RESOURCE_EXPIRE'),
35-
minSecsBetweenPings: nconf.get('MIN_SECS_BETWEEN_PINGS')
36-
};
36+
minSecsBetweenPings: nconf.get('MIN_SECS_BETWEEN_PINGS'),
37+
requestTimeout: nconf.get('REQUEST_TIMEOUT')
38+
};

services/notify-one-challenge.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(function () {
22
"use strict";
33

4-
const notifyOne = require('./notify-one'),
4+
const config = require('../config'),
5+
notifyOne = require('./notify-one'),
56
getRandomPassword = require('./get-random-password'),
67
querystring = require('querystring'),
78
request = require('request-promise-native');
@@ -14,6 +15,7 @@
1415
}),
1516
res = await request({
1617
uri: testUrl,
18+
timeout: config.requestTimeout,
1719
resolveWithFullResponse: true
1820
});
1921

services/notify-one.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"use strict";
33

44
const builder = require('xmlbuilder'),
5+
config = require('../config'),
56
request = require('request-promise-native');
67

78
async function notifyOneRest(apiurl, resourceUrl) {
89
const res = await request({
910
method: 'POST',
1011
uri: apiurl,
12+
timeout: config.requestTimeout,
1113
form: {
1214
'url': resourceUrl
1315
},
@@ -36,6 +38,7 @@
3638
let res = await request({
3739
method: 'POST',
3840
uri: apiurl,
41+
timeout: 4000,
3942
body: xmldoc,
4043
resolveWithFullResponse: true,
4144
headers: {

services/ping.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
res = await request({
3434
method: 'GET',
3535
uri: resourceUrl,
36+
timeout: config.requestTimeout,
3637
followRedirect: true,
3738
maxRedirects: 3,
3839
resolveWithFullResponse: true

services/please-notify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
return request({
1818
method: 'GET',
1919
uri: resourceUrl,
20+
timeout: config.requestTimeout,
2021
followRedirect: true,
2122
maxRedirects: 3,
2223
resolveWithFullResponse: true

test/ping.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,71 @@ for (const pingProtocol of ['XML-RPC', 'REST']) {
408408
}
409409
});
410410

411+
it(`should consider a very slow subscription an error`, async function () {
412+
const feedPath = '/rss.xml',
413+
pingPath = '/feedupdated',
414+
resourceUrl = mock.serverUrl + feedPath;
415+
416+
let apiurl = ('http-post' === protocol ? mock.serverUrl : mock.secureServerUrl) + pingPath,
417+
notifyProcedure = false;
418+
419+
if ('xml-rpc' === protocol) {
420+
apiurl = mock.serverUrl + '/RPC2';
421+
notifyProcedure = 'river.feedUpdated';
422+
}
423+
424+
function slowRestResponse(req) {
425+
return new Promise((resolve) => {
426+
setTimeout(() => {
427+
resolve('Thanks for the update! :-)');
428+
}, 8000)
429+
});
430+
}
431+
432+
function slowRpcResponse(req) {
433+
return new Promise((resolve) => {
434+
setTimeout(() => {
435+
resolve(rpcReturnSuccess(true));
436+
}, 8000)
437+
});
438+
}
439+
440+
mock.route('GET', feedPath, 200, '<RSS Feed />');
441+
mock.route('POST', pingPath, 200, slowRestResponse);
442+
mock.rpc(notifyProcedure, slowRpcResponse);
443+
await mongodb.addSubscription(resourceUrl, notifyProcedure, apiurl, protocol);
444+
445+
let res = await ping(pingProtocol, resourceUrl, returnFormat);
446+
447+
expect(res).status(200);
448+
449+
const subscription = await mongodb.addSubscription(resourceUrl, notifyProcedure, apiurl, protocol);
450+
expect(subscription.ctConsecutiveErrors).equal(1);
451+
452+
if ('XML-RPC' === pingProtocol) {
453+
expect(res.text).xml.equal(rpcReturnSuccess(true));
454+
} else {
455+
if ('JSON' === returnFormat) {
456+
expect(res.body).deep.equal({ success: true, msg: 'Thanks for the ping.' });
457+
} else {
458+
expect(res.text).xml.equal('<result success="true" msg="Thanks for the ping."/>');
459+
}
460+
}
461+
462+
expect(mock.requests.GET).property(feedPath).lengthOf(1, `Missing GET ${feedPath}`);
463+
464+
if ('xml-rpc' === protocol) {
465+
expect(mock.requests.RPC2).property(notifyProcedure).lengthOf(1, `Missing XML-RPC call ${notifyProcedure}`);
466+
expect(mock.requests.RPC2[notifyProcedure][0]).property('rpcBody');
467+
expect(mock.requests.RPC2[notifyProcedure][0].rpcBody.params[0]).equal(resourceUrl);
468+
} else {
469+
expect(mock.requests.POST).property(pingPath).lengthOf(1, `Missing POST ${pingPath}`);
470+
expect(mock.requests.POST[pingPath][0]).property('body');
471+
expect(mock.requests.POST[pingPath][0].body).property('url');
472+
expect(mock.requests.POST[pingPath][0].body.url).equal(resourceUrl);
473+
}
474+
});
475+
411476
});
412477

413478
} // end for pingProtocol

0 commit comments

Comments
 (0)