Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection timed out #385

Closed
TehesFR opened this issue Oct 10, 2016 · 5 comments
Closed

Connection timed out #385

TehesFR opened this issue Oct 10, 2016 · 5 comments

Comments

@TehesFR
Copy link

TehesFR commented Oct 10, 2016

Hi guys,

I have a strange behaviour using predis with a php/symfony application.
Everytime the application tries to connect to redis, I got this in the logs:

stream_socket_client(): unable to connect to tcp://redis_ip_server:6379 (Connection timed out)

This is not a firewall or remote connection issue because, if I use telnet on the exact same "redis_ip_server" and port 6379, the connection is OK and I'm able to set and get keys... so basically the connection is timed out only when using predis.

After a couple of hours, I've finally fixed this issue by changing one line in predis/src/Connection/StreamConnection.php on line 127:

if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags)) {
        $this->onConnectionError(trim($errstr), $errno);
}

BY

if (!$resource = @stream_socket_client($address, $errno, $errstr, $flags)) {
        $this->onConnectionError(trim($errstr), $errno);
}

I don't really understand why I have to do this...
Is it an issue on your side or mine ? I hope maybe you can help me understand.
For information, I'm using the official redis docker container as my redis server (v3.2.4).

Thanks a lot.

@nrk
Copy link
Contributor

nrk commented Oct 12, 2016

First thing, your change is actually wrong as you are passing the value of $flags as a timeout for stream_socket_client(). That said, considered that $flags is set to 4 (if you are not using persistent connections, the only connection flag is STREAM_CLIENT_CONNECT) which is smaller than the default value of $timeout used by Predis for connections (5 seconds) this behaviour doesn't make sense to me. Have you tried increasing the timeout for connection attempts, let's say to 30 seconds, just to see if anything changes? You can do it by setting timeout=30 in the connection parameters passed to Predis.

@TehesFR
Copy link
Author

TehesFR commented Oct 14, 2016

@nrk thanks for your answer, I've made some tests:

It's working fine with:

if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout=30, $flags)) {
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout=5, $flags)) {
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout=1, $flags)) {

And I have the error with:

if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout=0, $flags)) {
if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags)) {

@nrk
Copy link
Contributor

nrk commented Nov 22, 2016

Now I still don't get how it is even possible that you get an error when you don't assign a custom value to timeout via connection parameters considering this line where $timeout it is set to 5 seconds when nothing is specified by the user...

@itbdw
Copy link

itbdw commented Jul 1, 2017

@TehesFR

I guess you set a error config, say set a empty timeout, and as a result $timeout become to 0;

You may debug the timeout value passed to the function. What do you think, @nrk

    protected function createStreamSocket(ParametersInterface $parameters, $address, $flags)
    {
        $timeout = (isset($parameters->timeout) ? (float) $parameters->timeout : 5.0);

       //debug here
       var_dump($timeout);

        if (!$resource = @stream_socket_client($address, $errno, $errstr, $timeout, $flags)) {

            $this->onConnectionError(trim($errstr), $errno);
        }

@nrk
Copy link
Contributor

nrk commented Aug 22, 2020

I'm closing this due to lack of feedback, feel free to re-open it if you have more details to share and are still interested in it.

@nrk nrk closed this as completed Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants