Redis.pm

filter: Statementstmt 24.4% 62 / 254 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 35.2% 18 / 51 Podpod 70.3% 19 / 27 totaltotal 19.5% 99 / 506 CC 65 SCAR 37.8
CC
65
Cov
16%
handle_pubsub_message
210.0
connect
72.0
execute_command
72.0
CRAP 43.8
« Redis/Server/Database.pm ↑ Summary Redis/Protocol.pm »
1 package Net::Async::Redis;
2 # ABSTRACT: Redis support for IO::Async
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use parent qw(Net::Async::Redis::Commands IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '2.001';
10  
11 =head1 NAME
12  
13 Net::Async::Redis - talk to Redis servers via L<IO::Async>
14  
15 =head1 SYNOPSIS
16  
17 use Net::Async::Redis;
18 use Future::AsyncAwait;
19 use IO::Async::Loop;
20 my $loop = IO::Async::Loop->new;
21 $loop->add(my $redis = Net::Async::Redis->new);
22 (async sub {
23 await $redis->connect;
24 my $value = await $redis->get('some_key');
25 $value ||= await $redis->set(some_key => 'some_value');
26 print "Value: $value";
27 })->()->get;
28  
29 # You can also use ->then chaining, see L<Future> for more details
30 $redis->connect->then(sub {
31 $redis->get('some_key')
32 })->then(sub {
33 my $value = shift;
34 return Future->done($value) if $value;
35 $redis->set(some_key => 'some_value')
36 })->on_done(sub {
37 print "Value: " . shift;
38 })->get;
39  
40 =head1 DESCRIPTION
41  
42 Provides client access for dealing with Redis servers.
43  
44 See L<Net::Async::Redis::Commands> for the full list of commands, this list
45 is autogenerated from the official documentation here:
46  
47 L<https://redis.io/commands>
48  
49 Note that this module uses L<Future::AsyncAwait>.
50  
51 =cut
52  
53 6 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 6 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 6 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 6 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 6 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 6 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 6 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 6 ▶use URI::redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 6 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 6 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 6 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 6 ▶use Net::Async::Redis::Multi;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 6 ▶use Net::Async::Redis::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 6 ▶use Net::Async::Redis::Subscription::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 =head1 METHODS
73  
74 B<NOTE>: For a full list of the Redis methods supported by this module,
75 please see L<Net::Async::Redis::Commands>.
76  
77 =cut
78  
79 =head1 METHODS - Subscriptions
80  
81 See L<https://redis.io/topics/pubsub> for more details on this topic.
82 There's also more details on the internal implementation in Redis here:
83 L<https://making.pusher.com/redis-pubsub-under-the-hood/>.
84  
85 =cut
86  
87 =head2 psubscribe
88  
89 Subscribes to a pattern.
90  
91 Returns a L<Future> which resolves to a L<Net::Async::Redis::Subscription> instance.
92  
93 =cut
94  
95 0 ▶async sub psubscribe {
Subroutine psubscribe: not called CC=4 SCAR=30.0
Pod documented
96 0 my ($self, $pattern) = @_;
97 0 await $self->next::method($pattern);
98 0 ▶ $self->{pubsub} //= 0;
Condition0%0/2
$self->{"pubsub"} //= 0
l!l
00
MC/DC0%0/1
$self->{"pubsub"} //= 0
$self->{"pubsub"}
Truth table0%0/2
A
A= $self->{"pubsub"}
Aresult
11
00
99 0 ▶ $self->{pending_subscription_pattern_channel}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']');
Condition0%0/3
$self->{"pending_subscription_pattern_channel"}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pending_subscription_pattern_channel"}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']')
$self->{"pending_subscription_pattern_channel"}{$pattern} $self->future('pattern_subscription[' . $pattern . ']')
Truth table0%0/3
A //= B
A= $self->{"pending_subscription_pattern_channel"}{$pattern}B= $self->future('pattern_subscription[' . $pattern . ']')
ABresult
1X1
011
000
100 0 ▶ return $self->{subscription_pattern_channel}{$pattern} //= Net::Async::Redis::Subscription->new(
Condition0%0/3
$self->{"subscription_pattern_channel"}{$pattern} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"subscription_pattern_channel"}{$pattern} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
$self->{"subscription_pattern_channel"}{$pattern} "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
Truth table0%0/3
A //= B
A= $self->{"subscription_pattern_channel"}{$pattern}B= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
ABresult
1X1
011
000
101 redis => $self,
102 channel => $pattern
103 )
104 6 }
105  
106 =head2 subscribe
107  
108 Subscribes to one or more channels.
109  
110 Returns a L<Future> which resolves to a L<Net::Async::Redis::Subscription> instance.
111  
112 Example:
113  
114 # Subscribe to 'notifications' channel,
115 # print the first 5 messages, then unsubscribe
116 $redis->subscribe('notifications')
117 ->then(sub {
118 my $sub = shift;
119 $sub->events
120 ->map('payload')
121 ->take(5)
122 ->say
123 ->completed
124 })->then(sub {
125 $redis->unsubscribe('notifications')
126 })->get
127  
128 =cut
129  
130 0 ▶async sub subscribe {
Subroutine subscribe: not called CC=3 SCAR=24.8
Pod documented
131 0 my ($self, @channels) = @_;
132 0 await $self->next::method(@channels);
133 0 $log->tracef('Marking as pubsub mode');
134 0 ▶ $self->{pubsub} //= 0;
Condition0%0/2
$self->{"pubsub"} //= 0
l!l
00
MC/DC0%0/1
$self->{"pubsub"} //= 0
$self->{"pubsub"}
Truth table0%0/2
A
A= $self->{"pubsub"}
Aresult
11
00
135 await Future->wait_all(
136 map {
137 0 ▶ $self->{pending_subscription_channel}{$_} //= $self->future('subscription[' . $_ . ']')
Condition0%0/3
$self->{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
$self->{"pending_subscription_channel"}{$_} $self->future('subscription[' . $_ . ']')
Truth table0%0/3
A //= B
A= $self->{"pending_subscription_channel"}{$_}B= $self->future('subscription[' . $_ . ']')
ABresult
1X1
011
000
138 } @channels
139 );
140 0 return @{$self->{subscription_channel}}{@channels};
141 6 }
142  
143 =head1 METHODS - Transactions
144  
145 =head2 multi
146  
147 Executes the given code in a Redis C<MULTI> transaction.
148  
149 This will cause each of the requests to be queued, then executed in a single atomic transaction.
150  
151 Example:
152  
153 $redis->multi(sub {
154 my $tx = shift;
155 $tx->incr('some::key')->on_done(sub { print "Final value for incremented key was " . shift . "\n"; });
156 $tx->set('other::key => 'test data')
157 })->then(sub {
158 my ($success, $failure) = @_;
159 return Future->fail("Had $failure failures, expecting everything to succeed") if $failure;
160 print "$success succeeded\m";
161 return Future->done;
162 })->retain;
163  
164 =cut
165  
166 0 ▶async sub multi {
Subroutine multi: not called CC=4 SCAR=30.0
Pod documented
167 0 my ($self, $code) = @_;
168 0 ▶ die 'Need a coderef' unless $code and reftype($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $code and builtin::reftype($code) eq "CODE" 0 0 0
Condition0%0/3
$code and builtin::reftype($code) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$code and builtin::reftype($code) eq "CODE"
$code builtin::reftype($code) eq "CODE"
Truth table0%0/3
A and B
A= $codeB= builtin::reftype($code) eq "CODE"
ABresult
0X0
100
111
169  
170 0 my $multi = Net::Async::Redis::Multi->new(
171 redis => $self,
172 );
173 0 my @pending = @{$self->{pending_multi}};
174  
175 0 $log->tracef('Have %d pending MULTI transactions',
176 0 + @pending
177 );
178 0 push @{$self->{pending_multi}}, $self->loop->new_future->set_label($self->command_label('multi'));
179  
180 0 ▶ await Future->wait_all(
Branch: 1 branch
BranchTrueFalseTotal
if @pending 0 0 0
181 @pending
182 ) if @pending;
183 0 await do {
184 0 local $self->{_is_multi} = 1;
185 0 Net::Async::Redis::Commands::multi($self);
186 };
187 0 return await $multi->exec($code)
188 6 }
189  
190 around [qw(discard exec)] => sub {
191 0 ▶ my ($code, $self, @args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 local $self->{_is_multi} = 1;
193 0 my $f = $self->$code(@args);
194 0 (shift @{$self->{pending_multi}})->done;
195 0 $f
196 6 };
197  
198 =head1 METHODS - Generic
199  
200 =head2 keys
201  
202 =cut
203  
204 sub keys : method {
205 0 ▶ my ($self, $match) = @_;
Subroutine keys: not called CC=2 SCAR=17.9
Pod documented
206 0 ▶ $match //= '*';
Condition0%0/2
$match //= "*"
l!l
00
MC/DC0%0/1
$match //= "*"
$match
Truth table0%0/2
A
A= $match
Aresult
11
00
207 0 return $self->next::method($match);
208 }
209  
210 =head2 watch_keyspace
211  
212 A convenience wrapper around the keyspace notifications API.
213  
214 Provides the necessary setup to establish a C<PSUBSCRIBE> subscription
215 on the C<__keyspace@*__> namespace, setting the configuration required
216 for this to start emitting events, and then calls C<$code> with each
217 event.
218  
219 Note that this will switch the connection into pubsub mode, so it will
220 no longer be available for any other activity.
221  
222 Resolves to a L<Ryu::Source> instance.
223  
224 =cut
225  
226 0 ▶async sub watch_keyspace {
Subroutine watch_keyspace: not called CC=4 SCAR=30.0
Pod documented
227 0 my ($self, $pattern, $code) = @_;
228 0 ▶ $pattern //= '*';
Condition0%0/2
$pattern //= "*"
l!l
00
MC/DC0%0/1
$pattern //= "*"
$pattern
Truth table0%0/2
A
A= $pattern
Aresult
11
00
229 0 my $sub_name = '__keyspace@*__:' . $pattern;
230 0 ▶ $self->{have_notify} ||= await $self->config_set(
Condition0%0/3
$self->{"have_notify"} ||= XXX
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"have_notify"} ||= XXX
$self->{"have_notify"} XXX
Truth table0%0/3
A ||= B
A= $self->{"have_notify"}B= XXX
ABresult
1X1
011
000
231 'notify-keyspace-events', 'Kg$xe'
232 );
233 0 my $sub = await $self->psubscribe($sub_name);
234 0 my $ev = $sub->events;
235 $ev->each(sub {
236 0 ▶ my $message = $_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
237 0 $log->tracef('Keyspace notification for channel %s, type %s, payload %s', map $message->$_, qw(channel type payload));
238 0 my $k = $message->channel;
239 0 $k =~ s/^[^:]+://;
240 0 my $f = $code->($message->payload, $k);
241 0 ▶ $f->retain if blessed($f) and $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($f) and $f->isa("Future") 0 0 0
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
242 0 ▶ }) if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
243 0 return $ev;
244 6 }
245  
246 =head2 endpoint
247  
248 The string describing the remote endpoint.
249  
250 =cut
251  
252 0 ▶sub endpoint { shift->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
253  
254 =head2 local_endpoint
255  
256 A string describing the local endpoint, usually C<host:port>.
257  
258 =cut
259  
260 0 ▶sub local_endpoint { shift->{local_endpoint} }
Subroutine local_endpoint: not called CC=1 SCAR=6.9
Pod documented
261  
262 =head2 connect
263  
264 Connects to the Redis server.
265  
266 =cut
267  
268 sub connect : method {
269 0 ▶ my ($self, %args) = @_;
Subroutine connect: not called CC=8 SCAR=42.8
Pod documented
270 0 ▶ $self->configure(%args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 0 0
271 0 my $uri = $self->uri->clone;
272 0 for (qw(host port)) {
273 0 ▶ $uri->$_($self->$_) if defined $self->$_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$_ 0 0 0
274 }
275 0 my $auth = $self->{auth};
276 0 ▶ $auth //= ($uri->userinfo =~ s{^[^:]*:}{}r) if defined $uri->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo 0 0 0
Condition0%0/3
$auth //= $uri->userinfo =~ s/^[^:]*://r
l!l&&r!l&&!r
000
MC/DC0%0/2
$auth //= $uri->userinfo =~ s/^[^:]*://r
$auth $uri->userinfo =~ s/^[^:]*://r
Truth table0%0/3
A //= B
A= $authB= $uri->userinfo =~ s/^[^:]*://r
ABresult
1X1
011
000
277 $self->{connection} //= $self->loop->connect(
278 service => $uri->port // 6379,
279 host => $uri->host,
280 socktype => 'stream',
281 )->then(sub {
282 0 ▶ my ($sock) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
283 0 $self->{endpoint} = join ':', $sock->peerhost, $sock->peerport;
284 0 $self->{local_endpoint} = join ':', $sock->sockhost, $sock->sockport;
285 0 my $proto = $self->protocol;
286 my $stream = IO::Async::Stream->new(
287 handle => $sock,
288 read_len => $self->stream_read_len,
289 write_len => $self->stream_write_len,
290 read_high_watermark => 8 * $self->stream_read_len,
291 read_low_watermark => 2 * $self->stream_read_len,
292 on_closed => $self->curry::weak::notify_close,
293 on_read => sub {
294 0 ▶ $proto->parse($_[1]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 0
296 }
297 0 );
298 0 $self->add_child($stream);
299 Scalar::Util::weaken(
300 0 $self->{stream} = $stream
301 );
302 0 ▶ return $self->auth($auth) if defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth 0 0 0
303 0 return Future->done;
304 0 ▶ })->on_fail(sub { delete $self->{connection} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/5
$self->{"connection"} //= $self->loop->connect("service", $uri->port // 6379, "host", $uri->host, "socktype", "stream")->then(sub { my($sock) = @_; $self->{"endpoint"} = join(":", $sock->peerhost, $sock->peerport); $self->{"local_endpoint"} = join(":", $sock->sockhost, $sock->sockport); my $proto = $self->protocol; my $stream = "IO::Async::Stream"->new("handle", $sock, "read_len", $self->stream_read_len, "write_len", $self->stream_write_len, "read_high_watermark", 8 * $self->stream_read_len, "read_low_watermark", 2 * $self->stream_read_len, "on_closed", $self->curry::weak::notify_close, "on_read", sub { $proto->parse($_[1]); 0; } ); $self->add_child($stream); builtin::weaken($self->{"stream"} = $stream); return $self->auth($auth) if defined $auth; return "Future"->done; } )->on_fail(sub { delete $self->{"connection"}; } )
l!l&&r!l&&!r
000
$uri->port // 6379
l!l
00
MC/DC0%0/2
$self->{"connection"} //= $self->loop->connect("service", $uri->port // 6379, "host", $uri->host, "socktype", "stream")->then(sub { my($sock) = @_; $self->{"endpoint"} = join(":", $sock->peerhost, $sock->peerport); $self->{"local_endpoint"} = join(":", $sock->sockhost, $sock->sockport); my $proto = $self->protocol; my $stream = "IO::Async::Stream"->new("handle", $sock, "read_len", $self->stream_read_len, "write_len", $self->stream_write_len, "read_high_watermark", 8 * $self->stream_read_len, "read_low_watermark", 2 * $self->stream_read_len, "on_closed", $self->curry::weak::notify_close, "on_read", sub { $proto->parse($_[1]); 0; } ); $self->add_child($stream); builtin::weaken($self->{"stream"} = $stream); return $self->auth($auth) if defined $auth; return "Future"->done; } )->on_fail(sub { delete $self->{"connection"}; } )
$self->{"connection"} $self->loop->connect("service", $uri->port // 6379, "host", $uri->host, "socktype", "stream")->then(sub { my($sock) = @_; $self->{"endpoint"} = join(":", $sock->peerhost, $sock->peerport); $self->{"local_endpoint"} = join(":", $sock->sockhost, $sock->sockport); my $proto = $self->protocol; my $stream = "IO::Async::Stream"->new("handle", $sock, "read_len", $self->stream_read_len, "write_len", $self->stream_write_len, "read_high_watermark", 8 * $self->stream_read_len, "read_low_watermark", 2 * $self->stream_read_len, "on_closed", $self->curry::weak::notify_close, "on_read", sub { $proto->parse($_[1]); 0; } ); $self->add_child($stream); builtin::weaken($self->{"stream"} = $stream); return $self->auth($auth) if defined $auth; return "Future"->done; } )->on_fail(sub { delete $self->{"connection"}; } )
MC/DC0%0/1
$uri->port // 6379
$uri->port
Truth table0%0/3
A //= B
A= $self->{"connection"}B= $self->loop->connect("service", $uri->port // 6379, "host", $uri->host, "socktype", "stream")->then(sub { my($sock) = @_; $self->{"endpoint"} = join(":", $sock->peerhost, $sock->peerport); $self->{"local_endpoint"} = join(":", $sock->sockhost, $sock->sockport); my $proto = $self->protocol; my $stream = "IO::Async::Stream"->new("handle", $sock, "read_len", $self->stream_read_len, "write_len", $self->stream_write_len, "read_high_watermark", 8 * $self->stream_read_len, "read_low_watermark", 2 * $self->stream_read_len, "on_closed", $self->curry::weak::notify_close, "on_read", sub { $proto->parse($_[1]); 0; } ); $self->add_child($stream); builtin::weaken($self->{"stream"} = $stream); return $self->auth($auth) if defined $auth; return "Future"->done; } )->on_fail(sub { delete $self->{"connection"}; } )
ABresult
1X1
011
000
Truth table0%0/2
A
A= $uri->port
Aresult
11
00
305 }
306  
307 =head2 connected
308  
309 Establishes a connection if needed, otherwise returns an immediately-available
310 L<Future> instance.
311  
312 =cut
313  
314 sub connected {
315 0 ▶ my ($self) = @_;
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
316 0 ▶ return $self->{connection} if $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connection"} 0 0 0
317 0 $self->connect;
318 }
319  
320 =head2 on_message
321  
322 Called for each incoming message.
323  
324 Passes off the work to L</handle_pubsub_message> or the next queue
325 item, depending on whether we're dealing with subscriptions at the moment.
326  
327 =cut
328  
329 sub on_message {
330 0 ▶ my ($self, $data) = @_;
Subroutine on_message: not called CC=3 SCAR=24.8
Pod documented
331 0 local @{$log->{context}}{qw(redis_remote redis_local)} = ($self->endpoint, $self->local_endpoint);
332 0 $log->tracef('Incoming message: %s', $data);
333 0 ▶ return $self->handle_pubsub_message(@$data) if exists $self->{pubsub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"pubsub"} 0 0 0
334  
335 0 ▶ my $next = shift @{$self->{pending}} or die "No pending handler";
Branch: 1 branch
BranchTrueFalseTotal
my $next = shift @{$$self{"pending"};} or die "No pending handler" 0 0 0
336 0 $next->[1]->done($data);
337 }
338  
339 sub on_error_message {
340 0 ▶ my ($self, $data) = @_;
Subroutine on_error_message: not called CC=2 SCAR=17.9
Pod undocumented
341 0 local @{$log->{context}}{qw(redis_remote redis_local)} = ($self->endpoint, $self->local_endpoint);
342 0 $log->tracef('Incoming error message: %s', $data);
343  
344 0 ▶ my $next = shift @{$self->{pending}} or die "No pending handler";
Branch: 1 branch
BranchTrueFalseTotal
my $next = shift @{$$self{"pending"};} or die "No pending handler" 0 0 0
345 0 $next->[1]->fail($data);
346 }
347  
348 sub handle_pubsub_message {
349 0 ▶ my ($self, $type, @details) = @_;
Subroutine handle_pubsub_message: not called CC=14 SCAR=53.5
Pod undocumented
350 0 $type = lc $type;
351 0 ▶ if($type eq 'message') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "message") 0 0 0
352 0 my ($channel, $payload) = @details;
353 0 ▶ if(my $sub = $self->{subscription_channel}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{'subscription_channel'}{$channel}) { } 0 0 0
354 0 my $msg = Net::Async::Redis::Subscription::Message->new(
355 type => $type,
356 channel => $channel,
357 payload => $payload,
358 redis => $self,
359 subscription => $sub
360 );
361 0 $sub->events->emit($msg);
362 } else {
363 0 $log->warnf('Have message for unknown channel [%s]', $channel);
364 }
365 0 ▶ $self->bus->invoke_event(message => [ $type, $channel, $payload ]) if exists $self->{bus};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bus"} 0 0 0
366 0 return;
367 }
368 0 ▶ if($type eq 'pmessage') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "pmessage") 0 0 0
369 0 my ($pattern, $channel, $payload) = @details;
370 0 ▶ if(my $sub = $self->{subscription_pattern_channel}{$pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{'subscription_pattern_channel'}{$pattern}) { } 0 0 0
371 0 my $msg = Net::Async::Redis::Subscription::Message->new(
372 type => $type,
373 pattern => $pattern,
374 channel => $channel,
375 payload => $payload,
376 redis => $self,
377 subscription => $sub
378 );
379 0 $sub->events->emit($msg);
380 } else {
381 0 $log->warnf('Have message for unknown channel [%s]', $channel);
382 }
383 0 ▶ $self->bus->invoke_event(message => [ $type, $channel, $payload ]) if exists $self->{bus};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bus"} 0 0 0
384 0 return;
385 }
386  
387 0 my ($channel, $payload) = @details;
388 0 ▶ my $k = (substr $type, 0, 1) eq 'p' ? 'subscription_pattern_channel' : 'subscription_channel';
Branch: 1 branch
BranchTrueFalseTotal
substr($type, 0, 1) eq 'p' ? : 0 0 0
389 0 ▶ if($type =~ /unsubscribe$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /unsubscribe$/) { } 0 0 0
elsif ($type =~ /subscribe$/) { } 0 0 0
390 0 --$self->{pubsub};
391 0 ▶ if(my $sub = delete $self->{$k}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = delete $self->{$k}{$channel}) { } 0 0 0
392 0 $log->tracef('Removed subscription for [%s]', $channel);
393 } else {
394 0 $log->warnf('Have unsubscription for unknown channel [%s]', $channel);
395 }
396 } elsif($type =~ /subscribe$/) {
397 0 ▶ $log->tracef('Have %s subscription for [%s]', (exists $self->{$k}{$channel} ? 'existing' : 'new'), $channel);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$k}{$channel} ? : 0 0 0
398 0 ++$self->{pubsub};
399 0 ▶ $self->{$k}{$channel} //= Net::Async::Redis::Subscription->new(
Condition0%0/3
$self->{$k}{$channel} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$k}{$channel} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
$self->{$k}{$channel} "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
Truth table0%0/3
A //= B
A= $self->{$k}{$channel}B= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
ABresult
1X1
011
000
400 redis => $self,
401 channel => $channel
402 );
403 0 ▶ $self->{'pending_' . $k}{$channel}->done($payload) unless $self->{'pending_' . $k}{$channel}->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{'pending_' . $k}{$channel}->is_done 0 0 0
404 } else {
405 0 $log->warnf('have unknown pubsub message type %s with channel %s payload %s', $type, $channel, $payload);
406 }
407 }
408  
409 =head2 stream
410  
411 Represents the L<IO::Async::Stream> instance for the active Redis connection.
412  
413 =cut
414  
415 0 ▶sub stream { shift->{stream} }
Subroutine stream: not called CC=1 SCAR=6.9
Pod documented
416  
417 =head2 pipeline_depth
418  
419 Number of requests awaiting responses before we start queuing.
420 This defaults to an arbitrary value of 100 requests.
421  
422 Note that this does not apply when in L<transaction|METHODS - Transactions> (C<MULTI>) mode.
423  
424 See L<https://redis.io/topics/pipelining> for more details on this concept.
425  
426 =cut
427  
428 0 ▶sub pipeline_depth { shift->{pipeline_depth} //= 100 }
Subroutine pipeline_depth: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"pipeline_depth"} //= 100
l!l
00
MC/DC0%0/1
+(shift())->{"pipeline_depth"} //= 100
+(shift())->{"pipeline_depth"}
Truth table0%0/2
A
A= +(shift())->{"pipeline_depth"}
Aresult
11
00
429  
430 =head1 METHODS - Deprecated
431  
432 This are still supported, but no longer recommended.
433  
434 =cut
435  
436 sub bus {
437 0 ▶ shift->{bus} //= do {
Subroutine bus: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
+(shift())->{"bus"} //= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"bus"} //= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
+(shift())->{"bus"} do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
Truth table0%0/3
A //= B
A= +(shift())->{"bus"}B= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
ABresult
1X1
011
000
438 0 require Mixin::Event::Dispatch::Bus;
439 0 Mixin::Event::Dispatch::Bus->VERSION(2.000);
440 0 Mixin::Event::Dispatch::Bus->new
441 }
442 }
443  
444 =head1 METHODS - Internal
445  
446 =cut
447  
448 =head2 notify_close
449  
450 Called when the socket is closed.
451  
452 =cut
453  
454 sub notify_close {
455 0 ▶ my ($self) = @_;
Subroutine notify_close: not called CC=6 SCAR=37.4
Pod documented
456 # If we think we have an existing connection, it needs removing:
457 # there's no guarantee that it's in a usable state.
458 0 ▶ if(my $stream = delete $self->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stream = delete $self->{"stream"}) 0 0 0
459 0 $stream->close_now;
460 }
461  
462 # Also clear our connection future so that the next request is triggered appropriately
463 0 delete $self->{connection};
464 # Clear out anything in the pending queue - we normally wouldn't expect anything to
465 # have ready status here, but no sense failing on a failure. Note that we aren't
466 # filtering out the list via grep because some of these Futures may be interdependent.
467 0 ▶ !$_->[1]->is_ready && $_->[1]->fail('Server connection is no longer active', redis => 'disconnected') for splice @{$self->{pending}};
Branch: 1 branch
BranchTrueFalseTotal
$_->[1]->is_ready or $_->[1]->fail("Server connection is no longer active", "redis", "disconnected") 0 0 0
468  
469 # Subscriptions also need clearing up
470 0 $_->cancel for values %{$self->{subscription_channel}};
471 0 $self->{subscription_channel} = {};
472 0 $_->cancel for values %{$self->{subscription_pattern_channel}};
473 0 $self->{subscription_pattern_channel} = {};
474  
475 0 $self->maybe_invoke_event(disconnect => );
476 }
477  
478 =head2 command_label
479  
480 Generate a label for the given command list.
481  
482 =cut
483  
484 sub command_label {
485 0 ▶ my ($self, @cmd) = @_;
Subroutine command_label: not called CC=2 SCAR=17.9
Pod documented
486 0 ▶ return join ' ', @cmd if $cmd[0] eq 'KEYS';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd[0] eq "KEYS" 0 0 0
487 0 return $cmd[0];
488 }
489  
490 6 our %ALLOWED_SUBSCRIPTION_COMMANDS = (
491 SUBSCRIBE => 1,
492 PSUBSCRIBE => 1,
493 UNSUBSCRIBE => 1,
494 PUNSUBSCRIBE => 1,
495 PING => 1,
496 QUIT => 1,
497 );
498  
499 sub execute_command {
500 0 ▶ my ($self, @cmd) = @_;
Subroutine execute_command: not called CC=8 SCAR=42.8
Pod undocumented
501  
502 # First, the rules: pubsub or plain
503 0 my $is_sub_command = exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]};
504 return Future->fail(
505 'Currently in pubsub mode, cannot send regular commands until unsubscribed',
506 redis =>
507 0 0 + (keys %{$self->{subscription_channel}}),
508 0 0 + (keys %{$self->{subscription_pattern_channel}})
509 0 ▶ ) if exists $self->{pubsub} and not $is_sub_command;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"pubsub"} and not $is_sub_command 0 0 0
Condition0%0/3
exists $self->{"pubsub"} and not $is_sub_command
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"pubsub"} and not $is_sub_command
exists $self->{"pubsub"} not $is_sub_command
Truth table0%0/3
A and not(B)
A= exists $self->{"pubsub"}B= not $is_sub_command
ABresult
0X0
100
111
510  
511 0 my $f = $self->loop->new_future->set_label($self->command_label(@cmd));
512 0 ▶ $log->tracef("Will have to wait for %d MULTI tx", 0 + @{$self->{pending_multi}}) unless $self->{_is_multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_is_multi"} 0 0 0
513 my $code = sub {
514 0 ▶ local @{$log->{context}}{qw(redis_remote redis_local)} = ($self->endpoint, $self->local_endpoint);
Subroutine __ANON__: not called CC=2 SCAR=17.9
515 0 my $cmd = join ' ', @cmd;
516 0 $log->tracef('Outgoing [%s]', $cmd);
517 0 push @{$self->{pending}}, [ $cmd, $f ];
518 0 $log->tracef("Pipeline depth now %d", 0 + @{$self->{pending}});
519 0 my $data = $self->protocol->encode_from_client(@cmd);
520 0 ▶ if($is_sub_command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_sub_command) { } 0 0 0
521 0 return $self->stream->write($data)->on_ready($f);
522 } else {
523 # Void-context write allows IaStream to combine multiple writes on the same connection.
524 0 $self->stream->write($data);
525 0 return $f
526 }
527 0 };
528 0 ▶ return $code->() if $self->{stream} and ($self->{is_multi} or 0 == @{$self->{pending_multi}});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stream"} and $self->{"is_multi"} || 0 == @{$self->{"pending_multi"};} 0 0 0
Condition0%0/6
$self->{"stream"} and $self->{"is_multi"} || 0 == @{$self->{"pending_multi"};}
!ll&&!rl&&r
000
$self->{"is_multi"} or 0 == @{$self->{"pending_multi"};}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"stream"} and $self->{"is_multi"} || 0 == @{$self->{"pending_multi"};}
$self->{"stream"} $self->{"is_multi"} 0 == @{$self->{"pending_multi"};}
Truth table0%0/4
A and B or C
A= $self->{"stream"}B= $self->{"is_multi"}C= 0 == @{$self->{"pending_multi"};}
ABCresult
0XX0
1000
11X1
1011
529 return (
530 $self->{_is_multi}
531 ? $self->connected
532 : Future->wait_all(
533 $self->connected,
534 0 ▶ @{$self->{pending_multi}}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_is_multi'} ? : 0 0 0
535 )
536 )->then($code)
537 ->retain;
538 }
539  
540 sub ryu {
541 0 ▶ my ($self) = @_;
Subroutine ryu: not called CC=2 SCAR=17.9
Pod undocumented
542 0 ▶ $self->{ryu} ||= do {
Condition0%0/3
$self->{"ryu"} ||= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ryu"} ||= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
$self->{"ryu"} do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
Truth table0%0/3
A ||= B
A= $self->{"ryu"}B= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
ABresult
1X1
011
000
543 0 $self->add_child(
544 my $ryu = Ryu::Async->new
545 );
546 0 $ryu
547 }
548 }
549  
550 sub future {
551 0 ▶ my ($self) = @_;
Subroutine future: not called CC=1 SCAR=6.9
Pod undocumented
552 0 return $self->loop->new_future(@_);
553 }
554  
555 sub protocol {
556 0 ▶ my ($self) = @_;
Subroutine protocol: not called CC=2 SCAR=17.9
Pod undocumented
557 0 ▶ $self->{protocol} ||= do {
Condition0%0/3
$self->{"protocol"} ||= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "error", $self->curry::weak::on_error_message) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"protocol"} ||= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "error", $self->curry::weak::on_error_message) }
$self->{"protocol"} do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "error", $self->curry::weak::on_error_message) }
Truth table0%0/3
A ||= B
A= $self->{"protocol"}B= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "error", $self->curry::weak::on_error_message) }
ABresult
1X1
011
000
558 0 require Net::Async::Redis::Protocol;
559 0 Net::Async::Redis::Protocol->new(
560 handler => $self->curry::weak::on_message,
561 error => $self->curry::weak::on_error_message,
562 )
563 };
564 }
565  
566 0 ▶sub host { shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
567 0 ▶sub port { shift->{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
568 0 ▶sub uri { shift->{uri} //= URI->new('redis://localhost') }
Subroutine uri: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
+(shift())->{"uri"} //= "URI"->new("redis://localhost")
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"uri"} //= "URI"->new("redis://localhost")
+(shift())->{"uri"} "URI"->new("redis://localhost")
Truth table0%0/3
A //= B
A= +(shift())->{"uri"}B= "URI"->new("redis://localhost")
ABresult
1X1
011
000
569  
570 =head2 stream_read_len
571  
572 Defines the buffer size when reading from a Redis connection.
573  
574 Defaults to 1MB, reduce this if you're dealing with a lot of connections and
575 want to minimise memory usage. Alternatively, if you're reading large amounts
576 of data and spend too much time in needless C<epoll_wait> calls, try a larger
577 value.
578  
579 =cut
580  
581 0 ▶sub stream_read_len { shift->{stream_read_len} //= 1048576 }
Subroutine stream_read_len: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"stream_read_len"} //= 1048576
l!l
00
MC/DC0%0/1
+(shift())->{"stream_read_len"} //= 1048576
+(shift())->{"stream_read_len"}
Truth table0%0/2
A
A= +(shift())->{"stream_read_len"}
Aresult
11
00
582  
583 =head2 stream_write_len
584  
585 The buffer size when writing to Redis connections, in bytes. Defaults to 1MB.
586  
587 See L</stream_read_len>.
588  
589 =cut
590  
591 0 ▶sub stream_write_len { shift->{stream_read_len} //= 1048576 }
Subroutine stream_write_len: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"stream_read_len"} //= 1048576
l!l
00
MC/DC0%0/1
+(shift())->{"stream_read_len"} //= 1048576
+(shift())->{"stream_read_len"}
Truth table0%0/2
A
A= +(shift())->{"stream_read_len"}
Aresult
11
00
592  
593 sub configure {
594 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=5 SCAR=34.0
Pod documented
595 0 ▶ $self->{pending_multi} //= [];
Condition0%0/2
$self->{"pending_multi"} //= []
l!l
00
MC/DC0%0/1
$self->{"pending_multi"} //= []
$self->{"pending_multi"}
Truth table0%0/2
A
A= $self->{"pending_multi"}
Aresult
11
00
596 0 for (qw(host port auth uri pipeline_depth stream_read_len stream_write_len)) {
597 0 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 0 0
598 }
599 0 ▶ $self->{uri} = URI->new($self->{uri}) unless ref $self->uri;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->uri 0 0 0
600 0 $self->next::method(%args)
601 }
602  
603 6 1;
604  
605 __END__
« Redis/Server/Database.pm ↑ Summary Redis/Protocol.pm »