| 1 | package DBIO::Replicated::Balancer::Random; | ||||||||||
| 2 | # ABSTRACT: Randomly choose an active replicant | ||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.900007'; | |||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use base 'DBIO::Replicated::Balancer'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | __PACKAGE__->mk_group_accessors(simple => 'master_read_weight'); | |||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 15 | 0 | my ($class, %args) = @_; | |||||||||
| 16 | 0 | my $self = $class->SUPER::new(%args); | |||||||||
| 17 | 0 | ▶ | $self->master_read_weight(exists $args{master_read_weight} ? $args{master_read_weight} : 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | return $self; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub next_storage { | ||||||||
|
Subroutine
next_storage: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 23 | 0 | my $self = shift; | |||||||||
| 24 | |||||||||||
| 25 | 0 | my @replicants = $self->pool->active_replicants; | |||||||||
| 26 | 0 | ▶ | return if not @replicants; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | my $rnd = $self->_random_number(@replicants + $self->master_read_weight); | |||||||||
| 29 | 0 | ▶ | return $rnd >= @replicants ? $self->master : $replicants[int $rnd]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub _random_number { | ||||||||
|
Subroutine
_random_number: not called CC=1 SCAR=6.9
| |||||||||||
| 33 | 0 | rand($_[1]); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 2 | 1; | |||||||||
| 37 | |||||||||||
| 38 | __END__ | ||||||||||