File Coverage

blib/lib/Proc/BackOff/Random.pm
Criterion Covered Total %
statement 22 28 78.5
branch 5 10 50.0
condition 5 15 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 39 60 65.0


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