批量向gateway发送并获取数据,并发时接收信息会不会串

lizuowang

问题描述

这个向网关发送请求并接收应答,$client是从静态属性中取出,存在多进程并用的情况,
那在进程并发执行 向网关发送消息时,能发确保收到的应答是本进程发送的请求应答呢?

 /**
     * 批量向gateway发送并获取数据
     * @param $gateway_buffer_array
     * @return array
     */
    protected static function getBufferFromGateway($gateway_buffer_array)
    {
        $client_array = $status_data = $client_address_map = $receive_buffer_array = $recv_length_array = array();
        // 批量向所有gateway进程发送请求数据
        foreach ($gateway_buffer_array as $address => $gateway_buffer) {
            $client = static::getGatewayConnection("tcp://$address");
            if (strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer)) {
                $socket_id                        = (int)$client;
                $client_array[$socket_id]         = $client;
                $client_address_map[$socket_id]   = explode(':', $address);
                $receive_buffer_array[$socket_id] = '';
            }
        }
        // 超时5秒
        $timeout    = 5;
        $time_start = microtime(true);
        // 批量接收请求
        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) {
                static::$gatewayConnections = [];
                break;
            }
        }
        $format_buffer_array = array();
        foreach ($receive_buffer_array as $socket_id => $buffer) {
            $local_ip                                    = ip2long($client_address_map[$socket_id][0]);
            $local_port                                  = $client_address_map[$socket_id][1];
            $format_buffer_array[$local_ip][$local_port] = unserialize(substr($buffer, 4));
        }
        return $format_buffer_array;
    }
652 1 0
1个回答

walkor 打赏

进程间是隔离的,进程间的变量也是隔离的。每个进程是单线程的,并且代码是顺序执行的,没有并发问题,不会串

  • lizuowang 2023-04-07

    多进程之间静态属性不是共享的嘛,不会有多个进程获取到同一个client的情况吗

  • walkor 2023-04-07

    多进程间变量是隔离的

  • lizuowang 2023-04-07

    哦哦哦 了解了,那同进程里的多个连接 同时拿到同一个client 这样的情况 是什么保证网关响应不串的呢?

  • walkor 2023-04-07

    同一个进程内代码是顺序执行的,不会并发执行

  • lizuowang 2023-04-07

    懂了 谢谢大佬解惑

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