File Coverage

blib/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   9 use strict;
  2         5  
  2         62  
4 2     2   9 use warnings;
  2         4  
  2         50  
5 2     2   1635 use List::Util::WeightedRoundRobin;
  2         1046  
  2         59  
6 2     2   12 use Carp;
  2         4  
  2         142  
7              
8 2     2   13 use base 'Geo::Coder::Many::Scheduler::UniquenessScheduler';
  2         5  
  2         1246  
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 193 my $class = shift;
37 140         357 my $ra_geocoders = shift;
38            
39 140         713 my $WeightedList = List::Util::WeightedRoundRobin->new();
40 140         2479 my $self = $class->SUPER::new({items => $ra_geocoders});
41 140         551 $self->{weighted_list}
42             = $WeightedList->create_weighted_list( $ra_geocoders );
43              
44 140 50       6763 unless( @{$self->{weighted_list}} ) {
  140         430  
45 0         0 carp "Unable to create weighted list from list of geocoders";
46             };
47              
48 140         473 bless $self, $class;
49 140         703 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 1370     1370   1539 my $self = shift;
66 1370         1375 my $next = shift @{$self->{weighted_list}};
  1370         3108  
67 1370         1822 push @{$self->{weighted_list}}, $next;
  1370         2868  
68 1370         4247 return $next;
69             };
70              
71             1;