swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.48k stars 3.16k forks source link

监听0.0.0.0时onClose重复回调 #1162

Closed 52fhy closed 7 years ago

52fhy commented 7 years ago

监听本机公网IP时正常,监听0.0.0.0时不断触发onClose回调:

代码:

public function __construct() {
        $serv = new \swoole_websocket_server('0.0.0.0', '9501');

        $serv->set(array(
            'worker_num' => 8,
            'daemonize' => true,
            'max_request' => 1000,
            'max_conn' => 65000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1,
            'task_worker_num'=>100,
            'log_file'    => "webServer.log",
            'heartbeat_check_interval'=>60,
            'heartbeat_idle_time' => 600, 
        ));
        $serv->on('Start', array($this, 'onStart'));
        $serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $serv->on('Open', array($this, 'onOpen'));
        $serv->on('Message', array($this, 'onMessage'));
        $serv->on('Task',array($this,'onTask'));
        $serv->on('Finish',array($this,'onFinish'));
        $serv->on('Close', array($this, 'onClose'));
        $serv->start();
    }

public function onOpen(swoole_websocket_server $server, $request){
        $fd = $request->fd;
        MyEcho("new Client onOpen ----->fd: {$fd}");
    }

public function onClose( $serv, $fd, $from_id ) {
        MyEcho("client Close----->fd: {$fd}");
    }

日志:

[2017-03-29 14:09:03] INFO: client Close----->fd: 1803
[2017-03-29 14:09:13] INFO: client Close----->fd: 1804
[2017-03-29 14:09:14] INFO: client Close----->fd: 1805
[2017-03-29 14:09:17] INFO: client Close----->fd: 1787
[2017-03-29 14:09:21] INFO: client Close----->fd: 1807
[2017-03-29 14:09:32] INFO: client Close----->fd: 1808
[2017-03-29 14:09:37] INFO: client Close----->fd: 1810
[2017-03-29 14:09:42] INFO: client Close----->fd: 1809
[2017-03-29 14:11:05] INFO: client Close----->fd: 1812
[2017-03-29 14:11:46] INFO: client Close----->fd: 1814
[2017-03-29 14:11:57] INFO: client Close----->fd: 1815
[2017-03-29 14:12:00] INFO: client Close----->fd: 1813
[2017-03-29 14:12:09] INFO: client Close----->fd: 1816
[2017-03-29 14:12:49] INFO: client Close----->fd: 1811
[2017-03-29 14:12:50] INFO: client Close----->fd: 1817
[2017-03-29 14:13:04] INFO: client Close----->fd: 1820
[2017-03-29 14:13:12] INFO: client Close----->fd: 1781
[2017-03-29 14:13:18] INFO: client Close----->fd: 1818
[2017-03-29 14:13:26] INFO: client Close----->fd: 1819
[2017-03-29 14:13:54] INFO: client Close----->fd: 1806
[2017-03-29 14:13:57] INFO: client Close----->fd: 1823
[2017-03-29 14:14:14] INFO: client Close----->fd: 1824
[2017-03-29 14:14:16] INFO: client Close----->fd: 1825
[2017-03-29 14:14:22] INFO: client Close----->fd: 1826
[2017-03-29 14:14:25] INFO: client Close----->fd: 1827
[2017-03-29 14:14:36] INFO: client Close----->fd: 1828
[2017-03-29 14:14:41] INFO: client Close----->fd: 1829
[2017-03-29 14:14:43] INFO: client Close----->fd: 1821
[2017-03-29 14:15:00] INFO: client Close----->fd: 1831
[2017-03-29 14:15:00] INFO: client Close----->fd: 1832
[2017-03-29 14:15:22] INFO: client Close----->fd: 1833
[2017-03-29 14:15:28] INFO: client Close----->fd: 1834
[2017-03-29 14:15:31] INFO: client Close----->fd: 1835
[2017-03-29 14:15:53] INFO: client Close----->fd: 1789
[2017-03-29 14:16:05] INFO: client Close----->fd: 1836

版本: PHP 7.0.3+swoole1.8.6

matyhtf commented 7 years ago

这里关闭的fd并不相同,怎么证明是重复回调?

52fhy commented 7 years ago

@matyhtf 没有调用onOpen

matyhtf commented 7 years ago

升级一下swoole的版本试试吧。

52fhy commented 7 years ago

@matyhtf 虽然没有调用onOpen,但是发现调用了Connect:


$serv = new \swoole_websocket_server('0.0.0.0', '9501');

$serv->set(array(
            'worker_num' => 8,
            'daemonize' => true,
            'max_request' => 1000,
            'max_conn' => 65000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1,
            'task_worker_num'=>100,
            'log_file'    => "webServer.log",
            'buffer_output_size' => 32 * 1024 *1024,
            'heartbeat_check_interval'=>60,
            'heartbeat_idle_time' => 600, 
        ));
        $serv->on('Start', array($this, 'onStart'));
        $serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $serv->on('Open', array($this, 'onOpen'));
        $serv->on('Message', array($this, 'onMessage'));
        $serv->on('Task',array($this,'onTask'));
        $serv->on('Finish',array($this,'onFinish'));
        $serv->on('Close', array($this, 'onClose'));
        $serv->on('Connect', function ($server, int $fd, int $from_id){
            My_Echo("client Connect----->fd: {$fd}" .'---'.$from_id);
        });
        $serv->start();

目前还不清楚是怎么调用的。

[2017-04-07 17:08:42] INFO: Start
[2017-04-07 17:08:43] INFO: client Connect----->fd: 1---0
[2017-04-07 17:08:43] INFO: client Close----->fd: 1
[2017-04-07 17:08:44] INFO: client Connect----->fd: 2---0
[2017-04-07 17:08:44] INFO: client Close----->fd: 2
[2017-04-07 17:08:44] INFO: client Connect----->fd: 3---0
[2017-04-07 17:08:44] INFO: client Close----->fd: 3
[2017-04-07 17:08:45] INFO: client Connect----->fd: 4---0
[2017-04-07 17:08:45] INFO: client Close----->fd: 4
[2017-04-07 17:08:45] INFO: client Connect----->fd: 5---0
[2017-04-07 17:08:45] INFO: client Close----->fd: 5
[2017-04-07 17:08:46] INFO: client Connect----->fd: 6---0
[2017-04-07 17:08:46] INFO: client Close----->fd: 6
[2017-04-07 17:08:46] INFO: client Connect----->fd: 7---0
[2017-04-07 17:08:46] INFO: client Close----->fd: 7
[2017-04-07 17:08:46] INFO: client Connect----->fd: 8---0
[2017-04-07 17:08:46] INFO: client Close----->fd: 8
[2017-04-07 17:08:47] INFO: client Connect----->fd: 9---0
[2017-04-07 17:08:47] INFO: client Close----->fd: 9
[2017-04-07 17:08:47] INFO: client Connect----->fd: 10---0
[2017-04-07 17:08:47] INFO: client Close----->fd: 10
[2017-04-07 17:08:49] INFO: client Connect----->fd: 11---0
[2017-04-07 17:08:49] INFO: client Close----->fd: 11
[2017-04-07 17:08:49] INFO: client Connect----->fd: 12---0
52fhy commented 7 years ago

原因已找到,是有客户端一直在进行tcp请求,这个客户端是我们配置的阿里云负载均衡器的健康检查,每隔2s运行一次。感谢您的回复!