Skip to content

add Redis::OPT_READ_TIMEOUT option for issue #70 #260

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

Merged
merged 1 commit into from
Feb 11, 2013

Conversation

kotas
Copy link
Contributor

@kotas kotas commented Oct 4, 2012

This is a patch to fix issue #70 debug read error on connection.

Redis#connect() uses its third argument timeout not only for the connection timeout but also for the read timeout on an opened stream. (by calling php_stream_set_option on the stream)

This behavior causes "read error" on any reading methods (get, hget, blpop, etc.) if you give very short timeout value to connect().

This patch adds Redis::OPT_READ_TIMEOUT for Redis#setOption() so that you can specify another timeout value for php_stream_set_option.

You can test this fix by @micharl-grunder's reproduce code.

Before

try {
        $r = new Redis();
        if(!$r->connect('localhost',6379,5)) {
                echo "Couldn't connect\n";
                die();
        }
        $st = microtime(true);
        $val = $r->blpop('mykey',0);
        $et = microtime(true);
} catch(Exception $ex) {
        echo "ex: " . $ex->getMessage() . "\n";
        $et = microtime(true);
}

echo "Took: " . ($et-$st) . " to get the key\n";

output:

ex: read error on connection
Took: 5.0015189647675 to get the key

After

try {
        $r = new Redis();
        if(!$r->connect('localhost',6379,5)) {
                echo "Couldn't connect\n";
                die();
        }
        $r->setOption(Redis::OPT_READ_TIMEOUT, 10);   // by patch
        $st = microtime(true);
        $val = $r->blpop('mykey',0);
        $et = microtime(true);
} catch(Exception $ex) {
        echo "ex: " . $ex->getMessage() . "\n";
        $et = microtime(true);
}

echo "Took: " . ($et-$st) . " to get the key\n";

output:

ex: read error on connection
Took: 10.001543998718 to get the key

@kotas
Copy link
Contributor Author

kotas commented Oct 4, 2012

Note: You cannot setOption(Redis::OPT_READ_TIMEOUT, 0) to NOT time out. php_stream_set_option doesn't accept 0 for that purpose.

@colinmollenhour
Copy link

Any word on this? It seems like a good feature to have while having no drawbacks..

@@ -915,9 +919,9 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)

php_stream_auto_cleanup(redis_sock->stream);

if(tv.tv_sec != 0) {
if(read_tv.tv_sec != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just seconds? microseconds should be checked.

@twogood
Copy link

twogood commented Jan 16, 2013

+1 on this pull request!

@njam
Copy link

njam commented Feb 10, 2013

+1 :shipit: please
So there is no way of setting no timeout, should we just set a very high number?

@michael-grunder
Copy link
Member

You should be able to set no timeout (overall). You also have to check the value of default_socket_timeout in php.ini. I believe -1 is what you want for unlimited.

However, I think this is a reasonable feature to add (separate read timeout), so I'll get it in later tonight.

@njam
Copy link

njam commented Feb 10, 2013

Ok I see, but my understanding is setting default_socket_timeout to -1 can have unwanted side effects, since this is a global PHP option? Will this also apply to the connection timeout?
I would like to have a reasonable connection timeout of a few seconds, but subscribe should never timeout.
Maybe some hint on the docu for "pub/sub" would be good when this is merged, since it seems many people stumble upon this (#230, #70, #4).

@michael-grunder
Copy link
Member

Yes, I see your issue. I will get this merged tonight for you guys. :)

Cheers!
Mike

@michael-grunder michael-grunder merged commit 3764a6c into phpredis:master Feb 11, 2013
@njam
Copy link

njam commented Feb 12, 2013

Hm I thought I posted this comment before, but can't see it anymore:

Thank you very much for merging this quickly!
Will there be a new tag or should I use master?

@michael-grunder
Copy link
Member

You can go ahead and use master. I think it's a safe enough fix. Just let me know if there are any issues and I can fix them.

@njam
Copy link

njam commented Apr 25, 2013

I hate to nag, but is there any chance you could create a tag of the current version?
(I don't like to use master in production since I want to stick to a specific version for better repeatability.)

@michael-grunder
Copy link
Member

Sure, seems fine to me.

@nicolasff Any issue with me tagging the current master 2.2.3? I'm planning on tackling some of the session issues this week, so it could be a decent time. There have been many things incorporated since 2.2.2

@nicolasff
Copy link
Member

I think it's a good idea, it's been a while a lots of features have made it to trunk since the last release.

@michael-grunder
Copy link
Member

Pushed and tagged! :)

Please let us know if anything was broken. There are quite a few features added in this release.

Cheers!
Mike

@njam
Copy link

njam commented Apr 29, 2013

Thanks 😘, will report any problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants