Skip to content

Commit

Permalink
feat: add retry delay on httpclient2
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 23, 2017
1 parent 997d887 commit 12c8eb9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
This software is licensed under the MIT License.

Copyright(c) 2011 - 2014 fengmk2 and other contributors.
Copyright(c) 2015 - 2016 node-modules and other contributors.
Copyright(c) 2015 - 2017 node-modules and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ HttpClient2 is a new instance for future. request method only return a promise,
options extends from urllib, besides below

- ***retry*** Number - a retry count, when get an error, it will request again until reach the retry count.
- ***retryDelay*** Number - wait a delay(ms) between retries.
- ***isRetry*** Function - determine whether retry, a response object as the first argument. it will retry when status >= 500 by default. Request error is not included.

## TODO
Expand Down
14 changes: 13 additions & 1 deletion lib/httpclient2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

var util = require('util');
var debug = require('debug')('urllib');
var ms = require('humanize-ms');
var sleep = require('mz-modules/sleep');
var HttpClient = require('./httpclient');


module.exports = HttpClient2;

function HttpClient2() {
Expand All @@ -17,6 +18,9 @@ HttpClient2.prototype.request = HttpClient2.prototype.curl = function request(ur
var self = this;
args = args || {};
args.retry = args.retry || 0;
if (args.retryDelay) {
args.retryDelay = ms(args.retryDelay);
}
args.isRetry = args.isRetry || function(res) {
return res.status >= 500;
};
Expand All @@ -25,6 +29,10 @@ HttpClient2.prototype.request = HttpClient2.prototype.curl = function request(ur
if (args.retry > 0) {
args.retry--;
debug('retry request %s, remain %s, err %s', url, args.retry, err);
if (args.retryDelay) {
debug('retry after %sms', args.retryDelay);
return sleep(args.retryDelay).then(() => self.request(url, args));
}
return self.request(url, args);
}
throw err;
Expand All @@ -33,6 +41,10 @@ HttpClient2.prototype.request = HttpClient2.prototype.curl = function request(ur
if (args.retry > 0 && typeof args.isRetry === 'function' && args.isRetry(res)) {
args.retry--;
debug('retry request %s, remain %s', url, args.retry);
if (args.retryDelay) {
debug('retry after %sms', args.retryDelay);
return sleep(args.retryDelay).then(() => self.request(url, args));
}
return self.request(url, args);
}
return res;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"digest-header": "^0.0.1",
"humanize-ms": "^1.2.0",
"iconv-lite": "^0.4.15",
"mz-modules": "^1.0.0",
"statuses": "^1.3.1"
},
"devDependencies": {
Expand Down
22 changes: 21 additions & 1 deletion test/httpclient2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var request = urllib.request;
var HttpClient = urllib.HttpClient2;

describe('test/httpclient2.test.js', function () {

var client;
var server;
before(function() {
Expand Down Expand Up @@ -74,6 +73,27 @@ describe('test/httpclient2.test.js', function () {
}).catch(done);
});

it('should request() with retry fail after 200ms', function (done) {
var count = 0;
muk(mockUrllib, 'request', function(url, args, callback) {
count++;
return request(url, args, callback);
});
var start = Date.now();
client.request('http://127.0.0.1:12345/500', {
timeout: 25000,
retry: 2,
retryDelay: 200,
}).then(function (result) {
should.ok(Buffer.isBuffer(result.data));
result.status.should.equal(500);
count.should.equal(3);
var use = Date.now() - start;
use.should.above(400);
done();
}).catch(done);
});

it('should request() with isRetry status', function (done) {
var count = 0;
muk(mockUrllib, 'request', function(url, args, callback) {
Expand Down

0 comments on commit 12c8eb9

Please sign in to comment.