| 1 | package Redis::Handle; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Redis; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use AnyEvent::Redis; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.2.0'; # VERSION | |||||||||||||||||||||||||||
| 9 | # ABSTRACT: Tie::Handle interface for Redis queues | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # use Data::Dump qw(pp); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Redis::Handle - A filehandle tie for a Redis queue | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | tie *REDIS, 'Redis::Handle'; | ||||||||||||||||||||||||||||
| 20 | print REDIS "Foo bar baz\n"; | ||||||||||||||||||||||||||||
| 21 | print while <REDIS>; # Prints "Foo bar baz\n" | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | print REDIS "Foo", "Bar"; | ||||||||||||||||||||||||||||
| 24 | my @baz = <REDIS>; # @baz is now ("Foo","Bar") | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | print REDIS "Foo", "Bar"; | ||||||||||||||||||||||||||||
| 27 | print <REDIS>; # Prints "Foo" | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | C<Redis::Handle> implements a tie interface to a Redis queue so that you can | ||||||||||||||||||||||||||||
| 32 | treat said queue as a filehandle. Pushing to the front of the queue is the same | ||||||||||||||||||||||||||||
| 33 | as a C<print>, and popping off the end of the queue is like a C<readline>. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 2 | my $timeout = 30; # For BLPOPs, in seconds | |||||||||||||||||||||||||||
| 39 | 2 | my $redis; # We want only a single Redis connection | |||||||||||||||||||||||||||
| 40 | 2 | my %redis; # Connection information | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 TIEHANDLE | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Ties the filehandle to the clientId in Redis. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head3 Usage | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | tie *CLIENT, "Redis::Handle", $clientId; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | tie *CLIENT, 'Redis::Handle', $clientId, | ||||||||||||||||||||||||||||
| 53 | timeout => 100, | ||||||||||||||||||||||||||||
| 54 | host => 'example.com', | ||||||||||||||||||||||||||||
| 55 | port => 5800; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # pass an existing Redis connection | ||||||||||||||||||||||||||||
| 58 | tie *CLIENT, 'Redis::Handle', $clientId, $redis; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub TIEHANDLE { | ||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($class,$clientId) = (+shift,+shift); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $redis = shift if ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | %redis = @_; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | $redis ||= Redis->new(%redis); | ||||||||||||||||||||||||||
|
Condition0%0/3
$redis ||= "Redis"->new(%redis)
MC/DC0%0/2
$redis ||= "Redis"->new(%redis)
$redis
"Redis"->new(%redis)
Truth table0%0/3
A ||= B
A= $redisB= "Redis"->new(%redis)
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($redis{timeout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $timeout = $redis{timeout}; | |||||||||||||||||||||||||||
| 70 | 0 | delete $redis{timeout}; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | bless \$clientId, $class; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 PRINT | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Sends the message(s) to the client. Since we're using an AnyEvent connection, | ||||||||||||||||||||||||||||
| 79 | events are still processed while waiting on Redis to process the push, | ||||||||||||||||||||||||||||
| 80 | including asynchronously pushing _other_ messages. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head3 Usage | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | print CLIENT nfreeze({ text => "foo", from => "bar" }); | ||||||||||||||||||||||||||||
| 85 | print CLIENT nfreeze({ text => "foo" }), nfreeze({ text => "bar" }), "System message"; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub PRINT { | ||||||||||||||||||||||||||
|
Subroutine
PRINT: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 89 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | $redis->ping or $redis = Redis->new(%redis); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | foreach (@_) { | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $redis->lpush($$this, $_) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | croak qq{Failed to push message [$_] to [$$this]: $!}; | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | return 1; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 READLINE | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Reads the next message or flushes the message queue (depending on context). | ||||||||||||||||||||||||||||
| 101 | This is a "blocking" operation, but, because we're using AnyEvent::Redis, other | ||||||||||||||||||||||||||||
| 102 | events are still processed while we wait. Since Redis's C<BLPOP> operation | ||||||||||||||||||||||||||||
| 103 | blocks the whole connection, this spawns a separate AnyEvent::Redis connection | ||||||||||||||||||||||||||||
| 104 | to deal with the blocking operation. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head3 Usage | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $message = <CLIENT>; # Reads only the next one | ||||||||||||||||||||||||||||
| 109 | my @messages = <CLIENT>; # Flushes the message queue into @messages | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub READLINE { | ||||||||||||||||||||||||||
|
Subroutine
READLINE: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 113 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | $redis->ping or $redis = Redis->new(%redis); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $r = AnyEvent::Redis->new(%redis) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | croak qq(Couldn't create AnyEvent::Redis connection to [@{[%redis]}]: $!); | |||||||||||||||||||||||||||
| 117 | 0 | my $message; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | until ($message) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $cv = $r->brpop($$this, $timeout, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 0 | $message = $_[0][1]; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | }) or croak qq{Couldn't BRPOP from [$$this]: $!}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $cv->recv; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | $r->quit; undef $r; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return $message unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | return ($message, _flush($this)); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =for READLINE | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Helper methods for READLINE | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | If you pass C<_flush> a nonzero number, it will read that many messages. An | ||||||||||||||||||||||||||||
| 134 | explicit "0" means "read nothing", while an C<undef> means "read everything". | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub _flush { | ||||||||||||||||||||||||||
|
Subroutine
_flush: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 138 | 0 | my ($this,$count) = @_; | |||||||||||||||||||||||||||
| 139 | 0 | my @messages; | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | while (my $m = $redis->rpop($$this)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | last if defined $count && --$count < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $count and --$count < 0
MC/DC0%0/2
defined $count and --$count < 0
defined $count
--$count < 0
Truth table0%0/3
A and B
A= defined $countB= --$count < 0
| |||||||||||||||||||||||||||||
| 142 | 0 | push @messages, $m; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | return @messages; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 EOF | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Just like the regular C<eof> call. Returns 1 if the next read will be the end | ||||||||||||||||||||||||||||
| 150 | of the queue or if the queue isn't open. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub EOF { | ||||||||||||||||||||||||||
|
Subroutine
EOF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 154 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 155 | 0 | return not _len($this); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =for EOF,READLINE | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Returns the length of the buffer. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub _len { | ||||||||||||||||||||||||||
|
Subroutine
_len: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 165 | 0 | return $redis->llen($$this); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 poll_once | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Returns the C<AnyEvent::Condvar> of the a blocking pop operation on a Redis queue. | ||||||||||||||||||||||||||||
| 171 | This is useful if, for example, you want to handle a C<BLPOP> as an asynchronous | ||||||||||||||||||||||||||||
| 172 | PSGI handler, since a standard C<READLINE> operation throws a "recursive blocking | ||||||||||||||||||||||||||||
| 173 | wait" exception (because you're waiting on a C<CondVar> that's waiting on a | ||||||||||||||||||||||||||||
| 174 | C<CondVar>). It takes a C<tied> variable, an optional C<count> of the maximum | ||||||||||||||||||||||||||||
| 175 | number of messages to return, and a callback as its arguments. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head3 Usage | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | sub get { | ||||||||||||||||||||||||||||
| 180 | my ($self,$clientId) = (+shift,+shift); | ||||||||||||||||||||||||||||
| 181 | my $output = tie local *CLIENT, 'Redis::MessageQueue', "$clientId:out"; | ||||||||||||||||||||||||||||
| 182 | $output->poll_once(sub { | ||||||||||||||||||||||||||||
| 183 | $self->write(+shift); | ||||||||||||||||||||||||||||
| 184 | $self->finish; | ||||||||||||||||||||||||||||
| 185 | }); | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub poll_once { | ||||||||||||||||||||||||||
|
Subroutine
poll_once: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 0 | my $fn = pop; | |||||||||||||||||||||||||||
| 191 | 0 | my ($this,$count) = @_; | |||||||||||||||||||||||||||
| 192 | 0 | my $r = AnyEvent::Redis->new(%redis); | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | $r->brpop($$this, $timeout, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 194 | 0 | my $message = $_[0][1]; | |||||||||||||||||||||||||||
| 195 | 0 | $r->quit; undef $r; | |||||||||||||||||||||||||||
| 196 | 0 | return $fn->($message, _flush($this,$count)); | |||||||||||||||||||||||||||
| 197 | 0 | }); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 CLOSE | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Cleanup code so that we don't end up with a bunch of open filehandles. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub CLOSE { | ||||||||||||||||||||||||||
|
Subroutine
CLOSE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 206 | # The elements of @_ are *aliases*, not copies, so undefing $_[0] marks | ||||||||||||||||||||||||||||
| 207 | # the caller's typeglob as empty. | ||||||||||||||||||||||||||||
| 208 | 0 | $redis->ping; | |||||||||||||||||||||||||||
| 209 | 0 | undef $_[0]; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 2 | 1; | |||||||||||||||||||||||||||