Permalink
Browse files

Allow new() name param to be used safely with redis-server < 2.6.9

If client_setname is not supported, the name attr is ignored.
  • Loading branch information...
1 parent 379b6f2 commit faf184fa766732a874a434a1cc7147f92fe04770 @melo melo committed Jan 23, 2013
Showing with 15 additions and 9 deletions.
  1. +10 −8 lib/Redis.pm
  2. +5 −1 t/01-basic.t
View
18 lib/Redis.pm
@@ -54,7 +54,8 @@ sub new {
if (my $name = $args{name}) {
my $on_conn = $self->{on_connect};
$self->{on_connect} = sub {
- $_[0]->client_setname($name);
+ my ($redis) = @_;
+ try { $redis->client_setname($name) };
$on_conn->(@_) if $on_conn;
}
}
@@ -931,13 +932,14 @@ sucessfull connection. The C<< on_connect >> attribute is used to provide the
code reference, and it will be called with the first parameter being the Redis
object.
-Starting with Redis 2.6.9, you can set a name for each connection. This can be
-very useful for debugging purposes, using the C<< CLIENT LIST >> command. To
-set a connection name, use the C<< name >> parameter. Please note that there
-are restrictions on the name you can set, the most important of which is, no
-spaces. See the L<CLIENT SETNAME
-documentation|http://redis.io/commands/client-setname> for all the juicy
-details.
+You can also set a name for each connection. This can be very useful for
+debugging purposes, using the C<< CLIENT LIST >> command. To set a connection
+name, use the C<< name >> parameter. Please note that there are restrictions on
+the name you can set, the most important of which is, no spaces. See the
+L<CLIENT SETNAME documentation|http://redis.io/commands/client-setname> for all
+the juicy details. This feature is safe to use with all versions of Redis
+servers. If C<< CLIENT SETNAME >> support is not available (Redis servers 2.6.9
+and above only), the name parameter is ignored.
The C<< debug >> parameter enables debug information to STDERR, including all
interactions with the server. You can also enable debug with the C<REDIS_DEBUG>
View
6 t/01-basic.t
@@ -12,7 +12,11 @@ my ($c, $srv) = redis();
END { $c->() if $c }
-ok(my $o = Redis->new(server => $srv), 'connected to our test redis-server');
+my $o;
+is(
+ exception { $o = Redis->new(server => $srv, name => 'my_name_is_glorious') },
+ undef, 'connected to our test redis-server',
+);
ok($o->ping, 'ping');

0 comments on commit faf184f

Please sign in to comment.