::DBI::Replicated - add master_read_weight to ::Random balancer_type
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / Balancer / Random.pm
index e90445c..2b0aef9 100644 (file)
@@ -1,12 +1,13 @@
 package DBIx::Class::Storage::DBI::Replicated::Balancer::Random;
 
-use List::Util qw(shuffle);
 use Moose;
 with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
-DBIx::Class::Storage::DBI::Replicated::Balancer::Random; A 'random' Balancer
+DBIx::Class::Storage::DBI::Replicated::Balancer::Random - A 'random' Balancer
 
 =head1 SYNOPSIS
 
@@ -27,6 +28,17 @@ you, patches welcome.
 
 This class defines the following attributes.
 
+=head2 master_read_weight
+
+A number from 0 to 1 that specifies what weight to give the master when choosing
+which backend to execute a read query on. A value of 0, which is the default,
+does no reads from master, while a value of 1 gives it the same priority as any
+single replicant.
+
+=cut
+
+has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
+
 =head1 METHODS
 
 This class defines the following methods.
@@ -41,10 +53,24 @@ be requested several times in a row.
 
 sub next_storage {
   my $self = shift @_;
-  my @active_replicants = $self->pool->active_replicants;
-  my $count_active_replicants = $#active_replicants +1;
-  
-  return $active_replicants[int(rand($count_active_replicants +1))];
+
+  my @replicants = $self->pool->active_replicants;
+  my $master     = $self->master;
+
+  my $rnd = $self->random_number(@replicants + $self->master_read_weight);
+
+  return $rnd >= @replicants ? $master : $replicants[int $rnd];
+}
+
+=head2 random_number
+
+Returns a random number from 0 to x, not including x. Uses perl's
+L<perlfunc/rand> by default.
+
+=cut
+
+sub random_number {
+  rand($_[1])
 }
 
 =head1 AUTHOR
@@ -59,4 +85,4 @@ You may distribute this code under the same terms as Perl itself.
 
 __PACKAGE__->meta->make_immutable;
 
-1;
\ No newline at end of file
+1;