1 package DBIx::Class::Storage::DBI::Replicated::Balancer::Random;
4 with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
5 use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
6 use namespace::clean -except => 'meta';
10 DBIx::Class::Storage::DBI::Replicated::Balancer::Random - A 'random' Balancer
14 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
15 shouldn't need to create instances of this class.
19 Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
20 database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
21 method by which query load can be spread out across each replicant in the pool.
23 This Balancer uses L<List::Util> keyword 'shuffle' to randomly pick an active
24 replicant from the associated pool. This may or may not be random enough for
29 This class defines the following attributes.
31 =head2 master_read_weight
33 A number from 0 to 1 that specifies what weight to give the master when choosing
34 which backend to execute a read query on. A value of 0, which is the default,
35 does no reads from master, while a value of 1 gives it the same priority as any
40 has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
44 This class defines the following methods.
48 Returns an active replicant at random. Please note that due to the nature of
49 the word 'random' this means it's possible for a particular active replicant to
50 be requested several times in a row.
57 my @replicants = $self->pool->active_replicants;
58 my $master = $self->master;
60 my $rnd = $self->random_number(@replicants + $self->master_read_weight);
62 return $rnd >= @replicants ? $master : $replicants[int $rnd];
67 Returns a random number from 0 to x, not including x. Uses perl's
68 L<perlfunc/rand> by default.
78 John Napiorkowski <john.napiorkowski@takkle.com>
82 You may distribute this code under the same terms as Perl itself.
86 __PACKAGE__->meta->make_immutable;