GatewayClient运行10几分钟后出错

dignfei

一开始正常,运行10分钟后,就会出错,Gateway::getAllGroupIdList()返回的数据会缺失一部分。
环境:php8.0.1到8.0.7都会出现这个问题。
提取了可重现代码如下:

<?php

use GatewayClient\Gateway;
use Swoole\Process;

require __DIR__.'/../vendor/autoload.php';

$Test = new test();
$Test->SwooleRundebug2();

class test{
    public function SwooleRundebug2(){
        // 监听一个http端口,端口只暴露给nginx
        global $Server;
        $Server = new Swoole\Http\Server('0.0.0.0', 16985); // SWOOLE_BASE 不支持reload
        $Server->set(['enable_coroutine' => false,'worker_num'      => 1,]);
        $Server->on('WorkerStart', function ($server, $worker_id){
            Swoole\Timer::tick(3000, function () {
                $this->subProcess();
            });
        });
        $Server->on('request', function ($request, $response){
        });
        $Server->start();
    }
    public function subProcess()
    {
        Swoole\Process::wait(false);
        // 开启子线程
        $process = new Process(function (Process $proc)  {
            echo '开始运行一次',  date('Y-m-d H:i:s');
            // 运行这个 获取rooms
            Gateway::$registerAddress = '127.0.0.1:9138';
            $rooms = Gateway::getAllGroupIdList();
            var_dump($rooms);
            if(empty($rooms)){
                Gateway::$registerAddress = '127.0.0.1:9138';// 这里打断点,方便调试
                $rooms = Gateway::getAllGroupIdLsist();
            }
            $proc->exit();
        }, false, 1, true);
        $process->start();
    }
}

经过xdebug调试发现,在 vendor\workerman\gatewayclient\Gateway.php的方法

protected static function getBufferFromGateway($gateway_buffer_array)

中的这一段代码有问题

while (count($client_array) > 0) {
            $write = $except = array();
            $read  = $client_array;
            if (@stream_select($read, $write, $except, $timeout)) {
                foreach ($read as $client) {
                    $socket_id = (int)$client;
                    $buffer    = stream_socket_recvfrom($client, 65535);
                    if ($buffer !== '' && $buffer !== false) {
                        $receive_buffer_array[$socket_id] .= $buffer;
                        $receive_length = strlen($receive_buffer_array[$socket_id]);
                        if (empty($recv_length_array[$socket_id]) && $receive_length >= 4) {
                            $recv_length_array[$socket_id] = current(unpack('N', $receive_buffer_array[$socket_id]));
                        }
                        if (!empty($recv_length_array[$socket_id]) && $receive_length >= $recv_length_array[$socket_id] + 4) {
                            unset($client_array[$socket_id]);
                        }
                    } elseif (feof($client)) {
                        unset($client_array[$socket_id]);
                    }
                }
            }
            if (microtime(true) - $time_start > $timeout) {
                break;
            }
        }

这里会访问4个端口获取数据,但是只有一个端口能得到数据,其他的端口获取不到数据。求大佬解决 @walkor

1595 2 0
2个回答

walkor

有问题的时候运行status截图下,截图截全。

  • dignfei 2021-07-29

    运行status是什么意思?执行什么代码?

  • yongdao35 2021-07-30

    php start.php status 吧

  • hachiko6177 2021-09-23

    常驻内存下 Gateway client 出问题了 ,会拿不到数据,不知道和 socket 没有 close 有没有关系

yongdao35

Gateway::getAllGroupIdList() 是返回在线的分组id,10分钟后只返回部分,我猜测是因为有的分组id没有连接在线了。

  • dignfei 2021-07-30

    都连接在线,新打开一个shell运行php,就都能读取到

年代过于久远,无法发表回答
🔝