File Coverage

lib/Geo/Coder/Many/Scheduler/UniquenessScheduler/WRR.pm
Criterion Covered Total %
statement 30 31 96.7
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             package Geo::Coder::Many::Scheduler::UniquenessScheduler::WRR;
2              
3 2     2   7 use strict;
  2         3  
  2         41  
4 2     2   5 use warnings;
  2         2  
  2         33  
5 2     2   799 use List::Util::WeightedRoundRobin;
  2         729  
  2         41  
6 2     2   7 use Carp;
  2         2  
  2         91  
7              
8 2     2   6 use base 'Geo::Coder::Many::Scheduler::UniquenessScheduler';
  2         2  
  2         652  
9              
10             our $VERSION = '0.01';
11              
12             =head1 NAME
13              
14             Geo::Coder::Many::Scheduler::UniquenessScheduler::WRR - Weighted Round Robin
15             scheduler (default)
16              
17             =head1 DESCRIPTION
18              
19             Returns items based on the weighted round-robin scheduling algorithm. It
20             inherits from UniquenessScheduler because it doesn't provide get_next_unique
21             and reset_available by itself.
22              
23             =head1 METHODS
24              
25             =head2 new
26              
27             Constructs and returns a new WRR scheduler based on a weighted-list of items.
28             (Due to the way List::Util::WeightedRoundRobin is implemented, the items - in
29             this case the names of geocoders - are copied such that the list contains the
30             appropriate number of each item for its corresponding weight. Note that using
31             large, coprime weights may produce a large list...!)
32              
33             =cut
34              
35             sub new {
36 140     140 1 136 my $class = shift;
37 140         111 my $ra_geocoders = shift;
38            
39 140         312 my $WeightedList = List::Util::WeightedRoundRobin->new();
40 140         818 my $self = $class->SUPER::new({items => $ra_geocoders});
41             $self->{weighted_list}
42 140         351 = $WeightedList->create_weighted_list( $ra_geocoders );
43              
44 140 50       3952 unless( @{$self->{weighted_list}} ) {
  140         291  
45 0         0 carp "Unable to create weighted list from list of geocoders";
46             };
47              
48 140         168 bless $self, $class;
49 140         317 return $self;
50             }
51              
52             =head1 INTERNAL METHODS
53              
54             =head2 _get_next
55              
56             Returns the next most appropriate geocoder based on the weighted round robin
57             scoring.
58              
59             =cut
60              
61             ## no critic (ProhibitUnusedPrivateSubroutines)
62             # ( _get_next is actually 'protected' )
63              
64             sub _get_next {
65 1365     1365   1000 my $self = shift;
66 1365         977 my $next = shift @{$self->{weighted_list}};
  1365         1425  
67 1365         879 push @{$self->{weighted_list}}, $next;
  1365         1496  
68 1365         2007 return $next;
69             };
70              
71             1;