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