Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(websocket): optimize report log #200

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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