blib/lib/Net/Proxy/Connector/tcp_balance.pm

filter: Statementstmt 36.3% 12 / 33 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 26.6% 16 / 60 CC 10 SCAR 44.4
CC
10
Cov
24%
connect
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 84.8
↑ Summary
1 ## no critic (RequireUseStrict)
2 package Net::Proxy::Connector::tcp_balance;
3 2 $Net::Proxy::Connector::tcp_balance::VERSION = '0.006';
4 ## use critic (RequireUseStrict)
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Net::Proxy::Connector::tcp;
9 2 ▶use base "Net::Proxy::Connector::tcp";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub connect {
Subroutine connect: not called CC=10 SCAR=47.0
Pod documented
12 0 my @params = @_;
13 0 my ($self) = shift @params;
14 0 my @hosts_sorted = sort {int(rand(3))-1} @{$self->{hosts}};
15 0 ▶ if ( $self->{sort} and $self->{sort} eq 'order' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sort'} and $self->{'sort'} eq 'order') { } 0 0 0
elsif ($self->{'sort'} and $self->{'sort'} eq 'none') { } 0 0 0
Condition0%0/3
$self->{"sort"} and $self->{"sort"} eq "order"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sort"} and $self->{"sort"} eq "order"
$self->{"sort"} $self->{"sort"} eq "order"
Truth table0%0/3
A and B
A= $self->{"sort"}B= $self->{"sort"} eq "order"
ABresult
0X0
100
111
16 0 @hosts_sorted = sort @{$self->{hosts}};
17 }
18 ▶ elsif ( $self->{sort} and $self->{sort} eq 'none' ){
Condition0%0/3
$self->{"sort"} and $self->{"sort"} eq "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sort"} and $self->{"sort"} eq "none"
$self->{"sort"} $self->{"sort"} eq "none"
Truth table0%0/3
A and B
A= $self->{"sort"}B= $self->{"sort"} eq "none"
ABresult
0X0
100
111
19 0 @hosts_sorted = @{$self->{hosts}};
20 }
21 0 ▶ warn "[ ".localtime." ] tcp_balance hosts_sorted are: ".join(',', @hosts_sorted)."\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
22 0 for ( @hosts_sorted ) {
23 0 $self->{host} = $_;
24 0 my $sock = eval { $self->SUPER::connect(@params); };
25 0 ▶ if ($@) { # connect() dies if the connection fails
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
26 0 warn "[ ".localtime." ] tcp_balance failed to connect to ".$self->{host} ." '$@'\n";
27 0 next;
28 }
29 0 ▶ warn "[ ".localtime." ] tcp_balance connected to ".$self->{host} ." '$@'\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
30 0 ▶ return $sock if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 0 0 0
31 }
32 0 die "[ ".localtime." ] tcp_balance failed all hosts";
33 }
34  
35 2 1;
36  
37 =pod
38  
39 =encoding UTF-8
40  
41 =head1 NAME
42  
43 Net::Proxy::Connector::tcp_balance - A Net::Proxy connector for outbound tcp balancing and failover
44  
45 =head1 VERSION
46  
47 version 0.006
48  
49 =head1 SYNOPSIS
50  
51 # sample proxy using Net::Proxy::Connector::tcp_balance
52 use Net::Proxy;
53 use Net::Proxy::Connector::tcp_balance; # optional
54  
55 # proxy connections from localhost:6789 to remotehost:9876
56 # using standard TCP connections
57 my $proxy = Net::Proxy->new(
58 { in => { type => 'tcp', port => '6789' },
59 out => { type => 'tcp_balance', hosts => [ 'remotehost1', 'remotehost2' ], port => '9876', verbose => 1 },
60 }
61 );
62 $proxy->register();
63  
64 Net::Proxy->mainloop();
65  
66 =head1 DESCRIPTION
67  
68 C<Net::Proxy::Connector::tcp_balance> is an outbound tcp connector for C<Net::Proxy> that provides randomized load balancing and also provides failover when outbound tcp hosts are unavailable.
69  
70 It will randomly connect to one of the specified hosts. If that host is unavailable, it will continue to try the other hosts until it makes a connection.
71  
72 The capabilities of the C<Net::Proxy::Connector::tcp_balance> are otherwise identical to those C<Net::Proxy::Connector::tcp>
73  
74 =head1 NAME
75  
76 Net::Proxy::Connector::tcp_balance - connector for outbound tcp balancing and failover
77  
78 =head1 CONNECTOR OPTIONS
79  
80 The connector accept the following options:
81  
82 =head2 C<in>
83  
84 =over 4
85  
86 =item * host
87  
88 The listening address. If not given, the default is C<localhost>.
89  
90 =item * port
91  
92 The listening port.
93  
94 =back
95  
96 =head2 C<out>
97  
98 =over 4
99  
100 =item * hosts
101  
102 The remote hosts. An array ref.
103  
104 =item * port
105  
106 The remote port.
107  
108 =item * sort
109  
110 (Optional) Connect to the hosts in sort algorithm. Possible values: order, none, random. Default is random.
111  
112 =item * timeout
113  
114 The socket timeout for connection (C<out> only).
115  
116 =item * verbose
117  
118 (Optional) Will print to STDERR the list of sorted hosts for every request.
119  
120 =back
121  
122 =head1 AUTHOR
123  
124 Jesse Thompson <zjt@cpan.org>
125  
126 =head1 COPYRIGHT AND LICENSE
127  
128 This software is copyright (c) 2019 by Jesse Thompson.
129  
130 This is free software; you can redistribute it and/or modify it under
131 the same terms as the Perl 5 programming language system itself.
132  
133 =cut
134  
135 __END__
↑ Summary