| 1 | package Geo::Coder::Many::Scheduler::UniquenessScheduler::WRR; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use List::Util::WeightedRoundRobin; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use base 'Geo::Coder::Many::Scheduler::UniquenessScheduler'; | ||
| 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 | my $class = shift; | ||
| 37 | my $ra_geocoders = shift; | ||
| 38 | |||
| 39 | my $WeightedList = List::Util::WeightedRoundRobin->new(); | ||
| 40 | my $self = $class->SUPER::new({items => $ra_geocoders}); | ||
| 41 | $self->{weighted_list} | ||
| 42 | = $WeightedList->create_weighted_list( $ra_geocoders ); | ||
| 43 | |||
| 44 | unless( @{$self->{weighted_list}} ) { | ||
| 45 | carp "Unable to create weighted list from list of geocoders"; | ||
| 46 | }; | ||
| 47 | |||
| 48 | bless $self, $class; | ||
| 49 | 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 | my $self = shift; | ||
| 66 | my $next = shift @{$self->{weighted_list}}; | ||
| 67 | push @{$self->{weighted_list}}, $next; | ||
| 68 | return $next; | ||
| 69 | }; | ||
| 70 | |||
| 71 | 1; |