Skip to content

Commit

Permalink
Add initial support for subscriber mode:
Browse files Browse the repository at this point in the history
When we use any of SUBSCRIBE commands, we need to restrict the
commands we can use after. Only other SUBSCRIBE, or UNSUBSCRIBE commands
are valid.

We added a "mode", is_subscriber, that default to 0 and will be true after
we subscribe something. When is_subscriber is active, invalid commands
will result in an exception.

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information
melo committed Aug 5, 2010
1 parent 297a71d commit f8b1b5a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Redis.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ sub new {
Proto => 'tcp',
) || confess("Could not connect to Redis server at $self->{server}: $!");
$self->{rbuf} = '';

$self->{is_subscriber} = 0;

return bless($self, $class);
}
Expand All @@ -73,6 +75,7 @@ sub AUTOLOAD {

my $command = $AUTOLOAD;
$command =~ s/.*://;
$self->__is_valid_command($command);

$self->__send_command($command, @_);

Expand All @@ -94,6 +97,7 @@ sub quit {

sub info {
my ($self) = @_;
$self->__is_valid_command('INFO');

$self->__send_command('INFO');

Expand All @@ -106,6 +110,7 @@ sub info {

sub keys {
my $self = shift;
$self->__is_valid_command('KEYS');

$self->__send_command('KEYS', @_);

Expand All @@ -118,6 +123,16 @@ sub keys {
}


### Mode validation
sub __is_valid_command {
my ($self, $cmd) = @_;

return unless $self->{is_subscriber};
return if $cmd =~ /^P?(UN)?SUBSCRIBE$/;
confess("Cannot use command '$cmd' while in SUBSCRIBE mode, ");
}


### Socket operations
sub __send_command {
my $self = shift;
Expand Down

0 comments on commit f8b1b5a

Please sign in to comment.