Skip to content

Commit

Permalink
Merge pull request #200 from tarotlwei/dev
Browse files Browse the repository at this point in the history
feat(websocket): optimize report log
  • Loading branch information
huangyoukun authored Jul 25, 2018
2 parents acd385c + 0f08a19 commit b91e064
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 0 additions & 4 deletions bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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路由
Expand Down
15 changes: 12 additions & 3 deletions bin/proxy/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -49,19 +50,26 @@ function wsFiller(ws, req) {
ws.logKey = req.headers['sec-websocket-key'] || Math.random();
}

function emitReportLog(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');
emitReportLog('reportLog');
} else if (logLength > 30) {
// 立即上报
ws.upgradeReq.emit('reportLogStream');
emitReportLog('reportLogStream');
} else if (ws.messageTriggerCount > 9) {
emitReportLog('reportLogStream');
} else {
ws.logReportTimer = setTimeout(function() {
ws.upgradeReq.emit('reportLogStream');
emitReportLog('reportLogStream');
}, 5000);
}
}
Expand Down Expand Up @@ -186,6 +194,7 @@ function bind_listen(server) {
};

ws.on('message', function(message) {
ws.messageTriggerCount++;
logger.debug('server get message : ${message}', {
message
});
Expand Down

0 comments on commit b91e064

Please sign in to comment.