Skip to content

Commit b60529f

Browse files
committedFeb 27, 2013
add toobusy for tcp acceptor
1 parent 99df341 commit b60529f

File tree

6 files changed

+637
-2
lines changed

6 files changed

+637
-2
lines changed
 

‎.jshintrc

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"describe",
44
"it",
55
"before",
6+
"beforeEach",
67
"after",
8+
"afterEach",
79
"window",
810
"__resources__"
911
],

‎lib/rpc-server/acceptors/tcp-acceptor.js

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var util = require('util');
33
var utils = require('../../util/utils');
44
var net = require('net');
55
var Composer = require('stream-pkg');
6+
var toobusy = require('toobusy');
67

78
var Acceptor = function(opts, cb){
89
EventEmitter.call(this);
@@ -47,6 +48,14 @@ pro.listen = function(port) {
4748

4849
socket.composer.on('data', function(data) {
4950
var pkg = JSON.parse(data.toString());
51+
if(toobusy()) {
52+
var resp = {
53+
id: pkg.id,
54+
resp: [cloneError(new Error('server too busy'))]
55+
};
56+
socket.write(socket.composer.compose(JSON.stringify(resp)));
57+
return;
58+
}
5059
if(pkg instanceof Array) {
5160
processMsgs(socket, self, pkg);
5261
} else {
@@ -72,6 +81,7 @@ pro.close = function() {
7281
return;
7382
}
7483
this.closed = true;
84+
toobusy.shutdown();
7585
if(this._interval) {
7686
clearInterval(this._interval);
7787
this._interval = null;
@@ -150,3 +160,8 @@ var flush = function(acceptor) {
150160
module.exports.create = function(opts, cb) {
151161
return new Acceptor(opts || {}, cb);
152162
};
163+
164+
process.on('SIGINT', function() {
165+
toobusy.shutdown();
166+
process.exit();
167+
});

‎lib/rpc-server/server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ module.exports.create = function(opts) {
5151
return gateway;
5252
};
5353

54-
module.exports.WSAcceptor = require('./acceptors/ws-acceptor');
54+
module.exports.WSAcceptor = require('./acceptors/ws-acceptor');
55+
module.exports.TcpAcceptor = require('./acceptors/tcp-acceptor');

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"socket.io-client": ">=0.9.4",
88
"pomelo-loader": "0.0.4",
99
"stream-pkg": "0.0.4",
10-
"pomelo-logger": "0.0.2"
10+
"pomelo-logger": "0.0.2",
11+
"toobusy": "0.2.2"
1112
},
1213
"devDependencies": {
1314
"mocha": ">=0.0.1",

‎test/rpc-client/tcp-mailbox.js

+308
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
var lib = process.env.POMELO_RPC_COV ? 'lib-cov' : 'lib';
2+
var should = require('should');
3+
var Mailbox = require('../../' + lib + '/rpc-client/mailboxes/tcp-mailbox');
4+
var Server = require('../../').server;
5+
6+
var WAIT_TIME = 100;
7+
8+
var paths = [
9+
{namespace: 'user', serverType: 'area', path: __dirname + '../../mock-remote/area'},
10+
{namespace: 'sys', serverType: 'connector', path: __dirname + '../../mock-remote/connector'}
11+
];
12+
13+
var port = 3333;
14+
15+
var server = {
16+
id: 'area-server-1',
17+
host: '127.0.0.1',
18+
port: port
19+
};
20+
21+
var msg = {
22+
namespace: 'user',
23+
serverType: 'area',
24+
service: 'addOneRemote',
25+
method: 'doService',
26+
args: [1]
27+
};
28+
29+
describe('tcp mailbox test', function() {
30+
var gateway;
31+
32+
before(function(done) {
33+
//start remote server
34+
var opts = {
35+
acceptorFactory: Server.TcpAcceptor,
36+
paths: paths,
37+
port: port,
38+
cacheMsg: true,
39+
interval: 30
40+
};
41+
42+
gateway = Server.create(opts);
43+
gateway.start();
44+
done();
45+
});
46+
47+
after(function(done) {
48+
//stop remote server
49+
gateway.stop();
50+
done();
51+
});
52+
53+
describe('#create', function() {
54+
it('should be ok for creating a mailbox and connect to the right remote server', function(done) {
55+
var mailbox = Mailbox.create(server);
56+
should.exist(mailbox);
57+
mailbox.connect(function(err) {
58+
should.not.exist(err);
59+
mailbox.close();
60+
done();
61+
});
62+
});
63+
64+
it('should return an error if connect fail', function(done) {
65+
var server = {
66+
id: "area-server-1",
67+
host: "127.0.0.1",
68+
port: -1000 //invalid port
69+
};
70+
71+
var mailbox = Mailbox.create(server);
72+
should.exist(mailbox);
73+
mailbox.connect(function(err) {
74+
should.exist(err);
75+
done();
76+
});
77+
});
78+
});
79+
80+
describe('#send', function() {
81+
it('should send request to remote server and get the response from callback function', function(done) {
82+
var mailbox = Mailbox.create(server);
83+
mailbox.connect(function(err) {
84+
should.not.exist(err);
85+
86+
mailbox.send(msg, null, function(err, res) {
87+
should.exist(res);
88+
res.should.equal(msg.args[0] + 1);
89+
mailbox.close();
90+
done();
91+
});
92+
});
93+
});
94+
95+
it('should distinguish different services and keep the right request/response relationship', function(done) {
96+
var value = 1;
97+
var msg1 = {
98+
namespace: 'user',
99+
serverType: 'area',
100+
service: 'addOneRemote',
101+
method: 'doService',
102+
args: [value]
103+
};
104+
var msg2 = {
105+
namespace: 'user',
106+
serverType: 'area',
107+
service: 'addOneRemote',
108+
method: 'doAddTwo',
109+
args: [value]
110+
};
111+
var msg3 = {
112+
namespace: 'user',
113+
serverType: 'area',
114+
service: 'addThreeRemote',
115+
method: 'doService',
116+
args: [value]
117+
};
118+
var callbackCount = 0;
119+
120+
var mailbox = Mailbox.create(server);
121+
mailbox.connect(function(err) {
122+
should.not.exist(err);
123+
124+
mailbox.send(msg1, null, function(err, res) {
125+
should.exist(res);
126+
res.should.equal(value + 1);
127+
callbackCount++;
128+
});
129+
130+
mailbox.send(msg2, null, function(err, res) {
131+
should.exist(res);
132+
res.should.equal(value + 2);
133+
callbackCount++;
134+
});
135+
136+
mailbox.send(msg3, null, function(err, res) {
137+
should.exist(res);
138+
res.should.equal(value + 3);
139+
callbackCount++;
140+
});
141+
});
142+
143+
setTimeout(function() {
144+
callbackCount.should.equal(3);
145+
if(!!mailbox) {
146+
mailbox.close();
147+
}
148+
done();
149+
}, WAIT_TIME);
150+
});
151+
152+
it('should distinguish different services and keep the right request/response relationship when use message cache mode', function(done) {
153+
var value = 1;
154+
var msg1 = {
155+
namespace: 'user',
156+
serverType: 'area',
157+
service: 'addOneRemote',
158+
method: 'doService',
159+
args: [value]
160+
};
161+
var msg2 = {
162+
namespace: 'user',
163+
serverType: 'area',
164+
service: 'addOneRemote',
165+
method: 'doAddTwo',
166+
args: [value]
167+
};
168+
var msg3 = {
169+
namespace: 'user',
170+
serverType: 'area',
171+
service: 'addThreeRemote',
172+
method: 'doService',
173+
args: [value]
174+
};
175+
var callbackCount = 0;
176+
177+
var mailbox = Mailbox.create(server, {cacheMsg: true});
178+
mailbox.connect(function(err) {
179+
should.not.exist(err);
180+
181+
mailbox.send(msg1, null, function(err, res) {
182+
should.exist(res);
183+
res.should.equal(value + 1);
184+
callbackCount++;
185+
});
186+
187+
mailbox.send(msg2, null, function(err, res) {
188+
should.exist(res);
189+
res.should.equal(value + 2);
190+
callbackCount++;
191+
});
192+
193+
mailbox.send(msg3, null, function(err, res) {
194+
should.exist(res);
195+
res.should.equal(value + 3);
196+
callbackCount++;
197+
});
198+
});
199+
200+
setTimeout(function() {
201+
callbackCount.should.equal(3);
202+
if(!!mailbox) {
203+
mailbox.close();
204+
}
205+
done();
206+
}, WAIT_TIME);
207+
});
208+
209+
it('should distinguish different services and keep the right request/response relationship if the client uses message cache mode but server not', function(done) {
210+
//start a new remote server without message cache mode
211+
var opts = {
212+
paths: paths,
213+
port: 3051
214+
};
215+
216+
var gateway = Server.create(opts);
217+
gateway.start();
218+
219+
var value = 1;
220+
var msg1 = {
221+
namespace: 'user',
222+
serverType: 'area',
223+
service: 'addOneRemote',
224+
method: 'doService',
225+
args: [value]
226+
};
227+
var msg2 = {
228+
namespace: 'user',
229+
serverType: 'area',
230+
service: 'addOneRemote',
231+
method: 'doAddTwo',
232+
args: [value]
233+
};
234+
var msg3 = {
235+
namespace: 'user',
236+
serverType: 'area',
237+
service: 'addThreeRemote',
238+
method: 'doService',
239+
args: [value]
240+
};
241+
var callbackCount = 0;
242+
243+
var mailbox = Mailbox.create(server, {cacheMsg: true});
244+
mailbox.connect(function(err) {
245+
should.not.exist(err);
246+
247+
mailbox.send(msg1, null, function(err, res) {
248+
should.exist(res);
249+
res.should.equal(value + 1);
250+
callbackCount++;
251+
});
252+
253+
mailbox.send(msg2, null, function(err, res) {
254+
should.exist(res);
255+
res.should.equal(value + 2);
256+
callbackCount++;
257+
});
258+
259+
mailbox.send(msg3, null, function(err, res) {
260+
should.exist(res);
261+
res.should.equal(value + 3);
262+
callbackCount++;
263+
});
264+
});
265+
266+
setTimeout(function() {
267+
callbackCount.should.equal(3);
268+
if(!!mailbox) {
269+
mailbox.close();
270+
}
271+
gateway.stop();
272+
done();
273+
}, WAIT_TIME);
274+
});
275+
});
276+
277+
describe('#close', function() {
278+
it('should emit a close event when mailbox close', function(done) {
279+
var closeEventCount = 0;
280+
var mailbox = Mailbox.create(server);
281+
mailbox.connect(function(err) {
282+
should.not.exist(err);
283+
mailbox.on('close', function() {
284+
closeEventCount++;
285+
});
286+
mailbox.close();
287+
});
288+
289+
setTimeout(function() {
290+
closeEventCount.should.equal(1);
291+
done();
292+
}, WAIT_TIME);
293+
});
294+
295+
it('should return an error when try to send message by a closed mailbox', function(done) {
296+
var mailbox = Mailbox.create(server);
297+
mailbox.connect(function(err) {
298+
should.not.exist(err);
299+
mailbox.close();
300+
mailbox.send(msg, null, function(err, res) {
301+
should.exist(err);
302+
done();
303+
});
304+
});
305+
});
306+
});
307+
308+
});

0 commit comments

Comments
 (0)