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: */

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

module.exports = require('./lib/itier-client.js');
62 changes: 21 additions & 41 deletions lib/http-client.js
Expand Up @@ -5,16 +5,16 @@
* @author: zhangxc83@gmail.com
*/

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

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

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

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

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

/* {{{ exports instance() */

/**
* @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];
}
exports.create = function (option) {
return new Client(option || {});
};
/* }}} */

/* {{{ Client constructor() */
Expand All @@ -88,7 +68,7 @@ var Client = function (option) {
/**
* @配置信息
*/
this.option = {
this.option = {
prefix : '', /**< request url prefix */
timeout : 25000, /**< request timeout (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
* @return Object this
*/
Client.prototype.bind = function (host, port) {
Client.prototype.bind = function (host, port) {
var item = {
host : host,
port : port,
Expand All @@ -143,21 +123,21 @@ Client.prototype.bind = function (host, port) {
/* }}} */

/* {{{ 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);
return this;
}
/* }}} */

/* {{{ 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);
return this;
}
/* }}} */

/* {{{ 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++) {
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
*/

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

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

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

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

0 comments on commit 3da24c9

Please sign in to comment.