File Coverage

blib/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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