mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
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:
parent
e73bd48445
commit
beb4f5d40b
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user