shogo82148 / Redis-Fast

fast perl binding for Redis database
https://metacpan.org/release/Redis-Fast
Other
25 stars 21 forks source link

Reconnect behaviour differs from cpan Redis module #73

Closed vsespb closed 6 years ago

vsespb commented 6 years ago

so, script:

use strict;
use warnings;
use Redis::Fast;
my $redis = Redis::Fast->new(
    server => 'localhost:6379',
    reconnect=>20,
    every => 100,
    cnx_timeout   => 10,
    read_timeout  => 3,
    write_timeout => 3,
);

while() {
    eval {
        $redis->get(42);
        print "works\n";
    } or do {
        print "err: $@"
    };
    sleep 1;
}

will print the following:

$ perl redispoc.pl 
works
works
works
works
works
err: Could not connect to Redis server at localhost:6379 at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.

if I stop redis server (after that first err: Could not connect to Redis server line emited), and then start it in a few minutes (after all reconnect attemts exhaused) $redis connect does not work even with live Redis server, it still emits "err: Not connected to any server".

In contrast behaviour with Redis module:

$ perl redispoc.pl 
works
works
works
works
works
works
works
works
works
err: Could not connect to Redis server at localhost:6379: Connection refused at /usr/local/share/perl/5.24.1/Redis.pm line 268.
    ...propagated at /usr/local/share/perl/5.24.1/Redis.pm line 606.
err: Could not connect to Redis server at localhost:6379: Connection refused at /usr/local/share/perl/5.24.1/Redis.pm line 268.
    ...propagated at /usr/local/share/perl/5.24.1/Redis.pm line 606.
works
works
works
works
works

i.e. the handle still can be used after redis server up.

Not sure which behaviour is more correct, but the fact is they differs.

Also our use case when we prefer Redis module behaviour: we have long running daemons and if redis server down for several minutes we have to restart all daemons and cannot use reconnect feature, or we have to write special code in every eval after which we would want to connect again.

shogo82148 commented 6 years ago

I think that because the every parameter is too short. the unit of every is micro second, not milli second.

vsespb commented 6 years ago

i've tried with 100_000. same:

$ perl 1.pl 
works
works
works
works
works
works
works
works
works
works
works
works
works
err: Could not connect to Redis server at localhost:6379 at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
err: Not connected to any server at /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/Redis/Fast.pm line 265.
^C
$ cat 1.pl 
use strict;
use warnings;
use Redis::Fast;
my $redis = Redis::Fast->new(
    server => 'localhost:6379',
    reconnect=>20,
    every => 100_000,
    cnx_timeout   => 10,
    read_timeout  => 3,
    write_timeout => 3,
);

while() {
    eval {
        $redis->get(42);
        print "works\n";
    } or do {
        print "err: $@"
    };
    sleep 1;
}
shogo82148 commented 6 years ago

I fixed this issue, and released Redis::Fast v0.21. Please try it.