possible fixes for the blocking heartbeat problem

This commit is contained in:
Daniel Sommer 2022-05-03 12:26:15 +02:00
parent f8eb00e04e
commit cb011c903b
6 changed files with 32 additions and 45 deletions

View file

@ -4,8 +4,7 @@ const Message = require('./Message');
class Audiostream {
constructor(eventParser) {
this.eventParser = eventParser;
constructor() {
this.#handleEvents();
}
@ -33,23 +32,23 @@ class Audiostream {
}
#handleEvents() {
this.eventParser.on('audio:initialize', (data) => {
eventparser.on('audio:initialize', (data) => {
logger.debug('handling event \'audio:initialize\'...');
this.#initialize(data);
});
this.eventParser.on(constants.AUDIO_PLAY, () => {
eventparser.on(constants.AUDIO_PLAY, () => {
logger.debug('handling event \'' + constants.AUDIO_PLAY + '\'...');
global.player.play();
});
this.eventParser.on(constants.AUDIO_RESUME, () => {
eventparser.on(constants.AUDIO_RESUME, () => {
logger.debug('handling event \'' + constants.AUDIO_RESUME + '\'...');
global.player.resume();
});
this.eventParser.on(constants.AUDIO_PAUSE, () => {
eventparser.on(constants.AUDIO_PAUSE, () => {
logger.debug('handling event \'' + constants.AUDIO_PAUSE + '\'...');
global.player.pause();
});
this.eventParser.on(constants.AUDIO_STOP, () => {
eventparser.on(constants.AUDIO_STOP, () => {
logger.debug('handling event \'' + constants.AUDIO_STOP + '\'...');
global.player.stop();
});
@ -71,13 +70,10 @@ class Audiostream {
socket.on('error', (error) => {
logger.error('error connecting to audio server \'' + this.getTag() + '\': ' + error);
});
socket.on('close', () => {
logger.info('connection to audio server \'' + this.getTag() + '\' closed');
});
}
destroy() {
}
}

View file

@ -2,7 +2,6 @@ const util = require('../libs/util.js');
const net = require('net');
const Heartbeat = require('./Heartbeat.js');
const EventParser = require('./EventParser.js');
const Audiostream = require('./Audiostream.js');
class Connection {
@ -16,8 +15,7 @@ class Connection {
}
this.host = config?.server?.host || "127.0.0.1";
this.port = config?.server?.port || "3000";
this.eventParser = new EventParser();
this.audiostream = new Audiostream(this.eventParser);
this.audiostream = new Audiostream();
}
getHost() {
@ -53,7 +51,7 @@ class Connection {
#handleEventConnect(resolve, socket) {
logger.info('connected to communication server \'' + this.getTag() + '\'...');
this.socket = socket;
this.heartbeat = new Heartbeat(this.eventParser);
this.heartbeat = new Heartbeat();
this.#handleHeartbeat();
socket.on('timeout', () => {
this.#handleEventTimeout();
@ -67,7 +65,7 @@ class Connection {
}
async #handleEventData(data) {
this.eventParser.parse(data);
eventparser.parse(data);
}
#handleEventTimeout() {
@ -93,16 +91,12 @@ class Connection {
destroy() {
if (this.heartbeat !== undefined) {
this.heartbeat.removeAllListeners();
this.heartbeat.destroy();
this.heartbeat.removeAllListeners('timeout');
this.heartbeat = undefined;
}
if (this.socket !== undefined) {
this.socket.removeAllListeners('connect');
this.socket.removeAllListeners('error');
this.socket.removeAllListeners('timeout');
this.socket.removeAllListeners('close');
this.socket.removeAllListeners('data');
this.socket.removeAllListeners();
this.socket.end();
this.socket.destroy();
this.socket = undefined;

View file

@ -1,52 +1,47 @@
const { sleep } = require('../libs/util.js');
const EventEmitter = require('events');
const Message = require('./Message.js');
class Heartbeat extends EventEmitter {
constructor(eventParser) {
constructor() {
super();
this.interval = config?.server?.heartbeat || 10000;
this.eventParser = eventParser;
this.#listenForPingPong();
this.#sendPing();
}
}
async #sendPing() {
if (this.timeout !== undefined) {
clearTimeout(this.timeout);
if (this.destroyed === true) {
return;
}
if (this.alive === false) {
this.emit('timeout');
return;
} else if (this.alive === undefined) {
await new Promise((resolve, reject) => {
setTimeout(resolve, this.interval);
})
}
if (this.alive === undefined) {
await sleep(this.interval);
}
this.alive = false;
await new Message('ping').send();
this.timeout = setTimeout(() => {
this.#sendPing();
}, this.interval);
await sleep(this.interval);
this.#sendPing();
}
async #listenForPingPong() {
this.eventParser.on('ping', () => {
eventparser.on('ping', () => {
logger.debug('handling event \'ping\', responding with \'pong\'...');
new Message('pong').send();
});
this.eventParser.on('pong', () => {
eventparser.on('pong', () => {
logger.debug('handling event \'pong\'...');
this.alive = true;
});
}
destroy() {
if (this.timeout !== undefined) {
clearTimeout(this.timeout);
}
this.eventParser.removeAllListeners('ping');
this.eventParser.removeAllListeners('pong');
this.destroyed = true;
eventparser.removeAllListeners('ping');
eventparser.removeAllListeners('pong');
}
}

View file

@ -25,7 +25,7 @@ class Message {
return;
}
const data = this.toString();
logger.debug('sending data to \'' + socket.remoteAddress + ':' + socket.remotePort + '\': ' + data);
// logger.debug('sending data to \'' + socket.remoteAddress + ':' + socket.remotePort + '\': ' + data);
await new Promise((resolve, reject) => {
socket.write(data + constants.EVENT_DELIMITER, resolve);
});

View file

@ -1,9 +1,10 @@
const packageJSON = require('./package.json');
const path = require('path');
const Connection = require('./classes/Connection.js');
const Logger = require('./classes/Logger.js');
const EventParser = require('./classes/EventParser.js');
const Player = require('./classes/Player.js');
const Connection = require('./classes/Connection.js');
const INTERRUPTS = ['beforeExit', 'SIGINT', 'SIGTERM'];
@ -23,6 +24,7 @@ async function main() {
handleExit();
global.constants = require('./libs/constants.js');
global.logger.info("launching " + packageJSON.name + " " + packageJSON.version + "...");
global.eventparser = new EventParser();
global.player = new Player();
global.connection = new Connection();
while (true) {

View file

@ -20,4 +20,4 @@
"dependencies": {
"moment": "^2.29.1"
}
}
}