Fix yet another bug with node IDs

* Pick appropriate slot
* Log nodeId vs clientId/etc. for less confusion
This commit is contained in:
Bryan Ashby 2020-05-13 19:30:57 -06:00
parent 714f32f695
commit fd6bb47427
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
5 changed files with 22 additions and 21 deletions

View File

@ -96,7 +96,7 @@ function Client(/*input, output*/) {
Object.defineProperty(this, 'node', { Object.defineProperty(this, 'node', {
get : function() { get : function() {
return self.session.id + 1; return self.session.id;
} }
}); });

View File

@ -61,27 +61,27 @@ function getActiveConnectionList(authUsersOnly) {
function addNewClient(client, clientSock) { function addNewClient(client, clientSock) {
// //
// Assign ID/client ID to next lowest & available # // Find a node ID "slot"
// //
let id = 0; let nodeId;
for(let i = 0; i < clientConnections.length; ++i) { for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) {
if(clientConnections[i].id > id) { const existing = clientConnections.find(client => nodeId === client.node);
break; if (!existing) {
break; // available slot
} }
id++;
} }
client.session.id = id; client.session.id = nodeId;
const remoteAddress = client.remoteAddress = clientSock.remoteAddress; const remoteAddress = client.remoteAddress = clientSock.remoteAddress;
// create a unique identifier one-time ID for this session // create a unique identifier one-time ID for this session
client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ id, moment().valueOf() ]); client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ nodeId, moment().valueOf() ]);
clientConnections.push(client); clientConnections.push(client);
clientConnections.sort( (c1, c2) => c1.session.id - c2.session.id); clientConnections.sort( (c1, c2) => c1.session.id - c2.session.id);
// Create a client specific logger // Create a client specific logger
// Note that this will be updated @ login with additional information // Note that this will be updated @ login with additional information
client.log = logger.log.child( { clientId : id, sessionId : client.session.uniqueId } ); client.log = logger.log.child( { nodeId, sessionId : client.session.uniqueId } );
const connInfo = { const connInfo = {
remoteAddress : remoteAddress, remoteAddress : remoteAddress,
@ -101,7 +101,7 @@ function addNewClient(client, clientSock) {
{ client : client, connectionCount : clientConnections.length } { client : client, connectionCount : clientConnections.length }
); );
return id; return nodeId;
} }
function removeClient(client) { function removeClient(client) {
@ -114,7 +114,7 @@ function removeClient(client) {
logger.log.info( logger.log.info(
{ {
connectionCount : clientConnections.length, connectionCount : clientConnections.length,
clientId : client.session.id nodeId : client.node,
}, },
'Client disconnected' 'Client disconnected'
); );

View File

@ -86,9 +86,9 @@ module.exports = class DownloadQueue {
this.add(entry, true); // true=systemFile this.add(entry, true); // true=systemFile
// clean up after ourselves when the session ends // clean up after ourselves when the session ends
const thisClientId = this.client.session.id; const thisUniqueId = this.client.session.uniqueId;
Events.once(Events.getSystemEvents().ClientDisconnected, evt => { Events.once(Events.getSystemEvents().ClientDisconnected, evt => {
if(thisClientId === _.get(evt, 'client.session.id')) { if(thisUniqueId === _.get(evt, 'client.session.uniqueId')) {
FileEntry.removeEntry(entry, { removePhysFile : true }, err => { FileEntry.removeEntry(entry, { removePhysFile : true }, err => {
const Log = require('./logger').log; const Log = require('./logger').log;
if(err) { if(err) {

View File

@ -72,12 +72,12 @@ module.exports = class LoginServerModule extends ServerModule {
}); });
client.on('error', err => { client.on('error', err => {
logger.log.info({ clientId : client.session.id, error : err.message }, 'Connection error'); logger.log.info({ nodeId : client.node, error : err.message }, 'Connection error');
}); });
client.on('close', err => { client.on('close', err => {
const logFunc = err ? logger.log.info : logger.log.debug; const logFunc = err ? logger.log.info : logger.log.debug;
logFunc( { clientId : client.session.id }, 'Connection closed'); logFunc( { nodeId : client.node }, 'Connection closed');
clientConns.removeClient(client); clientConns.removeClient(client);
}); });

View File

@ -81,9 +81,9 @@ function userLogin(client, username, password, options, cb) {
if(existingClientConnection) { if(existingClientConnection) {
client.log.info( client.log.info(
{ {
existingClientId : existingClientConnection.session.id, existingNodeId : existingClientConnection.node,
username : user.username, username : user.username,
userId : user.userId userId : user.userId
}, },
'Already logged in' 'Already logged in'
); );
@ -97,11 +97,12 @@ function userLogin(client, username, password, options, cb) {
// update client logger with addition of username // update client logger with addition of username
client.log = logger.log.child( client.log = logger.log.child(
{ {
clientId : client.log.fields.clientId, nodeId : client.log.fields.nodeId,
sessionId : client.log.fields.sessionId, sessionId : client.log.fields.sessionId,
username : user.username, username : user.username,
} }
); );
client.log.info('Successful login'); client.log.info('Successful login');
// User's unique session identifier is the same as the connection itself // User's unique session identifier is the same as the connection itself