Skip to content

Commit

Permalink
Merge pull request #6 from JacksonTian/master
Browse files Browse the repository at this point in the history
空格改动和去掉单子模式
  • Loading branch information
aleafs committed May 13, 2012
2 parents 786e442 + b670cf8 commit 3da24c9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 49 deletions.
4 changes: 1 addition & 3 deletions index.js
@@ -1,4 +1,2 @@
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */

module.exports = require('./lib/itier-client.js');
module.exports = require(__dirname + '/lib/itier-client.js', true);

62 changes: 21 additions & 41 deletions lib/http-client.js
Expand Up @@ -5,16 +5,16 @@
* @author: zhangxc83@gmail.com * @author: zhangxc83@gmail.com
*/ */


var http = require('http'); var http = require('http');
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;


// change Agent.maxSockets to 1000 // change Agent.maxSockets to 1000
exports.agent = new http.Agent(); exports.agent = new http.Agent();
exports.agent.maxSockets = 1000; exports.agent.maxSockets = 1000;


/* {{{ private function Ping() */ /* {{{ private function Ping() */
var Ping = function(host, callback) { var Ping = function(host, callback) {
var pong = spawn('ping', ['-c1', '-W1', host]); var pong = spawn('ping', ['-c1', '-W1', host]);
pong.stdout.on('data', function(data) { pong.stdout.on('data', function(data) {
pong.kill(); pong.kill();
var res = data.toString().split("\n")[1]; var res = data.toString().split("\n")[1];
Expand All @@ -34,17 +34,17 @@ var Ping = function(host, callback) {
/* }}} */ /* }}} */


/* {{{ private function HeartBeat() */ /* {{{ private function HeartBeat() */
var HeartBeat = function(obj) { var HeartBeat = function(obj) {
var _onlines = []; var _onlines = [];
var _counts = obj.backup.length; var _counts = obj.backup.length;
obj.backup.forEach(function(item) { obj.backup.forEach(function(item) {
Ping(item.host, function(error, latency) { Ping(item.host, function(error, latency) {
_counts = _counts - 1; _counts = _counts - 1;
if (error) { if (error) {
return; return;
} }
_onlines.push(item); _onlines.push(item);
obj.online = _onlines; /**< 有一个就能跑,避免ping不通的机器阻塞请求 */ obj.online = _onlines; /**< 有一个就能跑,避免ping不通的机器阻塞请求 */


if (_counts == 0) { if (_counts == 0) {
setTimeout(function() { setTimeout(function() {
Expand All @@ -57,29 +57,9 @@ var HeartBeat = function(obj) {
/* }}} */ /* }}} */


/* {{{ exports instance() */ /* {{{ exports instance() */

exports.create = function (option) {
/** return new Client(option || {});
* @sington objects list };
*/
var __client_object = {};

/**
* @count of objects
*/
var __client_number = 0;
exports.create = function (option, name) {
if (!name) {
name = '__' + __client_number;
} else {
name = name.toLowerCase();
}
if (!__client_object[name]) {
__client_object[name] = new Client(option || {});
__client_number++;
}

return __client_object[name];
}
/* }}} */ /* }}} */


/* {{{ Client constructor() */ /* {{{ Client constructor() */
Expand All @@ -88,7 +68,7 @@ var Client = function (option) {
/** /**
* @配置信息 * @配置信息
*/ */
this.option = { this.option = {
prefix : '', /**< request url prefix */ prefix : '', /**< request url prefix */
timeout : 25000, /**< request timeout (ms) */ timeout : 25000, /**< request timeout (ms) */
heartbeat : 1000, /**< heartbeat interval(ms) */ heartbeat : 1000, /**< heartbeat interval(ms) */
Expand All @@ -100,22 +80,22 @@ var Client = function (option) {
/** /**
* @备用机器列表 * @备用机器列表
*/ */
this.backup = []; this.backup = [];


/** /**
* @在线机器列表 * @在线机器列表
*/ */
this.online = []; this.online = [];


/** /**
* @请求计数 * @请求计数
*/ */
this.reqnum = 0; this.reqnum = 0;


/** /**
* @心跳标记 * @心跳标记
*/ */
this._heart = false; this._heart = false;
} }
/* }}} */ /* }}} */


Expand All @@ -127,7 +107,7 @@ var Client = function (option) {
* @param port: Integer, optional * @param port: Integer, optional
* @return Object this * @return Object this
*/ */
Client.prototype.bind = function (host, port) { Client.prototype.bind = function (host, port) {
var item = { var item = {
host : host, host : host,
port : port, port : port,
Expand All @@ -143,21 +123,21 @@ Client.prototype.bind = function (host, port) {
/* }}} */ /* }}} */


/* {{{ Client prototype get() */ /* {{{ Client prototype get() */
Client.prototype.get = function (url, callback, header) { Client.prototype.get = function (url, callback, header) {
call_http_query(this, url, 'GET', null, header, callback); call_http_query(this, url, 'GET', null, header, callback);
return this; return this;
} }
/* }}} */ /* }}} */


/* {{{ Client prototype post() */ /* {{{ Client prototype post() */
Client.prototype.post = function (url, data, callback, header) { Client.prototype.post = function (url, data, callback, header) {
call_http_query(this, url, 'POST', data, header, callback); call_http_query(this, url, 'POST', data, header, callback);
return this; return this;
} }
/* }}} */ /* }}} */


/* {{{ Client prototype walk() */ /* {{{ Client prototype walk() */
Client.prototype.walk = function (url, data, callback, method, header) { Client.prototype.walk = function (url, data, callback, method, header) {
for (var i = 0; i < this.backup.length; i++) { for (var i = 0; i < this.backup.length; i++) {
call_http_query(this, url, method, data, header, callback, i); call_http_query(this, url, method, data, header, callback, i);
} }
Expand Down
9 changes: 5 additions & 4 deletions lib/itier-client.js
Expand Up @@ -5,12 +5,12 @@
* @author: zhangxc83@gmail.com * @author: zhangxc83@gmail.com
*/ */


var Client = require('./http-client.js'); var Client = require('./http-client.js');


/** /**
* @缓存控制常量 * @缓存控制常量
*/ */
var CACHE = { var CACHE = {
READ : 1, READ : 1,
WRITE : 2, WRITE : 2,
}; };
Expand Down Expand Up @@ -66,7 +66,7 @@ function Itier(options) {
Itier.prototype.connect = function(host, port) { Itier.prototype.connect = function(host, port) {
this.client.bind(host, port || 9999); this.client.bind(host, port || 9999);
return this; return this;
} };
/* }}} */ /* }}} */


/* {{{ Itier prototype query() */ /* {{{ Itier prototype query() */
Expand Down Expand Up @@ -200,5 +200,6 @@ exports.CACHE = CACHE;
exports.FETCH = FETCH; exports.FETCH = FETCH;
exports.createClient = function(options) { exports.createClient = function(options) {
return Itier(options); return Itier(options);
} };

exports.HttpClient = Client; exports.HttpClient = Client;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -13,7 +13,7 @@
"engines": { "engines": {
"node": ">=0.6.6" "node": ">=0.6.6"
}, },
"main": "index.js", "main": "./index.js",
"devDependencies": { "devDependencies": {
"should" : ">=0.4.2", "should" : ">=0.4.2",
"mocha" : ">=0.9.0", "mocha" : ">=0.9.0",
Expand Down

0 comments on commit 3da24c9

Please sign in to comment.