This piece of doc has been incorrect since c354902c
[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.
d4daee7b 16
cb6ec758 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
cb6ec758 23=head1 ATTRIBUTES
24
25This class defines the following attributes.
26
ee356d00 27=head2 master_read_weight
28
b88b85e7 29A number greater than 0 that specifies what weight to give the master when
30choosing which backend to execute a read query on. A value of 0, which is the
31default, does no reads from master, while a value of 1 gives it the same
32priority as any single replicant.
33
34For example: if you have 2 replicants, and a L</master_read_weight> of C<0.5>,
35the chance of reading from master will be C<20%>.
36
37You can set it to a value higher than 1, making master have higher weight than
38any single replicant, if for example you have a very powerful master.
ee356d00 39
40=cut
41
42has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
43
cb6ec758 44=head1 METHODS
45
46This class defines the following methods.
47
48=head2 next_storage
49
50Returns an active replicant at random. Please note that due to the nature of
51the word 'random' this means it's possible for a particular active replicant to
52be requested several times in a row.
53
54=cut
55
56sub next_storage {
c354902c 57 my $self = shift @_;
ee356d00 58
59 my @replicants = $self->pool->active_replicants;
f404f53c 60
61 if (not @replicants) {
62 # will fall back to master anyway
63 return;
64 }
65
ee356d00 66 my $master = $self->master;
67
f404f53c 68 my $rnd = $self->_random_number(@replicants + $self->master_read_weight);
ee356d00 69
70 return $rnd >= @replicants ? $master : $replicants[int $rnd];
71}
72
f404f53c 73sub _random_number {
ee356d00 74 rand($_[1])
cb6ec758 75}
76
a2bd3796 77=head1 FURTHER QUESTIONS?
cb6ec758 78
a2bd3796 79Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
cb6ec758 80
a2bd3796 81=head1 COPYRIGHT AND LICENSE
cb6ec758 82
a2bd3796 83This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
84by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
85redistribute it and/or modify it under the same terms as the
86L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
cb6ec758 87
88=cut
89
c354902c 90__PACKAGE__->meta->make_immutable;
91
41916570 921;