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
CommitLineData
cb6ec758 1package DBIx::Class::Storage::DBI::Replicated::Balancer::Random;
2
cb6ec758 3use Moose;
17b05c13 4with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
ee356d00 5use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
41916570 6use namespace::clean -except => 'meta';
cb6ec758 7
8=head1 NAME
9
f09cd1d4 10DBIx::Class::Storage::DBI::Replicated::Balancer::Random - A 'random' Balancer
cb6ec758 11
12=head1 SYNOPSIS
13
14This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
15shouldn't need to create instances of this class.
16
17=head1 DESCRIPTION
18
19Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
20database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
21method by which query load can be spread out across each replicant in the pool.
22
23This Balancer uses L<List::Util> keyword 'shuffle' to randomly pick an active
24replicant from the associated pool. This may or may not be random enough for
25you, patches welcome.
26
27=head1 ATTRIBUTES
28
29This class defines the following attributes.
30
ee356d00 31=head2 master_read_weight
32
b88b85e7 33A number greater than 0 that specifies what weight to give the master when
34choosing which backend to execute a read query on. A value of 0, which is the
35default, does no reads from master, while a value of 1 gives it the same
36priority as any single replicant.
37
38For example: if you have 2 replicants, and a L</master_read_weight> of C<0.5>,
39the chance of reading from master will be C<20%>.
40
41You can set it to a value higher than 1, making master have higher weight than
42any single replicant, if for example you have a very powerful master.
ee356d00 43
44=cut
45
46has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
47
cb6ec758 48=head1 METHODS
49
50This class defines the following methods.
51
52=head2 next_storage
53
54Returns an active replicant at random. Please note that due to the nature of
55the word 'random' this means it's possible for a particular active replicant to
56be requested several times in a row.
57
58=cut
59
60sub next_storage {
c354902c 61 my $self = shift @_;
ee356d00 62
63 my @replicants = $self->pool->active_replicants;
f404f53c 64
65 if (not @replicants) {
66 # will fall back to master anyway
67 return;
68 }
69
ee356d00 70 my $master = $self->master;
71
f404f53c 72 my $rnd = $self->_random_number(@replicants + $self->master_read_weight);
ee356d00 73
74 return $rnd >= @replicants ? $master : $replicants[int $rnd];
75}
76
f404f53c 77sub _random_number {
ee356d00 78 rand($_[1])
cb6ec758 79}
80
cb6ec758 81=head1 AUTHOR
82
83John Napiorkowski <john.napiorkowski@takkle.com>
84
85=head1 LICENSE
86
87You may distribute this code under the same terms as Perl itself.
88
89=cut
90
c354902c 91__PACKAGE__->meta->make_immutable;
92
41916570 931;