Skip to content

Commit

Permalink
improve debugger already connected handling (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
yehiyam authored Apr 25, 2022
1 parent ddf572c commit 7929fa1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/algorithm-debug/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const configIt = require('@hkube/config');
const Logger = require('@hkube/logger');
const component = require('./lib/algorithm-communication/consts/component-names').MAIN;
const component = require('./lib/consts/component-names').MAIN;
const { main: config, logger } = configIt.load();
const log = new Logger(config.serviceName, logger);

Expand Down

This file was deleted.

19 changes: 12 additions & 7 deletions core/algorithm-debug/lib/algorithm-communication/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const url = require('url');
const Logger = require('@hkube/logger');
const { Encoding } = require('@hkube/encoding');
const Validator = require('ajv');
const schema = require('./schema').socketWorkerCommunicationSchema;
const component = require('./consts').Components.COMMUNICATIONS;
const { socketWorkerCommunicationSchema: schema } = require('./schema');
const { Components, debugMessages } = require('../consts');
const component = Components.COMMUNICATIONS;
const validator = new Validator({ useDefaults: true, coerceTypes: true });
let log;
let connectedPromise;
Expand All @@ -16,6 +17,7 @@ class WsWorkerCommunication extends EventEmitter {
super();
this._socketServer = null;
this._socket = null;
this._queryData = null;
connectedPromise = new Promise((res) => {
resolveConnected = res;
});
Expand All @@ -35,13 +37,14 @@ class WsWorkerCommunication extends EventEmitter {

this._socketServer.on('connection', (socket, opt) => {
if (this._socketServer.clients.size > 1) {
log.info('Debugger connected already');
socket.close(1013);
log.info(`Debugger already connected from ${this._queryData?.hostname}`);
socket.send(this._encoding.encode({ command: debugMessages.outgoing.alreadyConnectedError, data: { hostname: this._queryData?.hostname } }));
socket.close(debugMessages.codes.close);
}
else {
const data = url.parse(opt.url, true).query;
log.info('Connected!!!', { component });
this._registerSocketMessages(socket);
log.info(`Debugger connected from ${data.hostname || 'unknown'}`, { component });
this._registerSocketMessages(socket, data);
this.setEncodingType(data.encoding);
this.emit('connection', data);
}
Expand All @@ -67,8 +70,9 @@ class WsWorkerCommunication extends EventEmitter {
this._encoding = new Encoding({ type });
}

_registerSocketMessages(socket) {
_registerSocketMessages(socket, queryData) {
this._socket = socket;
this._queryData = queryData;
resolveConnected();
socket.on('message', (data) => {
const payload = this._encoding.decode(data);
Expand All @@ -79,6 +83,7 @@ class WsWorkerCommunication extends EventEmitter {
log.info('Debug connection closed');
const reason = code === 1006 ? 'CLOSE_ABNORMAL' : `${code}`;
this._socket = null;
this._queryData = null;
connectedPromise = new Promise((res) => {
resolveConnected = res;
});
Expand Down
6 changes: 6 additions & 0 deletions core/algorithm-debug/lib/consts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Components = require('./component-names');
const debugMessages = require('./messages');
module.exports = {
Components,
debugMessages
};
8 changes: 8 additions & 0 deletions core/algorithm-debug/lib/consts/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
outgoing: {
alreadyConnectedError: 'alreadyConnectedError',
},
codes: {
close: 4001
}
};
22 changes: 10 additions & 12 deletions core/algorithm-debug/tests/debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const configIt = require('@hkube/config');
const { once } = require('events');
const { main: config } = configIt.load();
const { expect } = require('chai');
const { messages } = require('@hkube/nodejs-wrapper');
const { debugMessages } = require('../lib/consts');
const WebSocket = require('ws');
let app;
// const Logger = require('@hkube/logger');
Expand Down Expand Up @@ -467,18 +469,14 @@ describe('Debug', () => {
});

it('connect twice', async () => {
socket = new WebSocket(combinedUrl, {});
const socket2 = new WebSocket(combinedUrl, {});
let resolveGotAlreadyConnected;
const gotConnectedAlready = new Promise((res, rej) => {
resolveGotAlreadyConnected = res;
});
socket = new WebSocket(`${combinedUrl}&hostname=host 1`, {});
const socket2 = new WebSocket(`${combinedUrl}&hostname=host 2`, {});
const [alreadyConnectedRes] = await once(socket2, 'message');
const decodedData = encoding.decode(alreadyConnectedRes);
expect(decodedData.command).to.eql(debugMessages.outgoing.alreadyConnectedError);
expect(decodedData.data.hostname).to.eql('host 1')
const [closeCode] = await once(socket2, 'close');
expect(closeCode).to.eql(debugMessages.codes.close);

socket2.on("close", (code) => {
if (code == 1013) {
resolveGotAlreadyConnected();
}
})
await gotConnectedAlready;
});
});

0 comments on commit 7929fa1

Please sign in to comment.