minor replication changes - use a real hash merge, clarify master_read_weight, really...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / Balancer / Random.pm
index 086ee13..f23db75 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,23 @@ you, patches welcome.
 
 This class defines the following attributes.
 
+=head2 master_read_weight
+
+A number greater than 0 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.
+
+For example: if you have 2 replicants, and a L</master_read_weight> of C<0.5>,
+the chance of reading from master will be C<20%>.
+
+You can set it to a value higher than 1, making master have higher weight than
+any single replicant, if for example you have a very powerful master.
+
+=cut
+
+has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
+
 =head1 METHODS
 
 This class defines the following methods.
@@ -40,7 +58,24 @@ be requested several times in a row.
 =cut
 
 sub next_storage {
-  return  (shuffle(shift->pool->active_replicants))[0];
+  my $self = shift @_;
+
+  my @replicants = $self->pool->active_replicants;
+
+  if (not @replicants) {
+    # will fall back to master anyway
+    return;
+  }
+
+  my $master     = $self->master;
+
+  my $rnd = $self->_random_number(@replicants + $self->master_read_weight);
+
+  return $rnd >= @replicants ? $master : $replicants[int $rnd];
+}
+
+sub _random_number {
+  rand($_[1])
 }
 
 =head1 AUTHOR
@@ -53,4 +88,6 @@ You may distribute this code under the same terms as Perl itself.
 
 =cut
 
-1;
\ No newline at end of file
+__PACKAGE__->meta->make_immutable;
+
+1;