fix ::DBI::Replicated::all_storages
[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';
cb6ec758 5
6=head1 NAME
7
f09cd1d4 8DBIx::Class::Storage::DBI::Replicated::Balancer::Random - A 'random' Balancer
cb6ec758 9
10=head1 SYNOPSIS
11
12This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
13shouldn't need to create instances of this class.
14
15=head1 DESCRIPTION
16
17Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
18database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
19method by which query load can be spread out across each replicant in the pool.
20
21This Balancer uses L<List::Util> keyword 'shuffle' to randomly pick an active
22replicant from the associated pool. This may or may not be random enough for
23you, patches welcome.
24
25=head1 ATTRIBUTES
26
27This class defines the following attributes.
28
29=head1 METHODS
30
31This class defines the following methods.
32
33=head2 next_storage
34
35Returns an active replicant at random. Please note that due to the nature of
36the word 'random' this means it's possible for a particular active replicant to
37be requested several times in a row.
38
39=cut
40
41sub next_storage {
c354902c 42 my $self = shift @_;
43 my @active_replicants = $self->pool->active_replicants;
44 my $count_active_replicants = $#active_replicants +1;
bc376f59 45 my $random_replicant = int(rand($count_active_replicants));
c354902c 46
bc376f59 47 return $active_replicants[$random_replicant];
cb6ec758 48}
49
cb6ec758 50=head1 AUTHOR
51
52John Napiorkowski <john.napiorkowski@takkle.com>
53
54=head1 LICENSE
55
56You may distribute this code under the same terms as Perl itself.
57
58=cut
59
c354902c 60__PACKAGE__->meta->make_immutable;
61
cb6ec758 621;