mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
lib: refactor internal/socket_list
PR-URL: https://github.com/nodejs/node/pull/11406 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
1 parent
f04387e9f2
commit
88c3f57cc3
1 file changed
+85
-84
+85
-84
@@ -1,104 +1,105 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {SocketListSend, SocketListReceive};
|
||||
|
||||
const EventEmitter = require('events');
|
||||
const util = require('util');
|
||||
|
||||
// This object keep track of the socket there are sended
|
||||
function SocketListSend(slave, key) {
|
||||
EventEmitter.call(this);
|
||||
class SocketListSend extends EventEmitter {
|
||||
constructor(child, key) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.slave = slave;
|
||||
_request(msg, cmd, callback) {
|
||||
var self = this;
|
||||
|
||||
if (!this.child.connected) return onclose();
|
||||
this.child.send(msg);
|
||||
|
||||
function onclose() {
|
||||
self.child.removeListener('internalMessage', onreply);
|
||||
callback(new Error('child closed before reply'));
|
||||
}
|
||||
|
||||
function onreply(msg) {
|
||||
if (!(msg.cmd === cmd && msg.key === self.key)) return;
|
||||
self.child.removeListener('disconnect', onclose);
|
||||
self.child.removeListener('internalMessage', onreply);
|
||||
|
||||
callback(null, msg);
|
||||
}
|
||||
|
||||
this.child.once('disconnect', onclose);
|
||||
this.child.on('internalMessage', onreply);
|
||||
}
|
||||
|
||||
close(callback) {
|
||||
this._request({
|
||||
cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
|
||||
key: this.key
|
||||
}, 'NODE_SOCKET_ALL_CLOSED', callback);
|
||||
}
|
||||
|
||||
getConnections(callback) {
|
||||
this._request({
|
||||
cmd: 'NODE_SOCKET_GET_COUNT',
|
||||
key: this.key
|
||||
}, 'NODE_SOCKET_COUNT', function(err, msg) {
|
||||
if (err) return callback(err);
|
||||
callback(null, msg.count);
|
||||
});
|
||||
}
|
||||
}
|
||||
util.inherits(SocketListSend, EventEmitter);
|
||||
|
||||
SocketListSend.prototype._request = function(msg, cmd, callback) {
|
||||
var self = this;
|
||||
|
||||
if (!this.slave.connected) return onclose();
|
||||
this.slave.send(msg);
|
||||
|
||||
function onclose() {
|
||||
self.slave.removeListener('internalMessage', onreply);
|
||||
callback(new Error('Slave closed before reply'));
|
||||
}
|
||||
|
||||
function onreply(msg) {
|
||||
if (!(msg.cmd === cmd && msg.key === self.key)) return;
|
||||
self.slave.removeListener('disconnect', onclose);
|
||||
self.slave.removeListener('internalMessage', onreply);
|
||||
|
||||
callback(null, msg);
|
||||
}
|
||||
|
||||
this.slave.once('disconnect', onclose);
|
||||
this.slave.on('internalMessage', onreply);
|
||||
};
|
||||
|
||||
SocketListSend.prototype.close = function close(callback) {
|
||||
this._request({
|
||||
cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
|
||||
key: this.key
|
||||
}, 'NODE_SOCKET_ALL_CLOSED', callback);
|
||||
};
|
||||
|
||||
SocketListSend.prototype.getConnections = function getConnections(callback) {
|
||||
this._request({
|
||||
cmd: 'NODE_SOCKET_GET_COUNT',
|
||||
key: this.key
|
||||
}, 'NODE_SOCKET_COUNT', function(err, msg) {
|
||||
if (err) return callback(err);
|
||||
callback(null, msg.count);
|
||||
});
|
||||
};
|
||||
|
||||
// This object keep track of the socket there are received
|
||||
function SocketListReceive(slave, key) {
|
||||
EventEmitter.call(this);
|
||||
class SocketListReceive extends EventEmitter {
|
||||
constructor(child, key) {
|
||||
super();
|
||||
|
||||
this.connections = 0;
|
||||
this.key = key;
|
||||
this.slave = slave;
|
||||
this.connections = 0;
|
||||
this.key = key;
|
||||
this.child = child;
|
||||
|
||||
function onempty(self) {
|
||||
if (!self.slave.connected) return;
|
||||
function onempty(self) {
|
||||
if (!self.child.connected) return;
|
||||
|
||||
self.slave.send({
|
||||
cmd: 'NODE_SOCKET_ALL_CLOSED',
|
||||
key: self.key
|
||||
self.child.send({
|
||||
cmd: 'NODE_SOCKET_ALL_CLOSED',
|
||||
key: self.key
|
||||
});
|
||||
}
|
||||
|
||||
this.child.on('internalMessage', (msg) => {
|
||||
if (msg.key !== this.key) return;
|
||||
|
||||
if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
|
||||
// Already empty
|
||||
if (this.connections === 0) return onempty(this);
|
||||
|
||||
// Wait for sockets to get closed
|
||||
this.once('empty', onempty);
|
||||
} else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
|
||||
if (!this.child.connected) return;
|
||||
this.child.send({
|
||||
cmd: 'NODE_SOCKET_COUNT',
|
||||
key: this.key,
|
||||
count: this.connections
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.slave.on('internalMessage', (msg) => {
|
||||
if (msg.key !== this.key) return;
|
||||
add(obj) {
|
||||
this.connections++;
|
||||
|
||||
if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
|
||||
// Already empty
|
||||
if (this.connections === 0) return onempty(this);
|
||||
// Notify previous owner of socket about its state change
|
||||
obj.socket.once('close', () => {
|
||||
this.connections--;
|
||||
|
||||
// Wait for sockets to get closed
|
||||
this.once('empty', onempty);
|
||||
} else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
|
||||
if (!this.slave.connected) return;
|
||||
this.slave.send({
|
||||
cmd: 'NODE_SOCKET_COUNT',
|
||||
key: this.key,
|
||||
count: this.connections
|
||||
});
|
||||
}
|
||||
});
|
||||
if (this.connections === 0) this.emit('empty', this);
|
||||
});
|
||||
}
|
||||
}
|
||||
util.inherits(SocketListReceive, EventEmitter);
|
||||
|
||||
SocketListReceive.prototype.add = function(obj) {
|
||||
this.connections++;
|
||||
|
||||
// Notify previous owner of socket about its state change
|
||||
obj.socket.once('close', () => {
|
||||
this.connections--;
|
||||
|
||||
if (this.connections === 0) this.emit('empty', this);
|
||||
});
|
||||
};
|
||||
module.exports = {SocketListSend, SocketListReceive};
|
||||
Reference in new issue
Block a user