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