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