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