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

websocket task进程的内存持续上涨,何故? #1238

Closed km3945 closed 7 years ago

km3945 commented 7 years ago

PHP=7.1.3 swoole=1.9.11 逻辑很简单,就是收到消息后广告到客户端. 这里用了swoole table来存客户端fd,我实际出现问题的时候只有一个客户端连接进来.也就是只有一条记录

一晚上时间.task进程从启动时占7708到早上的79m 还在向上涨. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
85780 root 20 0 463m 79m 1092 S 0.0 0.5 2:22.61 php_mrWS:task:2
85781 root 20 0 463m 79m 1084 S 0.0 0.5 2:23.57 php_mrWS:task:3
85782 root 20 0 463m 79m 1084 S 0.0 0.5 2:23.24 php_mrWS:task:4
85783 root 20 0 463m 79m 1084 S 0.0 0.5 2:23.50 php_mrWS:task:5
85776 root 20 0 549m 43m 23m S 0.0 0.3 13:03.22 php_mrWS:master
85785 root 20 0 395m 12m 3684 S 0.0 0.1 4:23.98 php_mrWS:work:1
85784 root 20 0 395m 12m 3672 S 0.0 0.1 5:25.81 php_mrWS:work:0
85777 root 20 0 401m 7360 740 S 0.0 0.0 0:00.00 php_mrWS:manager

以下是个主要代码.


    public function onMessage($server, $frame){
        $fd=$frame->fd;
        if(!($data=jsonDecode($frame->data, true))){
            return;
        }

        $_cmd=trim($data['cmd']);
        $_data=$data['data'];

        if($_cmd==='self_reported'){
            if($_data!=='server'){
                $this->tableClient->set('cfd:'.$fd,['fd'=>$fd]);
            }
        }elseif($_cmd==='to_desktop'){
            $server->task($_data);
        }
    }
    public function onTask($serv, $task_id, $src_worker_id, $data){
        $msg=$this->getMsg('msg', $data);
        foreach ($this->tableClient as $key => $value){
            $serv->push($value['fd'], $msg);
        }
        $serv->finish('done');
    }
    public function onFinish($serv, $task_id, $data){
        //dev('send done: '.$task_id);
    }

    public function onOpen($server, $req){
        $fd=$req->fd;
        dev('connection open: '.$fd);
    }
    public function onClose($server, $fd){
        dev('connection close: '.$fd);
        $this->tableClient->del('cfd:'.$fd);
    }
    public function createSwooleTable(){
        $this->tableClient = new swoole_table(1024);
        $this->tableClient->column('fd', swoole_table::TYPE_INT, 4);
        $this->tableClient->create();
    }
    public function getMsg($cmd,$data){
        $s=[
            'cmd'=>$cmd,
            'data'=>$data
        ];
        return jsonEncode($s);
    }
matyhtf commented 7 years ago

已修复。