cheprasov / php-redis-client

RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 6.0
MIT License
127 stars 41 forks source link

Timeout issue #62

Closed hpolonkoev closed 6 years ago

hpolonkoev commented 6 years ago

Hi, I am trying to understand what the timeout is for.

$Redis = ClientFactory::create([
    'server' => 'redis:6379', // external redis server 
    'timeout' => 2,
]);

In case the Redis server is unavailable Redis instance doesn't really throw an error or stops after 2 seconds (with the error of timeout). How can I limite the timeout if the redis server is unavailable ?

edepietromaria commented 6 years ago

hello, you can add a timeout in seconds on stream_socket_client function

stream_socket_client($this->server, $errno, $errstr, 2)

or

stream_socket_client($this->server, $errno, $errstr, ($this->timeout / 1000000))

on RedisClient/Connection/StreamConnection.php on line 95

cheprasov commented 6 years ago

Hey @hpolonkoev

Param timeout sets response timeout of server (Redis). The timeout for reading/writing data over the socket,

Timeout means that a server is taking too long to reply to a data request made from a client. Timeouts are not a reply message: they show up when there isn't a reply and a server request is not fulfilled in a predetermined length of time. The purpose of a server timeout is to prevent a client from endlessly waiting for a sever to respond.

Also, please see monitor example, it uses the timeout configuration: https://github.com/cheprasov/php-redis-client/blob/master/examples/monitor.php

cheprasov commented 6 years ago

@hpolonkoev Also, I will release new version on RedisClient soon, and I will add timeout param for connection, not only for reading/writing data over the socket like it is now. Thanks.

hpolonkoev commented 6 years ago

@edepietromaria Thanks! This worked for me ;)

@cheprasov Thanks for your explanation;) Waiting for you new release. Until then I'll use the @edepietromaria 's "patch".

cheprasov commented 6 years ago

@hpolonkoev Done. Version 1.8.0 of RedisClient is released.

I added connection param to config.

$config = [
...
    // Optional. Default = null
    // See more here: http://php.net/manual/en/function.stream-socket-client.php
    'connection' => [
        // Optional. Default = ini_get("default_socket_timeout")
        // The timeout only applies while making connecting the socket
        'timeout' => 2,

        // Optional. Default = STREAM_CLIENT_CONNECT
        // Bitmask field which may be set to any combination of connection flags.
        // Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default),
        // STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.
        'flags' => STREAM_CLIENT_CONNECT
    ],
...
];
hpolonkoev commented 6 years ago

@cheprasov Thanks a lot! Really apreciate your reactivity ;) And of corse thanks for RedisClient ;)