Skip to content

Commit b91e064

Browse files
authored
Merge pull request Tencent#200 from tarotlwei/dev
feat(websocket): optimize report log
2 parents acd385c + 0f08a19 commit b91e064

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

bin/proxy/http.proxy.js

-4
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ function requestHandler(req, res) {
200200
// 发者模式清除缓存
201201
cleanCache();
202202
}
203-
if (req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') {
204-
// websocket
205-
return;
206-
}
207203
res.flush = res.flush || empty;
208204
parseGet(req); // 解析get参数
209205
doRoute(req, res); // HTTP路由

bin/proxy/websocket.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function wsFiller(ws, req) {
3434
ws.logReportTimer = null;
3535
ws.__tempSend = ws.send;
3636
ws.reportIndex = 1;
37+
ws.messageTriggerCount = 0;
3738

3839
ws.send = function(message) {
3940
if (ws.readyState == WebSocket.OPEN) {
@@ -49,19 +50,26 @@ function wsFiller(ws, req) {
4950
ws.logKey = req.headers['sec-websocket-key'] || Math.random();
5051
}
5152

53+
function emitReportLog(ws, type) {
54+
ws.upgradeReq.emit(type);
55+
ws.messageTriggerCount = 0;
56+
}
57+
5258
function reportWebSocketLog(ws, isEnd) {
5359
// 这里触发log上报
5460
const logLength = logger.getTextLength();
5561
// 每次上报log时,先看下Log多不多,不多的话,延迟上报下
5662
clearTimeout(ws.logReportTimer);
5763
if (isEnd) {
58-
ws.upgradeReq.emit('reportLog');
64+
emitReportLog('reportLog');
5965
} else if (logLength > 30) {
6066
// 立即上报
61-
ws.upgradeReq.emit('reportLogStream');
67+
emitReportLog('reportLogStream');
68+
} else if (ws.messageTriggerCount > 9) {
69+
emitReportLog('reportLogStream');
6270
} else {
6371
ws.logReportTimer = setTimeout(function() {
64-
ws.upgradeReq.emit('reportLogStream');
72+
emitReportLog('reportLogStream');
6573
}, 5000);
6674
}
6775
}
@@ -186,6 +194,7 @@ function bind_listen(server) {
186194
};
187195

188196
ws.on('message', function(message) {
197+
ws.messageTriggerCount++;
189198
logger.debug('server get message : ${message}', {
190199
message
191200
});

0 commit comments

Comments
 (0)