blib/lib/Async/Redis/Subscription.pm

filter: Statementstmt 26.4% 87 / 329 Branchbran 7.2% 13 / 180 Conditioncond 4.0% 3 / 74 MC/DCmcdc 2.2% 1 / 45 Subroutinesub 46.5% 20 / 43 Podpod 93.3% 14 / 15 totaltotal 20.1% 138 / 686 CC 123 SCAR 47.2
CC
123
Cov
18%
_run_driver
272.0
_dequeue
240.0
_dispatch_frame
240.0
CRAP 111.9
« blib/lib/Async/Redis.pm ↑ Summary blib/lib/Async/Redis/Iterator.pm »
1 package Async::Redis::Subscription;
2  
3 91 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 91 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 91 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 91 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 91 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 91 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 91 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 91 ▶use Scalar::Util qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # Threshold for periodic event-loop yield inside the callback driver
15 # loop. Prevents stack growth when many messages are pre-queued and
16 # await on an already-ready Future returns synchronously.
17 91 ▶use constant MAX_SYNC_DEPTH => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 4 my ($class, %args) = @_;
21  
22 return bless {
23 redis => $args{redis},
24 4 channels => {}, # channel => 1 (for regular subscribe)
25 patterns => {}, # pattern => 1 (for psubscribe)
26 sharded_channels => {}, # channel => 1 (for ssubscribe)
27 _pending_messages => [], # Queued messages for iterator consumers
28 _message_waiter => undef, # Future signalled when a message arrives
29 _slot_waiter => undef, # Future signalled when queue drains below depth
30 _fatal_error => undef, # Typed error set by _fail_fatal
31 _on_reconnect => undef, # Callback for reconnect notification
32 _on_message => undef, # Message-arrived callback (callback mode)
33 _on_error => undef, # Fatal-error callback
34 _driver_step => undef, # Running driver loop Future (owned by _tasks selector)
35 _closed => 0,
36 _paused => 0, # Set during reconnect; clears in _resume_after_reconnect
37 }, $class;
38 }
39  
40 # Set/get reconnect callback
41 3 ▶sub on_reconnect {
Subroutine on_reconnect: called CC=2 SCAR=6.9
Pod documented
42 3 my ($self, $cb) = @_;
43 3 ▶ $self->{_on_reconnect} = $cb if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 2 3
44 3 return $self->{_on_reconnect};
45 }
46  
47 # Set/get message-arrived callback. Once set, next() croaks — the
48 # subscription is in callback mode for the rest of its lifetime.
49 # $cb->($sub, $msg) receives the Subscription and the message hashref.
50 0 ▶sub on_message {
Subroutine on_message: not called CC=5 SCAR=34.0
Pod documented
51 0 my ($self, $cb) = @_;
52 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
53 0 ▶ if (!$cb && $self->{_on_message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cb and $self->{"_on_message"}) 0 0 0
Condition0%0/3
not $cb and $self->{"_on_message"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $cb and $self->{"_on_message"}
not $cb $self->{"_on_message"}
Truth table0%0/3
not(A) and B
A= not $cbB= $self->{"_on_message"}
ABresult
0X0
100
111
54 0 Carp::croak(
55 "on_message is sticky; cannot clear once set "
56 . "(construct a new Subscription for iterator mode)"
57 );
58 }
59 0 $self->{_on_message} = $cb;
60 # If the subscription already has channels and is open, start
61 # the driver. If not, it'll be started when channels are added.
62 0 ▶ $self->_start_driver if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
63 }
64 0 return $self->{_on_message};
65 }
66  
67 # Set/get fatal-error callback. Fires once per fatal error; default
68 # (when unset) is to die so silent death is impossible.
69 # $cb->($sub, $err) receives the Subscription and the error.
70 0 ▶sub on_error {
Subroutine on_error: not called CC=2 SCAR=17.9
Pod documented
71 0 my ($self, $cb) = @_;
72 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
73 0 $self->{_on_error} = $cb;
74 }
75 0 return $self->{_on_error};
76 }
77  
78 # Invoke a user-supplied callback with the standard exception-handling
79 # policy: save/restore $@, use eval-and-check-boolean idiom to survive
80 # DESTROY side effects, and route die to the fatal-error handler.
81 # Returns the callback's return value. Task 7 wires backpressure: if
82 # the return is a Future the driver will await it before the next
83 # read. Task 6's driver does not yet consume the return value.
84 0 ▶sub _invoke_user_callback {
Subroutine _invoke_user_callback: not called CC=3 SCAR=24.8
85 0 my ($self, $cb, $msg) = @_;
86 0 local $@;
87 0 my $result;
88 0 my $ok = eval {
89 0 $result = $cb->($self, $msg);
90 0 1;
91 };
92 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
93 0 ▶ my $err = $@ // 'unknown error';
Condition0%0/2
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
94 0 $self->_handle_fatal_error("on_message callback died: $err");
95 0 return undef;
96 }
97 0 return $result;
98 }
99  
100 # Single chokepoint for fatal errors from either the read loop or the
101 # callback path. Closes the subscription, fires on_error if registered,
102 # and dies loudly if not. Loud-by-default prevents silent zombies.
103 0 ▶sub _handle_fatal_error {
Subroutine _handle_fatal_error: not called CC=4 SCAR=30.0
104 0 my ($self, $err) = @_;
105 0 $self->_close;
106 0 ▶ if (my $cb = $self->{_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"_on_error"}) 0 0 0
107 0 local $@;
108 0 my $ok = eval { $cb->($self, $err); 1 };
109 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
110 0 ▶ Carp::carp("on_error callback died: " . ($@ // 'unknown error'));
Condition0%0/2
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
111 }
112 0 return;
113 }
114 0 die $err;
115 }
116  
117 # Track a channel subscription
118 3 ▶sub _add_channel {
Subroutine _add_channel: called CC=1 SCAR=0.0
119 3 my ($self, $channel) = @_;
120 3 $self->{channels}{$channel} = 1;
121 3 $self->_start_driver;
122 }
123  
124 1 ▶sub _add_pattern {
Subroutine _add_pattern: called CC=1 SCAR=0.0
125 1 my ($self, $pattern) = @_;
126 1 $self->{patterns}{$pattern} = 1;
127 1 $self->_start_driver;
128 }
129  
130 0 ▶sub _add_sharded_channel {
Subroutine _add_sharded_channel: not called CC=1 SCAR=6.9
131 0 my ($self, $channel) = @_;
132 0 $self->{sharded_channels}{$channel} = 1;
133 0 $self->_start_driver;
134 }
135  
136 0 ▶sub _remove_channel {
Subroutine _remove_channel: not called CC=1 SCAR=6.9
137 0 my ($self, $channel) = @_;
138 0 delete $self->{channels}{$channel};
139 }
140  
141 0 ▶sub _remove_pattern {
Subroutine _remove_pattern: not called CC=1 SCAR=6.9
142 0 my ($self, $pattern) = @_;
143 0 delete $self->{patterns}{$pattern};
144 }
145  
146 0 ▶sub _remove_sharded_channel {
Subroutine _remove_sharded_channel: not called CC=1 SCAR=6.9
147 0 my ($self, $channel) = @_;
148 0 delete $self->{sharded_channels}{$channel};
149 }
150  
151 # List subscribed channels/patterns
152 2 ▶sub channels { keys %{shift->{channels}} }
Subroutine channels: called CC=1 SCAR=0.0
Pod documented
153 2 ▶sub patterns { keys %{shift->{patterns}} }
Subroutine patterns: called CC=1 SCAR=0.0
Pod documented
154 2 ▶sub sharded_channels { keys %{shift->{sharded_channels}} }
Subroutine sharded_channels: called CC=1 SCAR=0.0
Pod documented
155  
156 1 ▶sub channel_count {
Subroutine channel_count: called CC=1 SCAR=0.0
Pod documented
157 1 my ($self) = @_;
158 1 return scalar(keys %{$self->{channels}})
159 1 + scalar(keys %{$self->{patterns}})
160 1 + scalar(keys %{$self->{sharded_channels}});
161 }
162  
163 # Internal dequeue: wait for a message from the queue, dequeue it, and
164 # signal _slot_waiter so any pending _dispatch_frame can proceed. Used by
165 # both next() (iterator mode) and _start_driver (callback mode driver loop).
166 # Returns undef on clean close; dies with typed error on fatal close.
167 0 ▶async sub _dequeue {
Subroutine _dequeue: not called CC=15 SCAR=54.8
168 0 my ($self, $exit_on_pause) = @_;
169  
170 # Iterator mode (default): pause is transient. Block through it and
171 # return real messages once the driver resumes.
172 #
173 # Callback driver (exit_on_pause=1): exit cleanly on pause so the
174 # driver task can be restarted after reconnect without two drivers
175 # racing. _pause_for_reconnect wakes any in-flight _message_waiter
176 # via done() so this path can exit promptly.
177 0 ▶ while (!@{$self->{_pending_messages}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_pending_messages"};}) 0 0 0
178 0 ▶ die $self->{_fatal_error} if $self->{_fatal_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fatal_error"} 0 0 0
179 0 ▶ return undef if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
180 0 ▶ return undef if $exit_on_pause && $self->{_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $exit_on_pause and $self->{"_paused"} 0 0 0
Condition0%0/3
$exit_on_pause and $self->{"_paused"}
!ll&&!rl&&r
000
MC/DC0%0/2
$exit_on_pause and $self->{"_paused"}
$exit_on_pause $self->{"_paused"}
Truth table0%0/3
A and B
A= $exit_on_pauseB= $self->{"_paused"}
ABresult
0X0
100
111
181 0 ▶ $self->{_message_waiter} //= Future->new;
Condition0%0/3
$self->{"_message_waiter"} //= "Future"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_message_waiter"} //= "Future"->new
$self->{"_message_waiter"} "Future"->new
Truth table0%0/3
A //= B
A= $self->{"_message_waiter"}B= "Future"->new
ABresult
1X1
011
000
182 0 await $self->{_message_waiter};
183 0 delete $self->{_message_waiter};
184 }
185  
186 0 ▶ die $self->{_fatal_error} if $self->{_fatal_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fatal_error"} 0 0 0
187 0 ▶ return undef if $self->{_closed} && !@{$self->{_pending_messages}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} and not @{$self->{"_pending_messages"};} 0 0 0
Condition0%0/3
$self->{"_closed"} and not @{$self->{"_pending_messages"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_closed"} and not @{$self->{"_pending_messages"};}
$self->{"_closed"} not @{$self->{"_pending_messages"};}
Truth table0%0/3
A and not(B)
A= $self->{"_closed"}B= not @{$self->{"_pending_messages"};}
ABresult
0X0
100
111
188 return undef
189 0 ▶ if $exit_on_pause && $self->{_paused} && !@{$self->{_pending_messages}};
Branch: 1 branch
BranchTrueFalseTotal
if $exit_on_pause and $self->{"_paused"} and not @{$self->{"_pending_messages"};} 0 0 0
Condition0%0/6
$exit_on_pause and $self->{"_paused"} and not @{$self->{"_pending_messages"};}
!ll&&!rl&&r
000
$exit_on_pause and $self->{"_paused"}
!ll&&!rl&&r
000
MC/DC0%0/3
$exit_on_pause and $self->{"_paused"} and not @{$self->{"_pending_messages"};}
$exit_on_pause $self->{"_paused"} not @{$self->{"_pending_messages"};}
Truth table0%0/4
A and B and not(C)
A= $exit_on_pauseB= $self->{"_paused"}C= not @{$self->{"_pending_messages"};}
ABCresult
0XX0
10X0
1100
1111
190  
191 0 my $msg = shift @{$self->{_pending_messages}};
192 0 ▶ if (my $w = delete $self->{_slot_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_slot_waiter"}) 0 0 0
193 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
194 }
195 0 return $msg;
196 91 }
197  
198 # Receive next message (async iterator pattern). Waits on the queue
199 # populated by _run_reader via _dispatch_frame. Returns undef on clean
200 # close; dies with the typed error on fatal close.
201 0 ▶async sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
202 0 my ($self) = @_;
203  
204 # Exclusivity check: callback mode disables iterator mode.
205 0 ▶ if ($self->{_on_message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_message"}) 0 0 0
206 0 Carp::croak("Cannot call next() on a callback-driven subscription");
207 }
208  
209 # In iterator mode the unified reader (_run_reader) feeds the queue.
210 # The reader is already running (started by _pubsub_command during
211 # subscribe). No separate driver start is needed.
212  
213 0 return await $self->_dequeue;
214 91 }
215  
216 # Read one pub/sub frame from the underlying connection. On transient
217 # read error, attempt reconnect if enabled and fire on_reconnect on
218 # success; on unrecoverable failure, propagate the error.
219 # Returns a Future resolving to the raw frame (arrayref) or undef if
220 # the connection is gone and no more frames are available.
221 # Shared by next() and the callback driver loop added in a later task.
222 2 ▶async sub _read_frame_with_reconnect {
Subroutine _read_frame_with_reconnect: called CC=7 SCAR=21.8
223 2 my ($self) = @_;
224 2 my $redis = $self->{redis};
225  
226 2 while (1) {
227 2 my $frame;
228 2 my $ok = eval {
229 2 $frame = await $redis->_read_pubsub_frame;
230 0 1;
231 };
232  
233 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 0 2
234 2 my $error = $@;
235 2 ▶ if ($redis->{reconnect} && $self->channel_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redis->{"reconnect"} and $self->channel_count > 0) 1 1 2
Condition66%2/3
$redis->{"reconnect"} and $self->channel_count > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$redis->{"reconnect"} and $self->channel_count > 0
$redis->{"reconnect"} $self->channel_count > 0
Truth table66%2/3
A and B
A= $redis->{"reconnect"}B= $self->channel_count > 0
ABresult
0X0
100
111
236 1 my $reconnect_error;
237 1 my $reconnect_ok = eval {
238 1 await $redis->_reconnect_pubsub;
239 0 1;
240 };
241 1 ▶ $reconnect_error = $@ unless $reconnect_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $reconnect_ok 1 0 1
242 1 ▶ unless ($reconnect_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reconnect_ok) 1 0 1
243 1 die $reconnect_error;
244 }
245  
246 0 ▶ if ($self->{_on_reconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_reconnect"}) 0 0 0
247 0 $self->{_on_reconnect}->($self);
248 }
249  
250 0 next;
251 }
252 1 die $error;
253 }
254  
255 0 return $frame;
256 }
257 91 }
258  
259 # Convert a raw RESP pub/sub frame into a message hashref and deliver it.
260 # In callback mode, invokes _on_message via _invoke_user_callback and
261 # returns its result (which may be a Future for consumer-side backpressure).
262 # In iterator mode, queues the message into _pending_messages and signals
263 # _message_waiter so a blocked next() can wake up.
264 #
265 # Non-message frames (subscribe confirmations, etc.) return undef and
266 # take no action — the driver loop will read the next frame.
267 0 ▶sub _dispatch_frame {
Subroutine _dispatch_frame: not called CC=15 SCAR=54.8
268 0 my ($self, $frame) = @_;
269 0 ▶ return unless $frame && ref $frame eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $frame and ref $frame eq "ARRAY" 0 0 0
Condition0%0/3
$frame and ref $frame eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$frame and ref $frame eq "ARRAY"
$frame ref $frame eq "ARRAY"
Truth table0%0/3
A and B
A= $frameB= ref $frame eq "ARRAY"
ABresult
0X0
100
111
270  
271 0 ▶ my $type = $frame->[0] // '';
Condition0%0/2
$frame->[0] // ""
l!l
00
MC/DC0%0/1
$frame->[0] // ""
$frame->[0]
Truth table0%0/2
A
A= $frame->[0]
Aresult
11
00
272 0 my $msg;
273  
274 0 ▶ if ($type eq 'message') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'message') { } 0 0 0
elsif ($type eq 'pmessage') { } 0 0 0
elsif ($type eq 'smessage') { } 0 0 0
275 0 $msg = {
276 type => 'message',
277 channel => $frame->[1],
278 pattern => undef,
279 data => $frame->[2],
280 };
281 }
282 elsif ($type eq 'pmessage') {
283 0 $msg = {
284 type => 'pmessage',
285 pattern => $frame->[1],
286 channel => $frame->[2],
287 data => $frame->[3],
288 };
289 }
290 elsif ($type eq 'smessage') {
291 0 $msg = {
292 type => 'smessage',
293 channel => $frame->[1],
294 pattern => undef,
295 data => $frame->[2],
296 };
297 }
298 else {
299 0 return undef; # non-message frame (subscribe confirmation, etc.)
300 }
301  
302 # Queue the message for consumption by next() (iterator mode) or the
303 # callback driver loop (callback mode). The driver invokes _on_message;
304 # _dispatch_frame is intentionally agnostic about the consumption mode.
305 # This keeps backpressure uniform: the depth limit applies to both modes.
306 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
307  
308 0 my $redis = $self->{redis};
309 my $depth = ($redis && $redis->{message_queue_depth})
310 ? $redis->{message_queue_depth}
311 0 ▶ : 0; # 0 = unbounded (default)
Branch: 1 branch
BranchTrueFalseTotal
$redis && $redis->{'message_queue_depth'} ? : 0 0 0
Condition0%0/3
$redis and $redis->{"message_queue_depth"}
!ll&&!rl&&r
000
MC/DC0%0/2
$redis and $redis->{"message_queue_depth"}
$redis $redis->{"message_queue_depth"}
Truth table0%0/3
A and B
A= $redisB= $redis->{"message_queue_depth"}
ABresult
0X0
100
111
312  
313 0 ▶ if ($depth && scalar(@{$self->{_pending_messages}}) >= $depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth and scalar @{$self->{"_pending_messages"};} >= $depth) 0 0 0
Condition0%0/3
$depth and scalar @{$self->{"_pending_messages"};} >= $depth
!ll&&!rl&&r
000
MC/DC0%0/2
$depth and scalar @{$self->{"_pending_messages"};} >= $depth
$depth scalar @{$self->{"_pending_messages"};} >= $depth
Truth table0%0/3
A and B
A= $depthB= scalar @{$self->{"_pending_messages"};} >= $depth
ABresult
0X0
100
111
314 # Queue full. Return a Future that queues the message once a slot
315 # opens (signalled by next() calling _slot_waiter->done).
316 0 ▶ $self->{_slot_waiter} //= Future->new;
Condition0%0/3
$self->{"_slot_waiter"} //= "Future"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_slot_waiter"} //= "Future"->new
$self->{"_slot_waiter"} "Future"->new
Truth table0%0/3
A //= B
A= $self->{"_slot_waiter"}B= "Future"->new
ABresult
1X1
011
000
317 0 my $slot = $self->{_slot_waiter};
318 0 weaken(my $weak = $self);
319 0 ▶ return $slot->then(sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
320 0 ▶ return Future->done if !$weak || $weak->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if not $weak or $weak->{"_closed"} 0 0 0
Condition0%0/3
not $weak or $weak->{"_closed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $weak or $weak->{"_closed"}
not $weak $weak->{"_closed"}
Truth table0%0/3
not(A) or B
A= not $weakB= $weak->{"_closed"}
ABresult
1X1
011
000
321 0 push @{$weak->{_pending_messages}}, $msg;
322 0 ▶ if (my $w = delete $weak->{_message_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $weak->{"_message_waiter"}) 0 0 0
323 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
324 }
325 0 Future->done;
326 0 });
327 }
328  
329 0 push @{$self->{_pending_messages}}, $msg;
330 0 ▶ if (my $w = delete $self->{_message_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_message_waiter"}) 0 0 0
331 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
332 }
333 0 return undef;
334 }
335  
336 # The callback-mode driver loop. Consumes from _pending_messages via
337 # _dequeue (populated by _run_reader's dispatch path), invokes the
338 # user's _on_message callback, and awaits its returned Future if any
339 # for consumer-opted backpressure.
340 #
341 # Exits cleanly when _dequeue returns undef (subscription closed or
342 # paused for reconnect). Dies with the typed error if _dequeue dies
343 # (fatal); _run_driver's Future failure is visible through the
344 # client's Future::Selector to any caller using run_until_ready.
345 #
346 # Periodic sleep(0) yield every MAX_SYNC_DEPTH iterations prevents
347 # stack growth when messages are pre-queued and await returns
348 # synchronously from an already-ready Future.
349 0 ▶async sub _run_driver {
Subroutine _run_driver: not called CC=16 SCAR=56.1
350 0 my ($self) = @_;
351 0 my $iter = 0;
352 0 ▶ while (!$self->{_closed} && !$self->{_paused}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_closed"} || $self->{"_paused"}) 0 0 0
Condition0%0/3
$self->{'_closed'} || $self->{'_paused'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_closed'} || $self->{'_paused'}
$self->{'_closed'} $self->{'_paused'}
Truth table0%0/3
A || B
A= $self->{'_closed'}B= $self->{'_paused'}
ABresult
1X1
011
000
353 0 my $msg;
354 0 my $deq_ok = eval { $msg = await $self->_dequeue(1); 1 };
355 0 ▶ unless ($deq_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($deq_ok) 0 0 0
356 0 my $err = $@;
357 # _fail_fatal already set _closed and fired on_error; don't
358 # double-fire. Any other propagation path routes through
359 # _handle_fatal_error.
360 0 ▶ return if $self->{_closed} || $self->{_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} or $self->{"_paused"} 0 0 0
Condition0%0/3
$self->{"_closed"} or $self->{"_paused"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_closed"} or $self->{"_paused"}
$self->{"_closed"} $self->{"_paused"}
Truth table0%0/3
A or B
A= $self->{"_closed"}B= $self->{"_paused"}
ABresult
1X1
011
000
361 0 $self->_handle_fatal_error($err);
362 0 return;
363 }
364 0 ▶ last unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
365 0 ▶ last if $self->{_closed} || $self->{_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} or $self->{"_paused"} 0 0 0
Condition0%0/3
$self->{"_closed"} or $self->{"_paused"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_closed"} or $self->{"_paused"}
$self->{"_closed"} $self->{"_paused"}
Truth table0%0/3
A or B
A= $self->{"_closed"}B= $self->{"_paused"}
ABresult
1X1
011
000
366  
367 0 ▶ my $cb = $self->{_on_message} or last;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"_on_message"} or last 0 0 0
368 0 my $result = $self->_invoke_user_callback($cb, $msg);
369  
370 0 ▶ if (blessed($result) && $result->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa("Future")) 0 0 0
Condition0%0/3
builtin::blessed($result) and $result->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Future")
builtin::blessed($result) $result->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->isa("Future")
ABresult
0X0
100
111
371 0 my $cb_ok = eval { await $result; 1 };
372 0 ▶ unless ($cb_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb_ok) 0 0 0
373 0 my $err = $@;
374 0 ▶ return if $self->{_closed} || $self->{_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} or $self->{"_paused"} 0 0 0
Condition0%0/3
$self->{"_closed"} or $self->{"_paused"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_closed"} or $self->{"_paused"}
$self->{"_closed"} $self->{"_paused"}
Truth table0%0/3
A or B
A= $self->{"_closed"}B= $self->{"_paused"}
ABresult
1X1
011
000
375 0 $self->_handle_fatal_error(
376 "on_message callback Future failed: $err"
377 );
378 0 return;
379 }
380 }
381  
382 # Periodic yield prevents stack blowup when pre-queued messages
383 # resolve await synchronously.
384 0 ▶ await Future::IO->sleep(0) if ++$iter % MAX_SYNC_DEPTH == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ++$iter % 32 == 0 0 0 0
385 }
386 91 }
387  
388 # Start the driver if not already running. Idempotent.
389 # Only starts when _on_message is set (callback mode). Iterator mode
390 # consumers call next() directly — no driver loop needed.
391 #
392 # Ownership: the driver Future is added to the client's Future::Selector
393 # ($redis->{_tasks}) and stored in $self->{_driver_step}. The selector
394 # owns the task; the slot is the dedup signal. on_ready clears the slot
395 # regardless of outcome. No ->retain.
396 4 ▶sub _start_driver {
Subroutine _start_driver: called CC=8 SCAR=36.6
397 4 my ($self, $force) = @_;
398 4 ▶ return if $self->{_driver_step} && !$self->{_driver_step}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_driver_step"} and not $self->{"_driver_step"}->is_ready 0 4 4
Condition33%1/3
$self->{"_driver_step"} and not $self->{"_driver_step"}->is_ready
!ll&&!rl&&r
400
MC/DC0%0/2
$self->{"_driver_step"} and not $self->{"_driver_step"}->is_ready
$self->{"_driver_step"} not $self->{"_driver_step"}->is_ready
Truth table33%1/3
A and not(B)
A= $self->{"_driver_step"}B= not $self->{"_driver_step"}->is_ready
ABresult
0X0
100
111
399 4 ▶ return unless $self->{_on_message}; # only callback mode needs a driver
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_on_message"} 4 0 4
400 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
401 0 ▶ return if $self->{_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_paused"} 0 0 0
402 0 ▶ return unless $self->channel_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->channel_count > 0 0 0 0
403  
404 0 ▶ my $redis = $self->{redis} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $redis = $self->{"redis"} or return 0 0 0
405  
406 0 my $f = $self->_run_driver;
407 0 $self->{_driver_step} = $f;
408 0 $redis->{_tasks}->add(data => 'subscription-driver', f => $f);
409 0 ▶ $f->on_ready(sub { $self->{_driver_step} = undef });
Subroutine __ANON__: not called CC=1 SCAR=6.9
410 0 return;
411 }
412  
413 # Backward-compatible wrapper
414 0 ▶async sub next_message {
Subroutine next_message: not called CC=2 SCAR=17.9
Pod documented
415 0 my ($self) = @_;
416 0 my $msg = await $self->next();
417 0 ▶ return undef unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 0 0
418  
419 # Convert new format to old format for compatibility
420 return {
421 channel => $msg->{channel},
422 message => $msg->{data},
423 pattern => $msg->{pattern},
424 type => $msg->{type},
425 0 };
426 91 }
427  
428 # Intentional teardown: marks the subscription closed and wakes any
429 # blocked next() with undef. Clears the parent _subscription slot
430 # with an identity guard so a stale _close cannot evict a newer
431 # subscription object that reused the same slot.
432 0 ▶sub _close {
Subroutine _close: not called CC=11 SCAR=48.8
433 0 my ($self) = @_;
434 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
435 0 $self->{_closed} = 1;
436  
437 0 $self->{_pending_messages} = [];
438  
439 0 ▶ if (my $w = delete $self->{_message_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_message_waiter"}) 0 0 0
440 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
441 }
442 0 ▶ if (my $w = delete $self->{_slot_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_slot_waiter"}) 0 0 0
443 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
444 }
445  
446 # Identity-guarded parent-slot clear.
447 0 my $redis = $self->{redis};
448 0 ▶ if ($redis && defined $redis->{_subscription}
Branch: 1 branch
BranchTrueFalseTotal
if ($redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)) 0 0 0
Condition0%0/6
$redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
!ll&&!rl&&r
000
$redis and defined $redis->{"_subscription"}
!ll&&!rl&&r
000
MC/DC0%0/3
$redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
$redis defined $redis->{"_subscription"} builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
Truth table0%0/4
A and B and C
A= $redisB= defined $redis->{"_subscription"}C= builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
ABCresult
0XX0
10X0
1100
1111
449 && refaddr($redis->{_subscription}) == refaddr($self)) {
450 0 delete $redis->{_subscription};
451 }
452  
453 # Cancel any running driver Future. The driver's await on _dequeue
454 # also unwinds because we resolved _message_waiter above, so this is
455 # belt-and-suspenders; either path exits the driver cleanly.
456 0 ▶ if (my $f = delete $self->{_driver_step}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_driver_step"}) 0 0 0
457 0 ▶ $f->cancel unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
458 }
459 }
460  
461 # Unrecoverable failure: marks the subscription closed with a typed
462 # error. Any blocked next() will die with that error. The error is
463 # preserved for callers who call next() after the fact.
464 # In callback mode, fires on_error if registered; dies otherwise.
465 0 ▶sub _fail_fatal {
Subroutine _fail_fatal: not called CC=15 SCAR=54.8
466 0 my ($self, $typed_error) = @_;
467 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
468 0 $self->{_closed} = 1;
469 0 $self->{_fatal_error} = $typed_error;
470  
471 0 $self->{_pending_messages} = [];
472  
473 0 ▶ if (my $w = delete $self->{_message_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_message_waiter"}) 0 0 0
474 0 ▶ $w->fail($typed_error) unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
475 }
476 0 ▶ if (my $w = delete $self->{_slot_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_slot_waiter"}) 0 0 0
477 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
478 }
479  
480 # Identity-guarded parent-slot clear.
481 0 my $redis = $self->{redis};
482 0 ▶ if ($redis && defined $redis->{_subscription}
Branch: 1 branch
BranchTrueFalseTotal
if ($redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)) 0 0 0
Condition0%0/6
$redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
!ll&&!rl&&r
000
$redis and defined $redis->{"_subscription"}
!ll&&!rl&&r
000
MC/DC0%0/3
$redis and defined $redis->{"_subscription"} and builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
$redis defined $redis->{"_subscription"} builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
Truth table0%0/4
A and B and C
A= $redisB= defined $redis->{"_subscription"}C= builtin::refaddr($redis->{"_subscription"}) == builtin::refaddr($self)
ABCresult
0XX0
10X0
1100
1111
483 && refaddr($redis->{_subscription}) == refaddr($self)) {
484 0 delete $redis->{_subscription};
485 }
486  
487 # Cancel any running driver Future. _message_waiter was failed with
488 # the typed error above, so driver's _dequeue also dies with the
489 # typed error; cancel is belt-and-suspenders.
490 0 ▶ if (my $f = delete $self->{_driver_step}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_driver_step"}) 0 0 0
491 0 ▶ $f->cancel unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
492 }
493  
494 # Notify callback-mode consumers of the fatal error. In iterator mode
495 # the caller detects it via die from next(). Loud-by-default: if
496 # no on_error is registered in callback mode, die so silent death
497 # of a listener is impossible.
498 0 ▶ if (my $cb = $self->{_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"_on_error"}) 0 0 0
499 0 local $@;
500 0 my $ok = eval { $cb->($self, $typed_error); 1 };
501 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
502 0 ▶ Carp::carp("on_error callback died: " . ($@ // 'unknown error'));
Condition0%0/2
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
503 }
504 0 return;
505 }
506 # In iterator mode (no callback), callers discover the error via next().
507 # In callback mode with no on_error, die loudly.
508 0 ▶ if ($self->{_on_message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_message"}) 0 0 0
509 0 die $typed_error;
510 }
511 }
512  
513 # Called before a reconnect attempt. Does NOT mark the subscription
514 # closed — the reader has already exited (connection dropped). Channels
515 # and patterns remain in their tracking hashes for replay via
516 # _resume_after_reconnect.
517 #
518 # Fixes a latent "two drivers after reconnect" bug from the closure-based
519 # driver era: clearing the driver slot without cancelling left the old
520 # driver suspended on _dequeue. After _resume_after_reconnect started a
521 # new driver, both raced. The fix: cancel the Future explicitly, and
522 # wake _dequeue via the _paused flag so its await exits cleanly.
523 0 ▶sub _pause_for_reconnect {
Subroutine _pause_for_reconnect: not called CC=5 SCAR=34.0
524 0 my ($self) = @_;
525 0 $self->{_paused} = 1;
526  
527 # Wake any suspended _dequeue so the driver's while-loop exits.
528 0 ▶ if (my $w = delete $self->{_message_waiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"_message_waiter"}) 0 0 0
529 0 ▶ $w->done unless $w->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->is_ready 0 0 0
530 }
531  
532 # Cancel the driver Future. F::AA's continuation stops; the Future
533 # becomes cancelled; the selector's on_ready fires and removes the
534 # item; our on_ready fires and clears _driver_step.
535 0 ▶ if (my $f = delete $self->{_driver_step}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_driver_step"}) 0 0 0
536 0 ▶ $f->cancel unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
537 }
538 0 return;
539 }
540  
541 # Replays all tracked subscriptions on a freshly reconnected socket.
542 # Sets in_pubsub=1 BEFORE sending SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE so
543 # racing message frames classify correctly (mirrors initial-subscribe timing).
544 0 ▶async sub _resume_after_reconnect {
Subroutine _resume_after_reconnect: not called CC=7 SCAR=40.3
545 0 my ($self) = @_;
546 0 ▶ my $redis = $self->{redis} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $redis = $self->{"redis"} or return 0 0 0
547  
548 # Clear the paused flag so _dequeue and _run_driver don't immediately
549 # exit when the new driver starts.
550 0 $self->{_paused} = 0;
551  
552 # Set in_pubsub before issuing any commands so the unified reader
553 # classifies incoming message frames correctly (mirrors subscribe timing).
554 0 $redis->{in_pubsub} = 1;
555  
556 0 my @channels = keys %{$self->{channels}};
557 0 my @patterns = keys %{$self->{patterns}};
558 0 my @sharded = keys %{$self->{sharded_channels}};
559  
560 # Route each replay command through the write gate and unified reader
561 # so confirmations are matched via the inflight queue.
562 0 for my $ch (@channels) { await $redis->_pubsub_command('SUBSCRIBE', $ch) }
563 0 for my $p (@patterns) { await $redis->_pubsub_command('PSUBSCRIBE', $p) }
564 0 for my $ch (@sharded) { await $redis->_pubsub_command('SSUBSCRIBE', $ch) }
565  
566 0 ▶ if (my $cb = $self->{_on_reconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"_on_reconnect"}) 0 0 0
567 0 $cb->($self);
568 }
569  
570 # Restart the driver in whichever mode the subscription is in.
571 0 ▶ $self->_start_driver($self->{_on_message} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_on_message'} ? : 0 0 0
572 91 }
573  
574 # Unsubscribe from specific channels
575 0 ▶async sub unsubscribe {
Subroutine unsubscribe: not called CC=5 SCAR=34.0
Pod documented
576 0 my ($self, @channels) = @_;
577  
578 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
579  
580 0 my $redis = $self->{redis};
581  
582 # Resolve the list of channels to unsubscribe: explicit list or all.
583 0 ▶ my @to_remove = @channels ? @channels : $self->channels;
Branch: 1 branch
BranchTrueFalseTotal
@channels ? : 0 0 0
584  
585 # Issue one UNSUBSCRIBE per channel through the write gate and unified
586 # reader so each confirmation is properly matched to an inflight entry.
587 0 for my $ch (@to_remove) {
588 0 await $redis->_pubsub_command('UNSUBSCRIBE', $ch);
589 0 $self->_remove_channel($ch);
590 }
591  
592 # If no subscriptions remain, close and exit pubsub mode
593 0 ▶ if ($self->channel_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel_count == 0) 0 0 0
594 0 $self->_close;
595 }
596  
597 0 return $self;
598 91 }
599  
600 # Unsubscribe from patterns
601 0 ▶async sub punsubscribe {
Subroutine punsubscribe: not called CC=5 SCAR=34.0
Pod documented
602 0 my ($self, @patterns) = @_;
603  
604 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
605  
606 0 my $redis = $self->{redis};
607  
608 0 ▶ my @to_remove = @patterns ? @patterns : $self->patterns;
Branch: 1 branch
BranchTrueFalseTotal
@patterns ? : 0 0 0
609  
610 0 for my $p (@to_remove) {
611 0 await $redis->_pubsub_command('PUNSUBSCRIBE', $p);
612 0 $self->_remove_pattern($p);
613 }
614  
615 0 ▶ if ($self->channel_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel_count == 0) 0 0 0
616 0 $self->_close;
617 }
618  
619 0 return $self;
620 91 }
621  
622 # Unsubscribe from sharded channels
623 0 ▶async sub sunsubscribe {
Subroutine sunsubscribe: not called CC=5 SCAR=34.0
Pod documented
624 0 my ($self, @channels) = @_;
625  
626 0 ▶ return if $self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"} 0 0 0
627  
628 0 my $redis = $self->{redis};
629  
630 0 ▶ my @to_remove = @channels ? @channels : $self->sharded_channels;
Branch: 1 branch
BranchTrueFalseTotal
@channels ? : 0 0 0
631  
632 0 for my $ch (@to_remove) {
633 0 await $redis->_pubsub_command('SUNSUBSCRIBE', $ch);
634 0 $self->_remove_sharded_channel($ch);
635 }
636  
637 0 ▶ if ($self->channel_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channel_count == 0) 0 0 0
638 0 $self->_close;
639 }
640  
641 0 return $self;
642 91 }
643  
644  
645 0 ▶sub is_closed { shift->{_closed} }
Subroutine is_closed: not called CC=1 SCAR=6.9
Pod documented
646  
647 # Get all subscriptions for reconnect replay
648 2 ▶sub get_replay_commands {
Subroutine get_replay_commands: called CC=4 SCAR=14.0
Pod undocumented
649 2 my ($self) = @_;
650  
651 2 my @commands;
652  
653 2 my @channels = $self->channels;
654 2 ▶ push @commands, ['SUBSCRIBE', @channels] if @channels;
Branch: 1 branch
BranchTrueFalseTotal
if @channels 2 0 2
655  
656 2 my @patterns = $self->patterns;
657 2 ▶ push @commands, ['PSUBSCRIBE', @patterns] if @patterns;
Branch: 1 branch
BranchTrueFalseTotal
if @patterns 1 1 2
658  
659 2 my @sharded = $self->sharded_channels;
660 2 ▶ push @commands, ['SSUBSCRIBE', @sharded] if @sharded;
Branch: 1 branch
BranchTrueFalseTotal
if @sharded 0 2 2
661  
662 2 return @commands;
663 }
664  
665 91 1;
666  
667 __END__
« blib/lib/Async/Redis.pm ↑ Summary blib/lib/Async/Redis/Iterator.pm »