From: John Napiorkowski Date: Thu, 10 Jul 2008 19:58:28 +0000 (+0000) Subject: fixed regression in the random balancer that I created when I removed the shuffle... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bc376f59da602a1af4b43849ad1e302c30f85224;p=dbsrgits%2FDBIx-Class-Historic.git fixed regression in the random balancer that I created when I removed the shuffle dependency, changed the syntax for returning the next storage in the pool to make debugging easier --- diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm index 316653a..25c8009 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm @@ -136,8 +136,11 @@ around 'next_storage' => sub { } ## Get a replicant, or the master if none - my $next = $self->$next_storage(@args); - return $next ? $next:$self->master; + if(my $next = $self->$next_storage(@args)) { + return $next; + } else { + return $self->master; + } }; =head2 increment_storage @@ -184,10 +187,10 @@ around 'select_single' => sub { my ($select_single, $self, @args) = @_; if (my $forced_pool = $args[-1]->{force_pool}) { - delete $args[-1]->{force_pool}; - return $self->_get_forced_pool($forced_pool)->select_single(@args); + delete $args[-1]->{force_pool}; + return $self->_get_forced_pool($forced_pool)->select_single(@args); } else { - $self->increment_storage; + $self->increment_storage; return $self->$select_single(@args); } }; diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm index e90445c..8bbaa49 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm @@ -1,6 +1,5 @@ package DBIx::Class::Storage::DBI::Replicated::Balancer::Random; -use List::Util qw(shuffle); use Moose; with 'DBIx::Class::Storage::DBI::Replicated::Balancer'; @@ -43,8 +42,9 @@ sub next_storage { my $self = shift @_; my @active_replicants = $self->pool->active_replicants; my $count_active_replicants = $#active_replicants +1; + my $random_replicant = int(rand($count_active_replicants)); - return $active_replicants[int(rand($count_active_replicants +1))]; + return $active_replicants[$random_replicant]; } =head1 AUTHOR