Skip to content

Commit c0d0dc2

Browse files
committed
Add user-agent support
1 parent 1b7e87d commit c0d0dc2

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
## About
55

6-
`httpha`
6+
`httpha` is a simple client-side load balance and HA module.
77

88
## Usage
99

@@ -13,13 +13,16 @@ var httpha = require('httpha');
1313

1414
var ha = httpha.create({
1515
'interval' : 1000,
16-
}, httpha.httpStatusChecker('/status', 1000));
16+
}, httpha.httpStatusChecker('/status', {
17+
'timeout' : 1000,
18+
}));
1719

1820
ha.add({'hostname' : '127.0.0.1', 'port' : 8080});
1921
ha.add({'hostname' : '127.0.0.2', 'port' : 8080});
2022
ha.add({'hostname' : '127.0.0.1', 'port' : 8081});
2123

22-
ha.fetch();
24+
console.log(ha.fetch());
25+
console.log(ha.fetch());
2326

2427
```
2528

lib/httpha.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ exports.create = function (options, helper) {
107107
return _me;
108108
};
109109

110-
exports.httpStatusChecker = function (request, timeout) {
110+
exports.httpStatusChecker = function (request, options) {
111111
request = String(request).trim();
112112
if ('/' !== request.substring(0, 1)) {
113113
request = '/' + request;
114114
}
115115

116-
timeout = parseInt(timeout, 10) || 1000;
116+
var configs = _extend({
117+
'timeout' : 1000,
118+
'useragent' : 'HttpHA/0.1.0',
119+
}, options);
117120

118121
return function (one, done) {
119122
var tmo = null;
@@ -128,6 +131,9 @@ exports.httpStatusChecker = function (request, timeout) {
128131
var req = http.request(_extend(one, {
129132
'method' : 'HEAD',
130133
'path' : request,
134+
'headers' : {
135+
'User-Agent' : configs.useragent,
136+
}
131137
}), function (res) {
132138
if (tmo) {
133139
clearTimeout(tmo);
@@ -140,10 +146,10 @@ exports.httpStatusChecker = function (request, timeout) {
140146

141147
tmo = setTimeout(function () {
142148
req.abort();
143-
var err = new Error('HeartBeat request "' + request + '" timeout after ' + timeout + 'ms');
149+
var err = new Error('HeartBeat request "' + request + '" timeout after ' + configs.timeout + 'ms');
144150
err.name = 'RequestTimeout';
145151
callback(err);
146-
}, timeout);
152+
}, configs.timeout);
147153

148154
req.once('error', function (err) {
149155
callback(err);

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "httpha",
3-
"description": "",
4-
"keywords": [ "system", "infomation", "usage" ],
5-
"version": "0.0.1",
3+
"description": "`httpha` is a simple client-side load balance and HA module.",
4+
"keywords": [ "load balance", "high available", "HA", "client-side" ],
5+
"version": "0.1.0",
66
"author": "Aleafs Zhang ([email protected])",
77
"repository": {
88
"type": "git",

test/httpha.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ describe('http status checker', function () {
117117
var wait = function (idx, cb) {
118118
all[idx] = true;
119119

120-
var _fn = httpha.httpStatusChecker(idx, 20);
120+
var _fn = httpha.httpStatusChecker(idx, {
121+
'timeout' : 20,
122+
});
121123
_fn({'hostname' : '127.0.0.1', 'port' : P}, function (err, yes) {
122124
cb(err, yes);
123125
all[idx] = null;

0 commit comments

Comments
 (0)