| 1 | package AnyEvent::Retry::Interval::Multi; | ||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | $AnyEvent::Retry::Interval::Multi::VERSION = '0.03'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | # ABSTRACT: combine multiple interval objects into one interval | ||||||||||||||
| 6 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use AnyEvent::Retry::Types qw(Interval); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use true; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | with 'AnyEvent::Retry::Interval'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | has [qw/first then/] => ( | |||||||||||||
| 15 | is => 'ro', | ||||||||||||||
| 16 | isa => Interval, | ||||||||||||||
| 17 | required => 1, | ||||||||||||||
| 18 | coerce => 1, | ||||||||||||||
| 19 | ); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | has '_source' => ( | |||||||||||||
| 22 | accessor => '_source', | ||||||||||||||
| 23 | isa => 'Str', | ||||||||||||||
| 24 | lazy => 1, | ||||||||||||||
| 25 | default => 'first', | ||||||||||||||
| 26 | clearer => '_reset_source', | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 1 | has 'after' => ( | |||||||||||||
| 30 | is => 'ro', | ||||||||||||||
| 31 | isa => 'Num|CodeRef', | ||||||||||||||
| 32 | required => 1, | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 12 | ▶ | sub reset { | ||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 12 | my $self = shift; | |||||||||||||
| 37 | 12 | $self->_reset_source; | |||||||||||||
| 38 | 12 | $self->first->reset; | |||||||||||||
| 39 | 12 | $self->then->reset; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 224 | ▶ | sub next { | ||||||||||||
|
Subroutine
next: called CC=3 SCAR=11.0
| |||||||||||||||
| 43 | 224 | my ($self, $i) = @_; | |||||||||||||
| 44 | 224 | my $source = $self->_source; | |||||||||||||
| 45 | 224 | ▶ | return $self->then->next if $self->_source eq 'then'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 68 | my $next = $self->first->next; | |||||||||||||
| 48 | 68 | my $switch = $self->check_condition($i, $next); | |||||||||||||
| 49 | 68 | ▶ | return $next if !$switch; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 12 | $self->_source('then'); | |||||||||||||
| 52 | 12 | return $self->then->next; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 68 | ▶ | sub check_condition { | ||||||||||||
|
Subroutine
check_condition: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||
| 56 | 68 | my ($self, $i, $next) = @_; | |||||||||||||
| 57 | 68 | my $cond = $self->after; | |||||||||||||
| 58 | 68 | my $switch = 0; | |||||||||||||
| 59 | 68 | ▶ | if(ref $cond){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 60 | 0 | ▶ | $switch = $cond->($i, $next) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | } | ||||||||||||||
| 62 | elsif($cond < 0){ | ||||||||||||||
| 63 | 40 | ▶ | $switch = 1 if $i > abs($cond); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | } | ||||||||||||||
| 65 | else { | ||||||||||||||
| 66 | 28 | ▶ | $switch = 1 if $next > $cond; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 68 | return $switch; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | |||||||||||||||
| 76 | =pod | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =head1 NAME | ||||||||||||||
| 79 | |||||||||||||||
| 80 | AnyEvent::Retry::Interval::Multi - combine multiple interval objects into one interval | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =head1 VERSION | ||||||||||||||
| 83 | |||||||||||||||
| 84 | version 0.03 | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||
| 87 | |||||||||||||||
| 88 | my $m = AnyEvent::Retry::Interval::Multi->new( | ||||||||||||||
| 89 | first => { Constant => { interval => .5 } }, | ||||||||||||||
| 90 | after => -2, | ||||||||||||||
| 91 | then => { Multi => { | ||||||||||||||
| 92 | first => 'Fibonacci', | ||||||||||||||
| 93 | after => 10, | ||||||||||||||
| 94 | then => { Multi => { | ||||||||||||||
| 95 | first => { Constant => { interval => 10 } }, | ||||||||||||||
| 96 | after => -6, | ||||||||||||||
| 97 | then => { Constant => { interval => 60 } }, | ||||||||||||||
| 98 | }}, | ||||||||||||||
| 99 | }}, | ||||||||||||||
| 100 | ); | ||||||||||||||
| 101 | |||||||||||||||
| 102 | C<$m> waits for .5 seconds twice, then it waits for 1 second, 1 | ||||||||||||||
| 103 | second, 2 seconds, 3 seconds, 5 seconds, 8 seconds, then 10 seconds 6 | ||||||||||||||
| 104 | times, then 60 seconds forever. | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||
| 107 | |||||||||||||||
| 108 | See the code and tests for more detail. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 115 | |||||||||||||||
| 116 | This software is copyright (c) 2010 by Jonathan Rockway. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 119 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =cut | ||||||||||||||
| 122 | |||||||||||||||
| 123 | |||||||||||||||
| 124 | __END__ | ||||||||||||||