Fix duplicate log messages in TCP server

This also fixes a potential concurrency issue by storing received data per socket connection instead of globally.
This commit is contained in:
Florian Reinhart 2018-07-20 14:33:03 +02:00 committed by GitHub
parent e73bd48445
commit beb4f5d40b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,22 +5,6 @@ const LoggingEvent = require('../LoggingEvent');
const DELIMITER = '__LOG4JS__';
let dataSoFar = '';
const send = (data) => {
if (data) {
dataSoFar += data;
if (dataSoFar.indexOf(DELIMITER)) {
const events = dataSoFar.split(DELIMITER);
if (!dataSoFar.endsWith(DELIMITER)) {
dataSoFar = events.pop();
}
events.filter(e => e.length).forEach((e) => {
clustering.send(LoggingEvent.deserialise(e));
});
}
}
};
exports.configure = (config) => {
debug('configure called with ', config);
// dummy shutdown if we're not master
@ -28,6 +12,24 @@ exports.configure = (config) => {
clustering.onlyOnMaster(() => {
const server = net.createServer((socket) => {
let dataSoFar = '';
const send = (data) => {
if (data) {
dataSoFar += data;
if (dataSoFar.indexOf(DELIMITER)) {
const events = dataSoFar.split(DELIMITER);
if (!dataSoFar.endsWith(DELIMITER)) {
dataSoFar = events.pop();
} else {
dataSoFar = '';
}
events.filter(e => e.length).forEach((e) => {
clustering.send(LoggingEvent.deserialise(e));
});
}
}
};
socket.setEncoding('utf8');
socket.on('data', send);
socket.on('end', send);