From c6608dd1eb6f30fedc75d81d885a3745740ac4ab Mon Sep 17 00:00:00 2001 From: tarotlwei Date: Tue, 24 Jul 2018 20:51:14 +0800 Subject: [PATCH 1/2] feat(websocket): optimize report log --- bin/proxy/websocket.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/proxy/websocket.js b/bin/proxy/websocket.js index eac36702..6bea3c5f 100644 --- a/bin/proxy/websocket.js +++ b/bin/proxy/websocket.js @@ -34,6 +34,7 @@ function wsFiller(ws, req) { ws.logReportTimer = null; ws.__tempSend = ws.send; ws.reportIndex = 1; + ws.messageTriggerCount = 0; ws.send = function(message) { if (ws.readyState == WebSocket.OPEN) { @@ -49,19 +50,26 @@ function wsFiller(ws, req) { ws.logKey = req.headers['sec-websocket-key'] || Math.random(); } +function emitReportLig(ws, type) { + ws.upgradeReq.emit(type); + ws.messageTriggerCount = 0; +} + function reportWebSocketLog(ws, isEnd) { // 这里触发log上报 const logLength = logger.getTextLength(); // 每次上报log时,先看下Log多不多,不多的话,延迟上报下 clearTimeout(ws.logReportTimer); if (isEnd) { - ws.upgradeReq.emit('reportLog'); + emitReportLig('reportLog'); } else if (logLength > 30) { // 立即上报 - ws.upgradeReq.emit('reportLogStream'); + emitReportLig('reportLogStream'); + } else if (ws.messageTriggerCount > 9) { + emitReportLig('reportLogStream'); } else { ws.logReportTimer = setTimeout(function() { - ws.upgradeReq.emit('reportLogStream'); + emitReportLig('reportLogStream'); }, 5000); } } @@ -186,6 +194,7 @@ function bind_listen(server) { }; ws.on('message', function(message) { + ws.messageTriggerCount++; logger.debug('server get message : ${message}', { message }); From 0f08a1942f88e3cc26834086d5ecc643f8e6f4ed Mon Sep 17 00:00:00 2001 From: tarotlwei Date: Wed, 25 Jul 2018 10:56:14 +0800 Subject: [PATCH 2/2] chore(websocket): dead code removal --- bin/proxy/http.proxy.js | 4 ---- bin/proxy/websocket.js | 10 +++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index e7cd1a78..6d6ac52a 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -200,10 +200,6 @@ function requestHandler(req, res) { // 发者模式清除缓存 cleanCache(); } - if (req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') { - // websocket - return; - } res.flush = res.flush || empty; parseGet(req); // 解析get参数 doRoute(req, res); // HTTP路由 diff --git a/bin/proxy/websocket.js b/bin/proxy/websocket.js index 6bea3c5f..1edaaecf 100644 --- a/bin/proxy/websocket.js +++ b/bin/proxy/websocket.js @@ -50,7 +50,7 @@ function wsFiller(ws, req) { ws.logKey = req.headers['sec-websocket-key'] || Math.random(); } -function emitReportLig(ws, type) { +function emitReportLog(ws, type) { ws.upgradeReq.emit(type); ws.messageTriggerCount = 0; } @@ -61,15 +61,15 @@ function reportWebSocketLog(ws, isEnd) { // 每次上报log时,先看下Log多不多,不多的话,延迟上报下 clearTimeout(ws.logReportTimer); if (isEnd) { - emitReportLig('reportLog'); + emitReportLog('reportLog'); } else if (logLength > 30) { // 立即上报 - emitReportLig('reportLogStream'); + emitReportLog('reportLogStream'); } else if (ws.messageTriggerCount > 9) { - emitReportLig('reportLogStream'); + emitReportLog('reportLogStream'); } else { ws.logReportTimer = setTimeout(function() { - emitReportLig('reportLogStream'); + emitReportLog('reportLogStream'); }, 5000); } }