1 package DBIx::Class::Storage::DBI::Replicated::Balancer;
7 DBIx::Class::Storage::DBI::Replicated::Balancer; A Software Load Balancer
11 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
12 shouldn't need to create instances of this class.
16 Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
17 database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
18 method by which query load can be spread out across each replicant in the pool.
22 This class defines the following attributes.
26 The L<DBIx::Class::Storage::DBI::Replicated::Pool> object that we are trying to
33 isa=>'DBIx::Class::Storage::DBI::Replicated::Pool',
37 =head2 current_replicant
39 Replicant storages (slaves) handle all read only traffic. The assumption is
40 that your database will become readbound well before it becomes write bound
41 and that being able to spread your read only traffic around to multiple
42 databases is going to help you to scale traffic.
44 This attribute returns the next slave to handle a read request. Your L</pool>
45 attribute has methods to help you shuffle through all the available replicants
46 via it's balancer object.
50 has 'current_replicant' => (
52 isa=>'DBIx::Class::Storage::DBI',
63 This class defines the following methods.
65 =head2 _build_current_replicant
67 Lazy builder for the L</current_replicant_storage> attribute.
71 sub _build_current_replicant {
73 $self->next_storage($self->pool);
78 Given a pool object, return the next replicant that will serve queries. The
79 default behavior is to grap the first replicant it finds but you can write
80 your own subclasses of L<DBIx::Class::Storage::DBI::Replicated::Balancer> to
81 support other balance systems.
87 return ($self->pool->active_replicants)[0]
88 if $self->pool->active_replicants;
93 Advice on the select attribute. Each time we use a replicant
94 we need to change it via the storage pool algorithm. That way we are spreading
95 the load evenly (hopefully) across existing capacity.
99 after 'select' => sub {
101 my $next_replicant = $self->next_storage;
102 $self->current_replicant($next_replicant);
105 =head2 after: select_single
107 Advice on the select_single attribute. Each time we use a replicant
108 we need to change it via the storage pool algorithm. That way we are spreading
109 the load evenly (hopefully) across existing capacity.
113 after 'select_single' => sub {
115 my $next_replicant = $self->next_storage;
116 $self->current_replicant($next_replicant);
119 =head2 after: columns_info_for
121 Advice on the current_replicant_storage attribute. Each time we use a replicant
122 we need to change it via the storage pool algorithm. That way we are spreading
123 the load evenly (hopefully) across existing capacity.
127 after 'columns_info_for' => sub {
129 my $next_replicant = $self->next_storage;
130 $self->current_replicant($next_replicant);
135 John Napiorkowski <john.napiorkowski@takkle.com>
139 You may distribute this code under the same terms as Perl itself.