pomelo-rpc
Advanced tools
Comparing version 0.4.10 to 1.0.0
@@ -111,38 +111,2 @@ var Loader = require('pomelo-loader'); | ||
/** | ||
* Add new remote server to the rpc client. | ||
* | ||
* @param {Object} server new server information | ||
*/ | ||
pro.addServer = function(server) { | ||
this._station.addServer(server); | ||
}; | ||
/** | ||
* Batch version for add new remote server. | ||
* | ||
* @param {Array} servers server info list | ||
*/ | ||
pro.addServers = function(servers) { | ||
this._station.addServers(servers); | ||
}; | ||
/** | ||
* Remove remote server from the rpc client. | ||
* | ||
* @param {String|Number} id server id | ||
*/ | ||
pro.removeServer = function(id) { | ||
this._station.removeServer(id); | ||
}; | ||
/** | ||
* Batch version for remove remote server. | ||
* | ||
* @param {Array} ids remote server id list | ||
*/ | ||
pro.removeServers = function(ids) { | ||
this._station.removeServers(ids); | ||
}; | ||
/** | ||
* Replace remote servers. | ||
@@ -412,4 +376,2 @@ * | ||
return new Client(opts); | ||
}; | ||
module.exports.WSMailbox = require('./mailboxes/ws-mailbox'); | ||
}; |
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var utils = require('../../util/utils'); | ||
var Composer = require('stream-pkg'); | ||
var Composer = require('../../util/composer'); | ||
var net = require('net'); | ||
var Tracer = require('../../util/tracer'); | ||
var logger = require('pomelo-logger').getLogger('pomelo-rpc', __filename); | ||
var DEFAULT_CALLBACK_TIMEOUT = 10 * 1000; | ||
var DEFAULT_INTERVAL = 50; | ||
var MSG_TYPE = 0; | ||
var PING = 1; | ||
var PONG = 2; | ||
var RES_TYPE = 3; | ||
@@ -25,4 +31,12 @@ var MailBox = function(server, opts) { | ||
this.timeoutValue = opts.timeout || DEFAULT_CALLBACK_TIMEOUT; | ||
// Heartbeat ping interval. | ||
this.ping = 'ping' in opts ? opts.ping : 25e3; | ||
// Heartbeat pong response timeout. | ||
this.pong = 'pong' in opts ? opts.pong : 10e3; | ||
this.timer = {}; | ||
this.connected = false; | ||
this.closed = false; | ||
}; | ||
@@ -49,2 +63,3 @@ util.inherits(MailBox, EventEmitter); | ||
} | ||
self.heartbeat(); | ||
utils.invokeCallback(cb, err); | ||
@@ -56,7 +71,18 @@ }); | ||
this.composer.on('data', function(data) { | ||
var pkg = JSON.parse(data.toString()); | ||
if(pkg instanceof Array) { | ||
processMsgs(self, pkg); | ||
if(data[0] === PONG) { | ||
//incoming::pong | ||
self.heartbeat(); | ||
} else { | ||
processMsg(self, pkg); | ||
try { | ||
var pkg = JSON.parse(data.toString('utf-8', 1)); | ||
if(pkg instanceof Array) { | ||
processMsgs(self, pkg); | ||
} else { | ||
processMsg(self, pkg); | ||
} | ||
} catch(err) { | ||
if(err) { | ||
logger.error('tcp mailbox process data error: %j', err.stack); | ||
} | ||
} | ||
} | ||
@@ -74,6 +100,11 @@ }); | ||
} | ||
self.emit('error', err, self); | ||
self.emit('close', self.id); | ||
}); | ||
this.socket.on('end', function() { | ||
if(self.timer){ | ||
clearTimeout(self.timer['ping']); | ||
clearTimeout(self.timer['pong']); | ||
} | ||
self.socket.end(); | ||
self.emit('close', self.id); | ||
@@ -88,7 +119,3 @@ }); | ||
*/ | ||
pro.close = function() { | ||
if(this.closed) { | ||
return; | ||
} | ||
this.closed = true; | ||
pro.close = function() { | ||
if(this._interval) { | ||
@@ -111,3 +138,3 @@ clearInterval(this._interval); | ||
*/ | ||
pro.send = function(tracer, msg, opts, cb) { | ||
pro.send = function(tracer, msg, opts, cb) { | ||
tracer.info('client', __filename, 'send', 'tcp-mailbox try to send'); | ||
@@ -119,8 +146,3 @@ if(!this.connected) { | ||
if(this.closed) { | ||
utils.invokeCallback(cb, tracer, new Error('mailbox alread closed.')); | ||
return; | ||
} | ||
var id = this.curId++; | ||
var id = this.curId++ & 0xffffffff; | ||
this.requests[id] = cb; | ||
@@ -140,6 +162,67 @@ setCbTimeout(this, id, tracer, cb); | ||
} else { | ||
this.socket.write(this.composer.compose(JSON.stringify(pkg))); | ||
this.socket.write(this.composer.compose(MSG_TYPE, JSON.stringify(pkg), id)); | ||
} | ||
}; | ||
/** | ||
* Send a new heartbeat over the connection to ensure that we're still | ||
* connected and our internet connection didn't drop. We cannot use server side | ||
* heartbeats for this unfortunately. | ||
* | ||
* @api private | ||
*/ | ||
pro.heartbeat = function() { | ||
var self = this; | ||
if(!self.ping) return; | ||
self.connected = true; | ||
if(self.timer['pong']) { | ||
clearTimeout(self.timer['pong']); | ||
self.timer['pong'] = null; | ||
} | ||
if(!self.timer['ping']) { | ||
self.timer['ping'] = setTimeout(ping, self.ping); | ||
} | ||
/** | ||
* Exterminate the connection as we've timed out. | ||
* | ||
* @api private | ||
*/ | ||
function pong() { | ||
if(self.timer['pong']) { | ||
clearTimeout(self.timer['pong']); | ||
self.timer['pong'] = null; | ||
} | ||
// | ||
// The network events already captured the offline event. | ||
// | ||
if (!self.connected) return; | ||
self.connected = false; | ||
// notify application offline | ||
self.socket.removeAllListeners(); | ||
self.composer.removeAllListeners(); | ||
self.socket.end(); | ||
self.emit('close', self.id); | ||
logger.warn('pong timeout'); | ||
} | ||
/** | ||
* We should send a ping message to the server. | ||
* | ||
* @api private | ||
*/ | ||
function ping() { | ||
if(self.timer['ping']) { | ||
clearTimeout(self.timer['ping']); | ||
self.timer['ping'] = null; | ||
} | ||
self.socket.write(self.composer.compose(PING)); | ||
self.timer['pong'] = setTimeout(pong, self.pong); | ||
} | ||
}; | ||
var enqueue = function(mailbox, msg) { | ||
@@ -150,6 +233,6 @@ mailbox.queue.push(msg); | ||
var flush = function(mailbox) { | ||
if(mailbox.closed || !mailbox.queue.length) { | ||
if(!mailbox || !mailbox.queue.length) { | ||
return; | ||
} | ||
mailbox.socket.write(mailbox.composer.compose(JSON.stringify(mailbox.queue))); | ||
mailbox.socket.write(mailbox.composer.compose(MSG_TYPE, JSON.stringify(mailbox.queue), mailbox.queue[0].id)); | ||
mailbox.queue = []; | ||
@@ -189,2 +272,3 @@ }; | ||
logger.error('rpc callback timeout, remote server host: %s, port: %s', mailbox.host, mailbox.port); | ||
mailbox.emit('close', mailbox.id); | ||
utils.invokeCallback(cb, tracer, new Error('rpc callback timeout')); | ||
@@ -197,3 +281,3 @@ }, mailbox.timeoutValue); | ||
if(!mailbox.timeout[id]) { | ||
console.warn('timer not exists, id: %s', id); | ||
logger.warn('timer not exists, id: %s', id); | ||
return; | ||
@@ -213,4 +297,4 @@ } | ||
*/ | ||
module.exports.create = function(server, opts) { | ||
module.exports.create = function(server, opts) { | ||
return new MailBox(server, opts || {}); | ||
}; | ||
}; |
var util = require('util'); | ||
var utils = require('../../util/utils'); | ||
var client = require('socket.io-client'); | ||
var WebSocket = require('ws'); | ||
var Tracer = require('../../util/tracer'); | ||
@@ -22,3 +22,2 @@ var constants = require('../../util/constants'); | ||
this.connected = false; | ||
this.closed = false; | ||
this.opts = opts; | ||
@@ -38,5 +37,7 @@ }; | ||
var self = this; | ||
this.socket = client.connect(this.host + ':' + this.port, {'force new connection': true, 'reconnect': false}); | ||
this.socket = new WebSocket('ws://' + this.host + ':' + this.port); | ||
this.socket.on('message', function(pkg) { | ||
try{ | ||
try { | ||
pkg = JSON.parse(pkg); | ||
if(pkg instanceof Array) { | ||
@@ -47,8 +48,8 @@ processMsgs(self, pkg); | ||
} | ||
}catch(e) { | ||
console.error('rpc client process message with error: %j', e.stack); | ||
} catch(e) { | ||
logger.error('rpc client process message with error: %j', e.stack); | ||
} | ||
}); | ||
this.socket.on('connect', function() { | ||
this.socket.on('open', function() { | ||
if(self.connected) { | ||
@@ -87,6 +88,2 @@ return; | ||
pro.close = function() { | ||
if(this.closed) { | ||
return; | ||
} | ||
this.closed = true; | ||
if(this._interval) { | ||
@@ -114,8 +111,2 @@ clearInterval(this._interval); | ||
if(this.closed) { | ||
tracer.error('client', __filename, 'send', 'mailbox has already closed'); | ||
utils.invokeCallback(cb, tracer, new Error('ws-mailbox has already closed')); | ||
return; | ||
} | ||
var id = this.curId++; | ||
@@ -135,3 +126,3 @@ this.requests[id] = cb; | ||
} else { | ||
this.socket.emit('message', pkg); | ||
this.socket.send(JSON.stringify(pkg)); | ||
} | ||
@@ -145,6 +136,6 @@ }; | ||
var flush = function(mailbox) { | ||
if(mailbox.closed || !mailbox.queue.length) { | ||
if(!mailbox || !mailbox.queue.length) { | ||
return; | ||
} | ||
mailbox.socket.emit('message', mailbox.queue); | ||
mailbox.socket.send(JSON.stringify(mailbox.queue)); | ||
mailbox.queue = []; | ||
@@ -185,2 +176,3 @@ }; | ||
logger.error('rpc callback timeout, remote server host: %s, port: %s', mailbox.host, mailbox.port); | ||
mailbox.emit('close', mailbox.id); | ||
utils.invokeCallback(cb, tracer, new Error('rpc callback timeout')); | ||
@@ -210,2 +202,2 @@ }, mailbox.timeoutValue); | ||
return new MailBox(server, opts || {}); | ||
}; | ||
}; |
var util = require('util'); | ||
var utils = require('../util/utils'); | ||
var constants = require('../util/constants'); | ||
var defaultMailboxFactory = require('./mailbox'); | ||
var blackhole = require('./mailboxes/blackhole'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -23,5 +21,17 @@ var logger = require('pomelo-logger').getLogger('pomelo-rpc', __filename); | ||
this.serversMap = {}; // remote server info map, key: serverType, value: servers array | ||
this.onlines = {}; // remote server online map, key: server id, value: 0/offline 1/online | ||
this.mailboxFactory = opts.mailboxFactory || defaultMailboxFactory; | ||
this.mailboxes = {}; | ||
this.mailboxes.__defineGetter__('tcp', utils.load.bind(null, '../rpc-client/mailboxes/tcp-mailbox')); | ||
this.mailboxes.__defineGetter__('ws', utils.load.bind(null, '../rpc-client/mailboxes/ws-mailbox')); | ||
if(!!opts.mailboxName && opts.mailboxName === 'ws') { | ||
this.mailboxFactory = this.mailboxes.ws; | ||
} else { | ||
this.mailboxFactory = this.mailboxes.tcp; | ||
} | ||
if(!!opts.mailboxFactory) { | ||
this.mailboxFactory = opts.mailboxFactory; | ||
} | ||
// filters | ||
@@ -39,3 +49,3 @@ this.befores = []; | ||
// working mailbox map | ||
this.mailboxes = {}; | ||
this.mailboxesMap = {}; | ||
@@ -82,4 +92,4 @@ this.state = STATE_INITED; | ||
function closeAll() { | ||
for(var id in self.mailboxes) { | ||
self.mailboxes[id].close(); | ||
for(var id in self.mailboxesMap) { | ||
self.mailboxesMap[id].close(); | ||
} | ||
@@ -95,71 +105,2 @@ } | ||
/** | ||
* Add a new server info into the mail station and clear | ||
* the blackhole associated with the server id if any before. | ||
* | ||
* @param {Object} serverInfo server info such as {id, host, port} | ||
*/ | ||
pro.addServer = function(serverInfo) { | ||
if(!serverInfo || !serverInfo.id) { | ||
return; | ||
} | ||
var id = serverInfo.id; | ||
var type = serverInfo.serverType; | ||
this.servers[id] = serverInfo; | ||
this.onlines[id] = 1; | ||
if(!this.serversMap[type]) { | ||
this.serversMap[type] = []; | ||
} | ||
this.serversMap[type].push(id); | ||
this.emit('addServer', id); | ||
}; | ||
/** | ||
* Batch version for add new server info. | ||
* | ||
* @param {Array} serverInfos server info list | ||
*/ | ||
pro.addServers = function(serverInfos) { | ||
if(!serverInfos || !serverInfos.length) { | ||
return; | ||
} | ||
for(var i=0, l=serverInfos.length; i<l; i++) { | ||
this.addServer(serverInfos[i]); | ||
} | ||
}; | ||
/** | ||
* Remove a server info from the mail station and remove | ||
* the mailbox instance associated with the server id. | ||
* | ||
* @param {String|Number} id server id | ||
*/ | ||
pro.removeServer = function(id) { | ||
this.onlines[id] = 0; | ||
var mailbox = this.mailboxes[id]; | ||
if(mailbox) { | ||
mailbox.close(); | ||
delete this.mailboxes[id]; | ||
} | ||
this.emit('removeServer', id); | ||
}; | ||
/** | ||
* Batch version for remove remote servers. | ||
* | ||
* @param {Array} ids server id list | ||
*/ | ||
pro.removeServers = function(ids) { | ||
if(!ids || !ids.length) { | ||
return; | ||
} | ||
for(var i=0, l=ids.length; i<l; i++) { | ||
this.removeServer(ids[i]); | ||
} | ||
}; | ||
/** | ||
* Clear station infomation. | ||
@@ -169,5 +110,4 @@ * | ||
pro.clearStation = function() { | ||
this.onlines = {}; | ||
this.serversMap = {}; | ||
} | ||
}; | ||
@@ -188,3 +128,2 @@ /** | ||
var type = serverInfos[i].serverType; | ||
this.onlines[id] = 1; | ||
if(!this.serversMap[type]) { | ||
@@ -219,3 +158,3 @@ this.serversMap[type] = []; | ||
var self = this; | ||
var mailbox = this.mailboxes[serverId]; | ||
var mailbox = this.mailboxesMap[serverId]; | ||
if(!mailbox) { | ||
@@ -243,3 +182,3 @@ tracer.debug('client', __filename, 'dispatch', 'mailbox is not exist'); | ||
tracer.info('client', __filename, 'send', 'get corresponding mailbox and try to send message'); | ||
var mailbox = self.mailboxes[serverId]; | ||
var mailbox = self.mailboxesMap[serverId]; | ||
if(!!err) { | ||
@@ -324,3 +263,3 @@ errorHandler(tracer, self, err, serverId, msg, opts, true, cb); | ||
var self = this; | ||
var mailbox = self.mailboxes[serverId]; | ||
var mailbox = self.mailboxesMap[serverId]; | ||
mailbox.connect(tracer, function(err) { | ||
@@ -330,4 +269,4 @@ if(!!err) { | ||
logger.error('[pomelo-rpc] mailbox fail to connect to remote server: ' + serverId); | ||
if(!!self.mailboxes[serverId]) { | ||
delete self.mailboxes[serverId]; | ||
if(!!self.mailboxesMap[serverId]) { | ||
delete self.mailboxesMap[serverId]; | ||
} | ||
@@ -338,6 +277,6 @@ self.emit('error', constants.RPC_ERROR.FAIL_CONNECT_SERVER, tracer, serverId, null, self.opts); | ||
mailbox.on('close', function(id) { | ||
var mbox = self.mailboxes[id]; | ||
var mbox = self.mailboxesMap[id]; | ||
if(!!mbox) { | ||
delete self.mailboxesMap[id]; | ||
mbox.close(); | ||
delete self.mailboxes[id]; | ||
} | ||
@@ -394,13 +333,8 @@ self.emit('close', id); | ||
var server = station.servers[serverId]; | ||
var online = station.onlines[serverId]; | ||
if(!server) { | ||
logger.error('[pomelo-rpc] unknown server: %s', serverId); | ||
return false; | ||
} | ||
if(!online || online !== 1) { | ||
logger.error('[pomelo-rpc] server is not online: %s', serverId); | ||
} | ||
var mailbox = factory.create(server, station.opts); | ||
station.connecting[serverId] = true; | ||
station.mailboxes[serverId] = mailbox; | ||
station.mailboxesMap[serverId] = mailbox; | ||
station.connect(tracer, serverId, cb); | ||
@@ -427,3 +361,3 @@ return true; | ||
var pending = station.pendings[serverId]; | ||
var mailbox = station.mailboxes[serverId]; | ||
var mailbox = station.mailboxesMap[serverId]; | ||
if(!pending || !pending.length) { | ||
@@ -461,2 +395,2 @@ return; | ||
return new MailStation(opts || {}); | ||
}; | ||
}; |
@@ -5,6 +5,12 @@ var EventEmitter = require('events').EventEmitter; | ||
var net = require('net'); | ||
var Composer = require('stream-pkg'); | ||
var Composer = require('../../util/composer'); | ||
var Tracer = require('../../util/tracer'); | ||
var logger = require('pomelo-logger').getLogger('pomelo-rpc', __filename); | ||
var Acceptor = function(opts, cb){ | ||
var MSG_TYPE = 0; | ||
var PING = 1; | ||
var PONG = 2; | ||
var RES_TYPE = 3; | ||
var Acceptor = function(opts, cb) { | ||
EventEmitter.call(this); | ||
@@ -16,2 +22,8 @@ opts = opts || {}; | ||
this._interval = null; // interval object | ||
// Heartbeat ping interval. | ||
this.ping = 'ping' in opts ? opts.ping : 25e3; | ||
//ping timer for each client connection | ||
this.timer = {}; | ||
this.server = null; | ||
@@ -21,2 +33,3 @@ this.sockets = {}; | ||
this.cb = cb; | ||
this.socketId = 0; | ||
}; | ||
@@ -45,5 +58,8 @@ util.inherits(Acceptor, EventEmitter); | ||
this.server.on('connection', function(socket) { | ||
socket.id = self.socketId++; | ||
self.sockets[socket.id] = socket; | ||
socket.composer = new Composer({maxLength: self.pkgSize}); | ||
self.timer[socket.id] = null; | ||
self.heartbeat(socket.id); | ||
socket.on('data', function(data) { | ||
@@ -54,7 +70,22 @@ socket.composer.feed(data); | ||
socket.composer.on('data', function(data) { | ||
var pkg = JSON.parse(data.toString()); | ||
if(pkg instanceof Array) { | ||
processMsgs(socket, self, pkg); | ||
self.heartbeat(socket.id); | ||
if(data[0] === PING) { | ||
//incoming::ping | ||
socket.write(socket.composer.compose(PONG)); | ||
} else { | ||
processMsg(socket, self, pkg); | ||
try { | ||
var pkg = JSON.parse(data.toString('utf-8', 1)); | ||
var id = null; | ||
// | ||
if(pkg instanceof Array) { | ||
processMsgs(socket, self, pkg, id); | ||
} else { | ||
processMsg(socket, self, pkg, id); | ||
} | ||
} catch(err) { //json parse exception | ||
if(err) { | ||
socket.composer.reset(); | ||
logger.error(err); | ||
} | ||
} | ||
} | ||
@@ -66,2 +97,6 @@ }); | ||
delete self.msgQueues[socket.id]; | ||
if(self.timer[socket.id]){ | ||
clearTimeout(self.timer[socket.id]); | ||
} | ||
delete self.timer[socket.id]; | ||
}); | ||
@@ -77,2 +112,48 @@ }); | ||
/** | ||
* Send a new heartbeat over the connection to ensure that we're still | ||
* connected and our internet connection didn't drop. We cannot use server side | ||
* heartbeats for this unfortunately. | ||
* | ||
* @api private | ||
*/ | ||
pro.heartbeat = function(socketId) { | ||
var self = this; | ||
if(!self.ping) return; | ||
//incoming::pong | ||
self.connected = true; | ||
if(self.timer[socketId]) { | ||
clearTimeout(self.timer[socketId]); | ||
self.timer[socketId] = null; | ||
} | ||
self.timer[socketId] = setTimeout(ping, self.ping + 5e3); | ||
logger.info('set ping with socket id: %s' ,socketId); | ||
/** | ||
* Exterminate the connection as we've timed out. | ||
* | ||
* @api private | ||
*/ | ||
function ping() { | ||
// notify application offline | ||
//remove listener on socket | ||
if(self.timer[socketId]){ | ||
clearTimeout(self.timer[socketId]); | ||
} | ||
delete self.timer[socketId]; | ||
self.sockets[socketId].composer.removeAllListeners(); | ||
self.sockets[socketId].removeAllListeners(); | ||
self.sockets[socketId].end(); | ||
delete self.sockets[socketId]; | ||
delete self.msgQueues[socketId]; | ||
logger.warn('ping timeout with socket id: %s', socketId); | ||
} | ||
}; | ||
pro.close = function() { | ||
@@ -90,3 +171,3 @@ if(!!this.closed) { | ||
} catch(err) { | ||
console.error('rpc server close error: %j', err.stack); | ||
logger.error('rpc server close error: %j', err.stack); | ||
} | ||
@@ -104,4 +185,4 @@ this.emit('closed'); | ||
}; | ||
var processMsg = function(socket, acceptor, pkg) { | ||
//need to redefine response | ||
var processMsg = function(socket, acceptor, pkg, id) { | ||
var tracer = new Tracer(acceptor.rpcLogger, acceptor.rpcDebugLog, pkg.remote, pkg.source, pkg.msg, pkg.traceId, pkg.seqId); | ||
@@ -126,3 +207,3 @@ tracer.info('server', __filename, 'processMsg', 'tcp-acceptor receive message and try to process message'); | ||
} else { | ||
socket.write(socket.composer.compose(JSON.stringify(resp))); | ||
socket.write(socket.composer.compose(RES_TYPE, JSON.stringify(resp), id)); | ||
} | ||
@@ -132,5 +213,5 @@ }); | ||
var processMsgs = function(socket, acceptor, pkgs) { | ||
var processMsgs = function(socket, acceptor, pkgs, id) { | ||
for(var i=0, l=pkgs.length; i<l; i++) { | ||
processMsg(socket, acceptor, pkgs[i]); | ||
processMsg(socket, acceptor, pkgs[i], id); | ||
} | ||
@@ -146,3 +227,3 @@ }; | ||
}; | ||
//need modify | ||
var flush = function(acceptor) { | ||
@@ -149,0 +230,0 @@ var sockets = acceptor.sockets, queues = acceptor.msgQueues, queue, socket; |
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var utils = require('../../util/utils'); | ||
var sio = require('socket.io'); | ||
var WebSocketServer = require('ws').Server; | ||
var logger = require('pomelo-logger').getLogger('pomelo-rpc', __filename); | ||
@@ -34,7 +34,5 @@ var Tracer = require('../../util/tracer'); | ||
this.server = sio.listen(port); | ||
this.server = new WebSocketServer({ port: port }); | ||
this.server.set('log level', 0); | ||
this.server.server.on('error', function(err) { | ||
this.server.on('error', function(err) { | ||
logger.error('rpc server is error: %j', err.stack); | ||
@@ -44,9 +42,11 @@ self.emit('error', err); | ||
this.server.sockets.on('connection', function(socket) { | ||
this.server.on('connection', function(socket) { | ||
self.sockets[socket.id] = socket; | ||
self.emit('connection', {id: socket.id, ip: socket.handshake.address.address}); | ||
self.emit('connection', {id: socket.id, ip: socket._socket.remoteAddress}); | ||
socket.on('message', function(pkg) { | ||
try { | ||
pkg = JSON.parse(pkg); | ||
if(pkg instanceof Array) { | ||
@@ -58,3 +58,2 @@ processMsgs(socket, self, pkg); | ||
} catch(e) { | ||
// socke.io would broken if uncaugth the exception | ||
logger.error('rpc server process message error: %j', e.stack); | ||
@@ -118,3 +117,3 @@ } | ||
try { | ||
this.server.server.close(); | ||
this.server.close(); | ||
} catch(err) { | ||
@@ -155,3 +154,3 @@ logger.error('rpc server close error: %j', err.stack); | ||
} else { | ||
socket.emit('message', resp); | ||
socket.send(JSON.stringify(resp)); | ||
} | ||
@@ -188,3 +187,3 @@ }); | ||
} | ||
socket.emit('message', queue); | ||
socket.send(JSON.stringify(queue)); | ||
queues[socketId] = []; | ||
@@ -191,0 +190,0 @@ } |
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var utils = require('../util/utils'); | ||
var defaultAcceptorFactory = require('./acceptor'); | ||
var Dispatcher = require('./dispatcher'); | ||
@@ -15,5 +14,3 @@ var fs = require('fs'); | ||
this.stoped = false; | ||
this.acceptorFactory = opts.acceptorFactory || defaultAcceptorFactory; | ||
this.services = opts.services; | ||
var dispatcher = new Dispatcher(this.services); | ||
if(!!this.opts.reloadRemotes) { | ||
@@ -23,2 +20,18 @@ watchServices(this, dispatcher); | ||
var self = this; | ||
this.acceptors = {}; | ||
this.acceptors.__defineGetter__('tcp', utils.load.bind(null, '../rpc-server/acceptors/tcp-acceptor')); | ||
this.acceptors.__defineGetter__('ws', utils.load.bind(null,'../rpc-server/acceptors/ws-acceptor')); | ||
if(!!opts.acceptorName && opts.acceptorName === 'ws') { | ||
this.acceptorFactory = this.acceptors.ws; | ||
} else { | ||
this.acceptorFactory = this.acceptors.tcp; | ||
} | ||
if(!!opts.acceptorFactory) { | ||
this.acceptorFactory = opts.acceptorFactory; | ||
} | ||
var dispatcher = new Dispatcher(this.services); | ||
this.acceptor = this.acceptorFactory.create(opts, function(tracer, msg, cb) { | ||
@@ -28,2 +41,3 @@ dispatcher.route(tracer, msg, cb); | ||
}; | ||
util.inherits(Gateway, EventEmitter); | ||
@@ -30,0 +44,0 @@ |
@@ -52,4 +52,1 @@ var Loader = require('pomelo-loader'); | ||
}; | ||
module.exports.WSAcceptor = require('./acceptors/ws-acceptor'); | ||
module.exports.TcpAcceptor = require('./acceptors/tcp-acceptor'); |
@@ -17,3 +17,3 @@ module.exports = { | ||
, FAILSAFE_CONNECT_TIME: 5 * 1000 | ||
, CALLBACK_TIMEOUT: 30 * 1000 | ||
, CALLBACK_TIMEOUT: 15 * 1000 | ||
, INTERVAL: 50 | ||
@@ -20,0 +20,0 @@ , GRACE_TIMEOUT: 3 * 1000 |
@@ -32,1 +32,5 @@ var exp = module.exports; | ||
}; | ||
exp.load = function(path) { | ||
return require(path); | ||
}; |
{ | ||
"name": "pomelo-rpc", | ||
"version": "0.4.10", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"socket.io": "0.9.16", | ||
"ws": "0.8.0", | ||
"crc": "0.2.0", | ||
"socket.io-client": "0.9.16", | ||
"pomelo-loader": "0.0.6", | ||
"stream-pkg": "0.0.5", | ||
"pomelo-logger": "0.1.7", | ||
@@ -11,0 +9,0 @@ "node-uuid": "1.4.0", |
@@ -115,7 +115,2 @@ #pomelo-rpc - rpc framework for pomelo | ||
###client.addServers(servers) | ||
Add new remote server informations. | ||
####Parameters | ||
+ servers - remote server information list. Format: [{id: remote_server_id, serverType: remote_server_type, host: remote_server_host, port: remote_server_port}] | ||
###client.start(cb) | ||
@@ -122,0 +117,0 @@ Start the RPC client. |
var Client = require('..').client; | ||
var config = require('./config.json'); | ||
//for test param | ||
var mailboxName = config.protocol || 'tcp'; | ||
var period = config.interval; //ms | ||
var msg = config.msg; | ||
var host = config.host || '127.0.0.1'; | ||
var port = config.port || 8080; | ||
// remote service interface path info list | ||
@@ -14,3 +22,3 @@ var records = [ | ||
var servers = [ | ||
{id: 'test-server-1', serverType: 'test', host: '127.0.0.1', port: 3333} | ||
{id: 'test-server-1', serverType: 'test', host: host, port: port} | ||
]; | ||
@@ -29,3 +37,3 @@ | ||
var client = Client.create({routeContext: routeContext, router: routeFunc, context: context}); | ||
var client = Client.create({routeContext: routeContext, router: routeFunc, context: context, mailboxName: mailboxName}); | ||
@@ -36,10 +44,19 @@ client.start(function(err) { | ||
client.addProxies(records); | ||
client.addServers(servers); | ||
client.replaceServers(servers); | ||
client.proxies.user.test.service.echo(routeParam, 'hello', function(err, resp) { | ||
if(err) { | ||
console.error(err.stack); | ||
} | ||
console.log(resp); | ||
}); | ||
}); | ||
var func = function(){ | ||
client.proxies.user.test.service.echo(routeParam, msg, function(err, resp) { | ||
if(err) { | ||
console.error(err.stack); | ||
} | ||
console.log(resp); | ||
}); | ||
} | ||
setInterval(func, period); | ||
}); | ||
process.on('uncaughtException', function (err) { | ||
console.error('Caught exception: ', err.stack); | ||
}); |
var Server = require('..').server; | ||
var config = require('./config.json'); | ||
var acceptorName = config.protocol || 'tcp'; | ||
// remote service path info list | ||
@@ -8,6 +10,6 @@ var paths = [ | ||
var port = 3333; | ||
var port = config.port || 8080; | ||
var server = Server.create({paths: paths, port: port}); | ||
var server = Server.create({paths: paths, port: port, acceptorName: acceptorName}); | ||
server.start(); | ||
console.log('rpc server started.'); | ||
console.log('rpc server started.'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
6
0
100
1
3
144593
47
4349
142
+ Addedws@0.8.0
+ Addedbindings@1.2.1(transitive)
+ Addedbufferutil@1.2.1(transitive)
+ Addednan@2.22.02.4.0(transitive)
+ Addedultron@1.0.2(transitive)
+ Addedutf-8-validate@1.2.2(transitive)
+ Addedws@0.8.0(transitive)
- Removedsocket.io@0.9.16
- Removedsocket.io-client@0.9.16
- Removedstream-pkg@0.0.5
- Removedactive-x-obfuscator@0.0.1(transitive)
- Removedbase64id@0.1.0(transitive)
- Removedcommander@2.1.0(transitive)
- Removednan@1.0.0(transitive)
- Removedpolicyfile@0.0.4(transitive)
- Removedredis@0.7.3(transitive)
- Removedsocket.io@0.9.16(transitive)
- Removedsocket.io-client@0.9.16(transitive)
- Removedstream-pkg@0.0.5(transitive)
- Removedtinycolor@0.0.1(transitive)
- Removeduglify-js@1.2.5(transitive)
- Removedws@0.4.32(transitive)
- Removedxmlhttprequest@1.4.2(transitive)
- Removedzeparser@0.0.5(transitive)