X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FReplicated%2FBalancer%2FRandom.pm;h=f06875e16150e36f50c7a4d9d7b78776c44d959f;hb=47e6d13a64b4fb3ab2c89670afe895c240814abf;hp=2b0aef9c2916b0b36f140aaf7ea50a172d673815;hpb=ee356d004e088518205ac720b37c31a64c9cd861;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm index 2b0aef9..f06875e 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm @@ -13,27 +13,29 @@ DBIx::Class::Storage::DBI::Replicated::Balancer::Random - A 'random' Balancer This class is used internally by L. You shouldn't need to create instances of this class. - + =head1 DESCRIPTION Given a pool (L) of replicated database's (L), defines a method by which query load can be spread out across each replicant in the pool. -This Balancer uses L keyword 'shuffle' to randomly pick an active -replicant from the associated pool. This may or may not be random enough for -you, patches welcome. - =head1 ATTRIBUTES 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. +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 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 @@ -55,31 +57,33 @@ sub next_storage { 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); + 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 by default. - -=cut - -sub random_number { +sub _random_number { rand($_[1]) } -=head1 AUTHOR +=head1 FURTHER QUESTIONS? -John Napiorkowski +Check the list of L. -=head1 LICENSE +=head1 COPYRIGHT AND LICENSE -You may distribute this code under the same terms as Perl itself. +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. =cut