| 1 | # $Id$ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Mvalve::Reader; | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mvalve; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mvalve::Const; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mvalve::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | extends 'Mvalve::Base'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | has 'timeout' => ( | |||||||||||||||||||||||||||
| 12 | is => 'rw', | ||||||||||||||||||||||||||||
| 13 | isa => 'Num', | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | default => 60 | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | has 'throttler' => ( | |||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | does => 'Mvalve::Throttler', | ||||||||||||||||||||||||||||
| 21 | required => 1, | ||||||||||||||||||||||||||||
| 22 | coerce => 1, | ||||||||||||||||||||||||||||
| 23 | handles => [ qw(try_push fill) ], | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | has 'drop_on_emergency' => ( | |||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||||||||||
| 30 | default => 1, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub next | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
next: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $qs = $self->queue_set; | |||||||||||||||||||||||||||
| 42 | 0 | my @names = $qs->as_q4m_args; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | Mvalve::trace("queue_wait with @names") if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | my $table = $self->q_next( | |||||||||||||||||||||||||||
| 46 | table_conds => \@names, | ||||||||||||||||||||||||||||
| 47 | timeout => $self->timeout + 0 | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (! $table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | Mvalve::trace( "q_next did not return a table name, simply returning" ) if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | return (); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | Mvalve::trace( "issueing fetch on table '$table'") if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $message = $self->q_fetch(table => $table); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (! $message) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | Mvalve::trace( "q_fetch did not return a message, simply returning" ) if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | return (); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $destination = $message->header( &Mvalve::Const::DESTINATION_HEADER ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (&Mvalve::Const::MVALVE_TRACE && $qs->is_timed( $table )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&Mvalve::Const::MVALVE_TRACE and $qs->is_timed($table)
MC/DC0%0/2
&Mvalve::Const::MVALVE_TRACE and $qs->is_timed($table)
&Mvalve::Const::MVALVE_TRACE
$qs->is_timed($table)
Truth table0%0/3
A and B
A= &Mvalve::Const::MVALVE_TRACEB= $qs->is_timed($table)
| |||||||||||||||||||||||||||||
| 66 | 0 | Mvalve::trace( "we should have dispatched at " . | |||||||||||||||||||||||||||
| 67 | scalar( localtime( $message->header( &Mvalve::Const::RETRY_HEADER ) /100_000 ) ) ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # destination is an abstract symbol representing the endpoint | ||||||||||||||||||||||||||||
| 71 | # service name. this /could/ be used by the queue consumer, but it | ||||||||||||||||||||||||||||
| 72 | # is *not* a | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $emerg_key = [ $destination, 'emergency' ]; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( $qs->is_emergency( $table ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($self->drop_on_emergency) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $duration = $message->header(&Mvalve::Const::DURATION_HEADER) | ||||||||||||||||||||||||||
|
Condition0%0/3
$message->header(&Mvalve::Const::DURATION_HEADER) || $self->throttler->interval
MC/DC0%0/2
$message->header(&Mvalve::Const::DURATION_HEADER) || $self->throttler->interval
$message->header(&Mvalve::Const::DURATION_HEADER)
$self->throttler->interval
Truth table0%0/3
A || B
A= $message->header(&Mvalve::Const::DURATION_HEADER)B= $self->throttler->interval
| |||||||||||||||||||||||||||||
| 78 | || $self->throttler->interval; | ||||||||||||||||||||||||||||
| 79 | 0 | $self->state_set($emerg_key, time + $duration); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 0 | $self->fill( key => $destination ) | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | goto RETURN_MESSAGE; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $avail_time = $self->state_get($emerg_key); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($avail_time && time < $avail_time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$avail_time and time < $avail_time
MC/DC0%0/2
$avail_time and time < $avail_time
$avail_time
time < $avail_time
Truth table0%0/3
A and B
A= $avail_timeB= time < $avail_time
| |||||||||||||||||||||||||||||
| 90 | 0 | return (); # drop message! | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $qs->is_timed( $table ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | # if this is from an emergency queue or a timed queue, we go ahead | ||||||||||||||||||||||||||||
| 95 | # and allow the message, but we also update the throttler's count | ||||||||||||||||||||||||||||
| 96 | # so the next message from a normal queue would be throttled correctly | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($message->header(&Mvalve::Const::RETRY_HEADER)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | $self->state_decr( [ $destination, 'retry' ] ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | $self->fill( key => $destination ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # XXX - This is bad practive, but oh well | ||||||||||||||||||||||||||||
| 103 | 0 | goto RETURN_MESSAGE; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # otherwise, we need to check if this message is going to be throttled | ||||||||||||||||||||||||||||
| 107 | 0 | my $is_pending = $self->is_pending( $destination ); | |||||||||||||||||||||||||||
| 108 | 0 | my $is_throttled = ! $self->try_push( key => $destination ); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | Mvalve::trace( "checking if message to $destination should be throttled (pending: $is_pending, throttled: $is_throttled)" ) if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($is_throttled || $is_pending) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_throttled or $is_pending
MC/DC0%0/2
$is_throttled or $is_pending
$is_throttled
$is_pending
Truth table0%0/3
A or B
A= $is_throttledB= $is_pending
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | Mvalve::trace( "message", $message->id, "is being throttled") if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $self->defer( | |||||||||||||||||||||||||||
| 114 | message => $message, | ||||||||||||||||||||||||||||
| 115 | interval => $self->throttler->interval, | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | 0 | $self->log( | |||||||||||||||||||||||||||
| 118 | action => "throttle", | ||||||||||||||||||||||||||||
| 119 | destination => $destination | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | 0 | return (); # no data for you! | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | RETURN_MESSAGE: | ||||||||||||||||||||||||||||
| 125 | # if we got here, we can just return the data | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | Mvalve::trace( "message", $message->id, "being returned") if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | $self->log( | |||||||||||||||||||||||||||
| 128 | action => "dequeue", | ||||||||||||||||||||||||||||
| 129 | destination => $destination, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | 0 | return $message; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub is_pending { | ||||||||||||||||||||||||||
|
Subroutine
is_pending: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my( $self, $destination ) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $retry_key = [ $destination, 'retry' ]; | |||||||||||||||||||||||||||
| 138 | 0 | my $count = $self->state_get($retry_key); | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | return $count ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||