blib/lib/Async/Redis/AutoPipeline.pm

filter: Statementstmt 32.9% 28 / 85 Branchbran 0.0% 0 / 14 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 9 / 18 Podpod 0.0% 0 / 2 totaltotal 31.2% 39 / 125 CC 13 SCAR 20.9
CC
13
Cov
29%
_do_flush
20.0
__ANON__
20.0
__ANON__
12.0
CRAP 8.1
« blib/lib/Async/Redis/URI.pm ↑ Summary blib/lib/Async/Redis/Script.pm »
1 package Async::Redis::AutoPipeline;
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 Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 91 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 91 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 91 ▶use Async::Redis::Error::Disconnected;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
13 1 my ($class, %args) = @_;
14  
15 return bless {
16 redis => $args{redis},
17 1 ▶ max_depth => $args{max_depth} // 1000,
Condition50%1/2
$args{"max_depth"} // 1000
l!l
10
MC/DC0%0/1
$args{"max_depth"} // 1000
$args{"max_depth"}
Truth table50%1/2
A
A= $args{"max_depth"}
Aresult
11
00
18 _queue => [],
19 _flush_pending => 0,
20 _flushing => 0,
21 }, $class;
22 }
23  
24 0 ▶sub command {
Subroutine command: not called CC=2 SCAR=17.9
Pod undocumented
25 0 my ($self, @args) = @_;
26  
27 0 my $future = Future->new;
28 0 push @{$self->{_queue}}, { cmd => \@args, future => $future };
29  
30 # Schedule flush exactly once per batch
31 0 ▶ unless ($self->{_flush_pending}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_flush_pending"}) 0 0 0
32 0 $self->{_flush_pending} = 1;
33 0 $self->_schedule_flush;
34 }
35  
36 0 return $future;
37 }
38  
39 0 ▶sub _schedule_flush {
Subroutine _schedule_flush: not called CC=1 SCAR=6.9
40 0 my ($self) = @_;
41  
42 # Use event loop's "next tick" mechanism
43 # sleep(0) yields to event loop then immediately returns
44 0 ▶ Future::IO->sleep(0)->on_done(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 $self->_do_flush;
46 0 });
47 }
48  
49 0 ▶sub _do_flush {
Subroutine _do_flush: not called CC=4 SCAR=30.0
50 0 my ($self) = @_;
51  
52 # Reentrancy guard
53 0 ▶ return if $self->{_flushing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_flushing"} 0 0 0
54 0 $self->{_flushing} = 1;
55  
56 # Reset pending flag before flush (allows new commands to queue)
57 0 $self->{_flush_pending} = 0;
58  
59 # Take current queue atomically
60 0 my @batch = splice @{$self->{_queue}};
61  
62 0 ▶ if (@batch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@batch) 0 0 0
63 # Respect depth limit
64 0 my $max = $self->{max_depth};
65 0 ▶ if (@batch > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (@batch > $max) 0 0 0
66 # Put excess back, schedule another flush
67 0 unshift @{$self->{_queue}}, splice(@batch, $max);
68 0 $self->{_flush_pending} = 1;
69 0 $self->_schedule_flush;
70 }
71  
72 0 $self->_send_batch(\@batch);
73 }
74  
75 0 $self->{_flushing} = 0;
76 }
77  
78 # Detach and return all queued-but-not-yet-flushed commands. Caller is
79 # responsible for failing their futures. Called by Async::Redis::_reader_fatal
80 # when the connection dies before a scheduled flush.
81 1 ▶sub _detach_queued {
Subroutine _detach_queued: called CC=2 SCAR=7.0
82 1 my ($self) = @_;
83 1 ▶ my $queued = $self->{_queue} // [];
Condition50%1/2
$self->{"_queue"} // []
l!l
10
MC/DC0%0/1
$self->{"_queue"} // []
$self->{"_queue"}
Truth table50%1/2
A
A= $self->{"_queue"}
Aresult
11
00
84 1 $self->{_queue} = [];
85 1 $self->{_flush_pending} = 0;
86 1 return $queued;
87 }
88  
89 0 ▶sub _send_batch {
Subroutine _send_batch: not called CC=1 SCAR=6.9
90 0 my ($self, $batch) = @_;
91 0 my $redis = $self->{redis};
92  
93 0 my @commands = map { $_->{cmd} } @$batch;
94 0 my @futures = map { $_->{future} } @$batch;
95  
96 0 ▶ my $submit = (async sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
97 0 my $buffer = '';
98 0 my @deadlines;
99 0 for my $cmd (@commands) {
100 0 $buffer .= $redis->_build_command(@$cmd);
101 0 push @deadlines, $redis->_calculate_deadline(@$cmd);
102 }
103  
104 0 ▶ await $redis->_with_write_gate(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 ▶ return (async sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
106 0 ▶ if (!$redis->{_socket_live}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($redis->{"_socket_live"}) 0 0 0
107 0 ▶ if ($redis->_reconnect_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redis->_reconnect_enabled) { } 0 0 0
108 0 await $redis->_ensure_connected;
109 } else {
110 0 die Async::Redis::Error::Disconnected->new(
111 message => "Not connected",
112 );
113 }
114 }
115 0 for my $i (0 .. $#commands) {
116 $redis->_add_inflight(
117 $futures[$i],
118 $commands[$i][0],
119 0 [ @{$commands[$i]}[1..$#{$commands[$i]}] ],
120 $deadlines[$i],
121 'fail',
122 );
123 }
124 0 await $redis->_send($buffer);
125 0 })->();
126 0 });
127  
128 0 $redis->_ensure_reader;
129 0 })->();
130  
131 # Transport failure on submit cascades to every future that wasn't
132 # already failed by _reader_fatal.
133 0 ▶ $submit->on_fail(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
134 0 my ($err) = @_;
135 0 for my $f (@futures) {
136 0 ▶ $f->fail($err) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
137 }
138 0 });
139  
140 # Ownership: the client's Future::Selector (_tasks) owns this submit
141 # task. Any caller currently awaiting inside run_until_ready sees a
142 # submit failure propagated via the selector. No ->retain needed.
143 0 $redis->{_tasks}->add(data => 'autopipe-submit', f => $submit);
144 }
145  
146 91 1;
147  
148 __END__
« blib/lib/Async/Redis/URI.pm ↑ Summary blib/lib/Async/Redis/Script.pm »