| 1 | package Bot::Backbone::SendPolicy::MinimumInterval; | ||||||||||||||||||||||||||||
| 2 | 1 | $Bot::Backbone::SendPolicy::MinimumInterval::VERSION = '0.161950'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | with 'Bot::Backbone::SendPolicy'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Prevent any message from being delivered too soon | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | has interval => ( | |||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => 'Num', | ||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | has queue_length => ( | |||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => 'Int', | ||||||||||||||||||||||||||||
| 23 | predicate => 'has_queue', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | has discard => ( | |||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||
| 31 | default => 0, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | has last_send_time => ( | |||||||||||||||||||||||||||
| 36 | is => 'rw', | ||||||||||||||||||||||||||||
| 37 | isa => 'Num', | ||||||||||||||||||||||||||||
| 38 | predicate => 'has_last_send_time', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 598 | ▶ | sub _too_soon { | ||||||||||||||||||||||||||
|
Subroutine
_too_soon: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 43 | 598 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 598 | my $now = AnyEvent->now; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 598 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless $self->has_last_send_time; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 596 | ▶ | return $self->last_send_time + $self->interval | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->last_send_time > $now or $now - $self->last_send_time < $self->interval
MC/DC100%2/2
$self->last_send_time > $now or $now - $self->last_send_time < $self->interval
$self->last_send_time > $now
$now - $self->last_send_time < $self->interval
Truth table100%3/3
A or B
A= $self->last_send_time > $nowB= $now - $self->last_send_time < $self->interval
| |||||||||||||||||||||||||||||
| 50 | if ($self->last_send_time > $now) | ||||||||||||||||||||||||||||
| 51 | or ($now - $self->last_send_time < $self->interval); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | return 0; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 598 | ▶ | sub allow_send { | ||||||||||||||||||||||||||
|
Subroutine
allow_send: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 598 | my ($self, $options) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 598 | my %send = ( allow => 1 ); | |||||||||||||||||||||||||||
| 60 | 598 | my $now = AnyEvent->now; | |||||||||||||||||||||||||||
| 61 | 598 | my $too_soon = $self->_too_soon; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 598 | my $save = 1; | |||||||||||||||||||||||||||
| 64 | 598 | ▶ | if ($too_soon) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Messages coming too fast should be thrown away | ||||||||||||||||||||||||||||
| 67 | 592 | ▶ | if ($self->discard) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 294 | $save = 0; | |||||||||||||||||||||||||||
| 69 | 294 | $send{allow} = 0; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Messages coming too fast should be postponed | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 298 | $send{after} = $too_soon - $now; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # If the number of messages queued is too long, nevermind... | ||||||||||||||||||||||||||||
| 77 | $send{allow} = 0 | ||||||||||||||||||||||||||||
| 78 | if $self->has_queue | ||||||||||||||||||||||||||||
| 79 | 298 | ▶ | and $send{after} / $self->interval > $self->queue_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->has_queue and $send{"after"} / $self->interval > $self->queue_length
MC/DC0%0/2
$self->has_queue and $send{"after"} / $self->interval > $self->queue_length
$self->has_queue
$send{"after"} / $self->interval > $self->queue_length
Truth table33%1/3
A and B
A= $self->has_queueB= $send{"after"} / $self->interval > $self->queue_length
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 598 | ▶ | $self->last_send_time($too_soon || $now) if $save; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$too_soon || $now
MC/DC0%0/2
$too_soon || $now
$too_soon
$now
Truth table66%2/3
A || B
A= $too_soonB= $now
| |||||||||||||||||||||||||||||
| 84 | 598 | return \%send; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||