File Coverage

blib/lib/Proc/BackOff/Linear.pm
Criterion Covered Total %
statement 24 32 75.0
branch 8 20 40.0
condition 4 12 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 43 71 60.5


line stmt bran cond sub pod time code
1             package Proc::BackOff::Linear;
2              
3             # Inheritance
4 2     2   33095 use base qw( Proc::BackOff );
  2         4  
  2         755  
5              
6             # Set up get/set fields
7             __PACKAGE__->mk_accessors( 'slope',
8             'x',
9             'b',
10             );
11              
12             # standard pragmas
13 2     2   13 use warnings;
  2         12  
  2         59  
14 2     2   9 use strict;
  2         10  
  2         773  
15              
16             # standard perl modules
17              
18             # CPAN & others
19              
20             our $VERSION = '0.02';
21              
22             =head1 NAME
23              
24             Proc::BackOff::Linear
25              
26             =head1 SYNOPSIS
27              
28             Usage:
29              
30             use Proc::BackOff::Linear;
31              
32             my $obj = Proc::BackOff::Linear->new( { slope => 5, x => 'count', b => 0 );
33             # On N'th failure delay would be set to:
34             # y = slope * x + b;
35             # 1st failure : 5 * count + b = 5 * 1 + 0 = 5
36             # 2nd failure : 5 * 2 + 0 = 10
37             # 3rd failure : 5 * 3 + 0 = 15
38             # 4th failure : 5 * 4 + 0 = 20
39              
40             See L for further documentation.
41              
42             =head1 Overloaded Methods
43              
44             =head2 new()
45              
46             Check for variables being set:
47              
48             Required: slope
49             b defaults to 0
50             x defaults to 'count'
51              
52             =cut
53              
54             sub new {
55 2     2 1 469 my $proto = shift;
56 2   33     14 my $class = ref $proto || $proto;
57 2         17 my $obj = $class->SUPER::new(@_);
58              
59 2 50 33     8 if ( ! defined $obj->slope() || ! $obj->valid_number_check($obj->slope())) {
60 0         0 warn "$proto: Slope value not set";
61 0         0 return undef;
62             }
63              
64 2 50 33     8 if ( ! defined $obj->b() || ! $obj->valid_number_check($obj->b())) {
65 0         0 warn "$proto: b value not set";
66 0 0       0 $obj->b(0) unless defined $obj->b();
67 0 0       0 return undef unless $obj->valid_number_check($obj->b());
68             }
69              
70 2 50 33     8 if ( ! defined $obj->x() || ! $obj->valid_number_check($obj->x())) {
71 0         0 warn "$proto: x value not set";
72 0 0       0 $obj->x(0) unless defined $obj->x();
73 0 0       0 return undef unless $obj->valid_number_check($obj->x());
74             }
75              
76 2         8 return $obj;
77             }
78              
79             =head2 calculate_back_off()
80              
81             Returns the new back off value.
82              
83             =cut
84              
85             sub calculate_back_off {
86 20     20 1 24 my $self = shift;
87              
88             # this is a linear back off
89             # y = slope * x + b;
90             # b = 0
91             # y = slope * x ;
92             # x = failure_count
93             # y = timeout
94             # slope = add_timeout
95             # timeout = add_timeout * failure_count
96              
97 20         41 my $slope = $self->slope();
98 20         158 my $x = $self->x();
99 20         144 my $b = $self->b();
100              
101 20 100       176 $slope = $self->failure_count() if $slope eq 'count';
102 20 50       120 $x = $self->failure_count() if $x eq 'count';
103 20 100       155 $b = $self->failure_count() if $b eq 'count';
104              
105 20         98 return $slope * $x + $b;
106             }
107              
108             1;
109              
110             =head1 Changes
111              
112             0.02 2007-08-12 -- Daniel Lo
113             - Documentation fixes. No code changes.
114              
115             0.01 2007-04-17 -- Daniel Lo
116             - Initial Version
117              
118             =head1 AUTHOR
119              
120             Daniel Lo
121              
122             =head1 LICENSE
123              
124             Copyright (C) PictureTrail Inc. 1999-2007
125             Santa Clara, California, United States of America.
126              
127             This code is released to the public for public use under Perl's Artisitic
128             licence.
129              
130             =cut