0

文档如下:

超时控制
协程客户端提供了更细粒度的超时控制。可以设置:
timeout:总超时,包括连接、发送、接收所有超时
connect_timeout:连接超时
read_timeout:接收超时
write_timeout:发送超时

use Swoole;
$client = new CoroutineClient(SWOOLE_SOCK_TCP);
$client->set([

'timeout' => 0.5,
'connect_timeout' => 1.0,
'write_timeout' => 10.0,
'read_timeout' => 0.5,

]);

请问timeout和下面三个xxx_timeout是什么关系呢???

GGGG430 26
2019-09-19 提问
1 个回答
0

已采纳

从源码里可以看到

swoole_client_coro.cc

bool php_swoole_client_set(Socket *cli, zval *zset)
{
    HashTable *vht = Z_ARRVAL_P(zset);
    zval *ztmp;
    bool ret = true;

    /**
     * timeout
     */
    if (php_swoole_array_get_value(vht, "timeout", ztmp))
    {
        cli->set_timeout(zval_get_double(ztmp));
    }
    if (php_swoole_array_get_value(vht, "connect_timeout", ztmp))
    {
        cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_CONNECT);
    }
    if (php_swoole_array_get_value(vht, "read_timeout", ztmp))
    {
        cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_READ);
    }
    if (php_swoole_array_get_value(vht, "write_timeout", ztmp))
    {
        cli->set_timeout(zval_get_double(ztmp), SW_TIMEOUT_WRITE);
    }
    // 省略……
}

include\coroutine_socket.h

    /* set connect read write timeout */
    inline void 
    set_timeout(double timeout, int type = SW_TIMEOUT_ALL)
    {
        if (timeout == 0)
        {
            return;
        }
        if (type & SW_TIMEOUT_CONNECT)
        {
            connect_timeout = timeout;
        }
        if (type & SW_TIMEOUT_READ)
        {
            read_timeout = timeout;
        }
        if (type & SW_TIMEOUT_WRITE)
        {
            write_timeout = timeout;
        }
    }

设置timeout会把connect_timeoutread_timeoutwrite_timeout全部设为一样的值。然后也可以单独设置某个操作的超时时间。

撰写答案

推广链接