fixed server connection status bug

This commit is contained in:
Jan Prochazka
2021-01-23 17:08:17 +01:00
parent e2ea2809b6
commit c9fefd14fd

View File

@@ -2,6 +2,8 @@ const connections = require('./connections');
const socket = require('../utility/socket'); const socket = require('../utility/socket');
const { fork } = require('child_process'); const { fork } = require('child_process');
const _ = require('lodash'); const _ = require('lodash');
const AsyncLock = require('async-lock');
const lock = new AsyncLock();
module.exports = { module.exports = {
opened: [], opened: [],
@@ -22,6 +24,7 @@ module.exports = {
handle_ping() {}, handle_ping() {},
async ensureOpened(conid) { async ensureOpened(conid) {
const res = await lock.acquire(conid, async () => {
const existing = this.opened.find(x => x.conid == conid); const existing = this.opened.find(x => x.conid == conid);
if (existing) return existing; if (existing) return existing;
const connection = await connections.get({ conid }); const connection = await connections.get({ conid });
@@ -50,6 +53,8 @@ module.exports = {
}); });
subprocess.send({ msgtype: 'connect', ...connection }); subprocess.send({ msgtype: 'connect', ...connection });
return newOpened; return newOpened;
});
return res;
}, },
close(conid, kill = true) { close(conid, kill = true) {
@@ -82,10 +87,12 @@ module.exports = {
ping_meta: 'post', ping_meta: 'post',
async ping({ connections }) { async ping({ connections }) {
for (const conid of connections) { await Promise.all(
connections.map(async conid => {
const opened = await this.ensureOpened(conid); const opened = await this.ensureOpened(conid);
opened.subprocess.send({ msgtype: 'ping' }); opened.subprocess.send({ msgtype: 'ping' });
} })
);
return { status: 'ok' }; return { status: 'ok' };
}, },