From 14bcb363f0a9c8eaac34e827e32406f28d4011c1 Mon Sep 17 00:00:00 2001 From: Unitech Date: Fri, 1 Apr 2016 19:52:49 +0200 Subject: [PATCH] remove asynchronisity checks --- lib/Interactor/PushInteractor.js | 69 +++++++++----------------------- 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/lib/Interactor/PushInteractor.js b/lib/Interactor/PushInteractor.js index 2a39bde4..183dcc13 100644 --- a/lib/Interactor/PushInteractor.js +++ b/lib/Interactor/PushInteractor.js @@ -122,13 +122,9 @@ var PushInteractor = module.exports = { startPoolingWorker : function() { var self = this; - setTimeout(function() { - (function operate() { - debug('[PUSH] +---- Pooling: sending data ----+'); - PushInteractor.sendData(function() { - setTimeout(operate, cst.SEND_INTERVAL); - }); - })(); + setInterval(function() { + debug('[PUSH] +---- Pooling: sending data ----+'); + PushInteractor.sendData(); }, cst.SEND_INTERVAL); }, /** @@ -218,7 +214,7 @@ var PushInteractor = module.exports = { if (packet.process && !packet.server) { if (event === 'logs' && (JSON.stringify(self._packet[event]).length > logs_limit_size - || self._packet[event].length > 100)) + || self._packet[event].length > 100)) return console.error('Logs packet larger than 50KB limit'); self._packet[event].push(packet); @@ -261,6 +257,12 @@ var PushInteractor = module.exports = { sendData : function(cb) { var self = this; + if (self.conf._connection_is_up === false) { + debug('[CRITICAL] Connection is down, skipping data'); + self.resetPacket(); + return false; + } + this.preparePacket(function() { var data = {}; @@ -283,57 +285,22 @@ var PushInteractor = module.exports = { } var str = JSON.stringify(data); + var t1 = new Date(); self.resetPacket(); - if (self.conf._connection_is_up === true) { - var t1 = new Date(); + if (!self.socket) return false; - var _cb_called = false; + self.socket.client.sendv2(str, function() { + var duration_sec = (new Date() - t1) / 1000; + debugInfo('Time to flush data %ds', duration_sec); - /** - * Avoid that too much data get buffered in case of offline - */ - var timer = setTimeout(function() { - console.error('[FALLBACK] Clearing agent data cache to avoid leakage + skip async'); - self.resetPacket(); + if (duration_sec > 1) + console.info('[WARN] Time to send data over TCP took %dseconds!', duration_sec); - _cb_called = true; - return cb({msg : 'Fallback clear', code : 'TIMEOUT'}); - }, 1000 * 30); - - if (!self.socket) { - return process.nextTick(function() { - return cb({msg: 'pub socket not defined', code : 'NOTDEFINED'}); - }); - } - - self.socket.client.sendv2(str, function() { - if (_cb_called == true) { - console.info('[FALLBACK] Avoid to re-call async'); - return false; - } - - clearTimeout(timer); - - var duration_sec = (new Date() - t1) / 1000; - debugInfo('Time to flush data %ds', duration_sec); - - if (duration_sec > 1) - console.info('[WARN] Time to send data over TCP took %dseconds!', duration_sec); - - data = null; - str = null; - - return cb(); - }); - } else { - // If not connected skip data to avoid leakage - debug('[CRITICAL] Connection is down'); data = null; str = null; - return cb(); - } + }); }); } };