go-judge icon indicating copy to clipboard operation
go-judge copied to clipboard

websocket 在 close 的时候会在日志中产生一行 warning

Open yzy-1 opened this issue 2 years ago • 1 comments

如题。连接 ws://localhost:5050/ws 并发送一条执行请求,可以正常收到结果。但是当请求结束,客户端退出时,无论是显式调用 connection.close() 关闭连接还是直接 exit 退出程序,均会在服务端(executorserver)产生格式如同下面的 warning:

// 直接退出
{"level":"warn","ts":1663066810.5931816,"caller":"ws_executor/websocket.go:146","msg":"ws read error:websocket: close 1006 (abnormal closure): unexpected EOF"}
// 显式调用 close
{"level":"warn","ts":1663066699.9190106,"caller":"ws_executor/websocket.go:146","msg":"ws read error:websocket: close 1000 (normal): Normal connection closure"}

测试代码:

#!/usr/bin/env node
var WebSocketClient = require('websocket').client;

var client = new WebSocketClient();

client.on('connectFailed', function(error) {
  console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
  console.log('WebSocket Client Connected');
  connection.on('error', function(error) {
    console.log("Connection Error: " + error.toString());
  });
  connection.on('close', function() {
    console.log('echo-protocol Connection Closed');
  });
  connection.on('message', function(message) {
    if (message.type === 'utf8') {
      console.log("Received: '" + message.utf8Data + "'");
      connection.close();
    }
  });

  function test() {
    if (connection.connected) {
      connection.sendUTF(
String.raw`{
  "requestId":"hello",
    "cmd": [{
        "args": ["/usr/bin/g++", "a.cc", "-o", "a"],
        "env": ["PATH=/usr/bin:/bin"],
        "files": [{
            "content": ""
        }, {
            "name": "stdout",
            "max": 10240
        }, {
            "name": "stderr",
            "max": 10240
        }],
        "cpuLimit": 10000000000,
        "memoryLimit": 104857600,
        "procLimit": 50,
        "copyIn": {
            "a.cc": {
                "content": "#include <iostream>\nusing namespace std;\nint main() {\nint a, b;\ncin >> a >> b;\ncout << a + b << endl;\n}"
            }
        },
        "copyOut": ["stdout", "stderr"],
        "copyOutCached": ["a.cc", "a"],
        "copyOutDir": "1"
    }]
}`
      );
    }
  }
  test();
});

client.connect('ws://localhost:5050/ws');

yzy-1 avatar Sep 13 '22 11:09 yzy-1

在 https://github.com/criyle/go-judge/blob/master/cmd/executorserver/ws_executor/websocket.go#L145 145,150 和 166 都会在错误产生的时候输出 WARN 级别日志,或许可以改成 INFO 级别的

criyle avatar Sep 14 '22 01:09 criyle