| 1 | package Net::Async::Redis::Multi; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | our $VERSION = '6.006'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Async::Redis::Multi - represents multiple operations in a single Redis transaction | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Instances are generated by L<Net::Async::Redis/multi>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 15 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 15 | ▶ | use Syntax::Keyword::Dynamically; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 15 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 15 | ▶ | use Scalar::Util qw(weaken blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 15 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | $args{queued_requests} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"queued_requests"} //= []
MC/DC0%0/1
$args{"queued_requests"} //= []
$args{"queued_requests"}
Truth table0%0/2
A
A= $args{"queued_requests"}
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | weaken($args{redis} // die 'Must be provided a Net::Async::Redis instance'); | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"redis"} // die("Must be provided a Net::Async::Redis instance")
MC/DC0%0/1
$args{"redis"} // die("Must be provided a Net::Async::Redis instance")
$args{"redis"}
Truth table0%0/2
A
A= $args{"redis"}
| |||||||||||||||||||||||||||||
| 28 | 0 | bless \%args, $class; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | async sub exec { | ||||||||||||||||||||||||||
|
Subroutine
exec: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | try { | ||||||||||||||||||||||||||||
| 35 | 0 | my $f = $self->$code; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | $f->retain if blessed($f) and $f->isa('Future'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
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")
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | $log->tracef('MULTI exec'); | |||||||||||||||||||||||||||
| 39 | 0 | dynamically $self->redis->{_is_multi} = $self->redis->{multi_queue}; | |||||||||||||||||||||||||||
| 40 | 0 | my ($exec_result) = await $self->redis->exec; | |||||||||||||||||||||||||||
| 41 | 0 | $self->redis->{multi_queue}->finish; | |||||||||||||||||||||||||||
| 42 | 0 | my @reply = $exec_result->@*; | |||||||||||||||||||||||||||
| 43 | 0 | my $success = 0; | |||||||||||||||||||||||||||
| 44 | 0 | my $failure = 0; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | while(@reply) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | try { | ||||||||||||||||||||||||||||
| 47 | 0 | my $reply = shift @reply; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $queued = shift @{$self->{queued_requests}} or die 'invalid queued request'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $queued->done($reply) unless $queued->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ++$success | |||||||||||||||||||||||||||
| 51 | 0 | } catch { | |||||||||||||||||||||||||||
| 52 | 0 | $log->warnf("Failure during transaction: %s", $@); | |||||||||||||||||||||||||||
| 53 | 0 | ++$failure | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | return $success, $failure; | |||||||||||||||||||||||||||
| 57 | 0 | } catch { | |||||||||||||||||||||||||||
| 58 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 59 | 0 | $log->errorf('Failed to complete multi - %s', $err); | |||||||||||||||||||||||||||
| 60 | 0 | for my $queued (splice @{$self->{queued_requests}}) { | |||||||||||||||||||||||||||
| 61 | try { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $queued->fail("Transaction failed", redis => 'transaction_failure') unless $queued->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | } catch { | |||||||||||||||||||||||||||
| 64 | 0 | $log->warnf("Failure during transaction: %s", $@); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 0 | die $@; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 15 | } | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 redis | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Accessor for the L<Net::Async::Redis> instance. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub redis { shift->{redis} } | ||||||||||||||||||||||||||
|
Subroutine
redis: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 15 | ▶ | use Sub::Util qw(set_subname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=22.6
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($method) = our $AUTOLOAD =~ m{::([^:]+)$}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # We only need to check this once | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | die "Unknown method $method" unless Net::Async::Redis::Commands->can($method) or Net::Async::Redis->can($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
"Net::Async::Redis::Commands"->can($method) or "Net::Async::Redis"->can($method)
MC/DC0%0/2
"Net::Async::Redis::Commands"->can($method) or "Net::Async::Redis"->can($method)
"Net::Async::Redis::Commands"->can($method)
"Net::Async::Redis"->can($method)
Truth table0%0/3
A or B
A= "Net::Async::Redis::Commands"->can($method)B= "Net::Async::Redis"->can($method)
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $code = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 89 | 0 | my $f = $self->redis->future->set_label($method); | |||||||||||||||||||||||||||
| 90 | 0 | push @{$self->{queued_requests}}, $f; | |||||||||||||||||||||||||||
| 91 | 0 | my $ff = do { | |||||||||||||||||||||||||||
| 92 | # $self->redis->{_is_multi} //= 0; | ||||||||||||||||||||||||||||
| 93 | 0 | dynamically $self->redis->{_is_multi} = $self->redis->{multi_queue}; | |||||||||||||||||||||||||||
| 94 | 0 | $self->redis->$method(@args); | |||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | 0 | my ($resp) = await $ff; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | return await $f if $resp eq 'QUEUED'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # my $addr = refaddr($f); | ||||||||||||||||||||||||||||
| 100 | # extract_by { $addr == refaddr($_) } @{$self->{queued_requests}}; | ||||||||||||||||||||||||||||
| 101 | 0 | $f->fail($resp); | |||||||||||||||||||||||||||
| 102 | 0 | die $resp; | |||||||||||||||||||||||||||
| 103 | 0 | }; | |||||||||||||||||||||||||||
| 104 | 0 | set_subname $method => $code; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | { no strict 'refs'; *$method = sub { $code->(@_)->retain } } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 106 | 0 | $code->(@_)->retain; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | return if ${^GLOBAL_PHASE} eq 'DESTRUCT' or not $self->{queued_requests}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
${^GLOBAL_PHASE} eq "DESTRUCT" or not $$self{"queued_requests"}
MC/DC0%0/2
${^GLOBAL_PHASE} eq "DESTRUCT" or not $$self{"queued_requests"}
${^GLOBAL_PHASE} eq "DESTRUCT"
not $$self{"queued_requests"}
Truth table0%0/3
A or not(B)
A= ${^GLOBAL_PHASE} eq "DESTRUCT"B= not $$self{"queued_requests"}
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | for my $queued (splice @{$self->{queued_requests}}) { | |||||||||||||||||||||||||||
| 114 | try { | ||||||||||||||||||||||||||||
| 115 | 0 | $queued->cancel; | |||||||||||||||||||||||||||
| 116 | 0 | } catch { | |||||||||||||||||||||||||||
| 117 | 0 | $log->warnf("Failure during cleanup: %s", $@); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 15 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||