File Coverage

blib/lib/Search/Elasticsearch/Role/CxnPool/Static/NoPing.pm
Criterion Covered Total %
statement 26 27 96.3
branch 5 6 83.3
condition 4 6 66.6
subroutine 8 8 100.0
pod 0 4 0.0
total 43 51 84.3


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V. under one or more contributor
2             # license agreements. See the NOTICE file distributed with
3             # this work for additional information regarding copyright
4             # ownership. Elasticsearch B.V. licenses this file to you under
5             # the Apache License, Version 2.0 (the "License"); you may
6             # not use this file except in compliance with the License.
7             # You may obtain a copy of the License at
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing,
12             # software distributed under the License is distributed on an
13             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14             # KIND, either express or implied. See the License for the
15             # specific language governing permissions and limitations
16             # under the License.
17              
18             package Search::Elasticsearch::Role::CxnPool::Static::NoPing;
19             $Search::Elasticsearch::Role::CxnPool::Static::NoPing::VERSION = '8.00';
20 7     7   3319 use Moo::Role;
  7         16  
  7         41  
21             with 'Search::Elasticsearch::Role::CxnPool';
22              
23 7     7   2353 use namespace::clean;
  7         16  
  7         42  
24              
25             has 'max_retries' => ( is => 'lazy' );
26             has '_dead_cxns' => ( is => 'ro', default => sub { [] } );
27              
28             #===================================
29             sub next_cxn {
30             #===================================
31 58     58 0 95 my $self = shift;
32              
33 58         118 my $cxns = $self->cxns;
34 58         95 my $total = @$cxns;
35 58         109 my $dead = $self->_dead_cxns;
36              
37 58         133 while ( $total-- ) {
38 69         176 my $cxn = $cxns->[ $self->next_cxn_num ];
39 69 100 100     206 return $cxn
40             if $cxn->is_live
41             || $cxn->next_ping < time();
42 13 100       62 push @$dead, $cxn unless grep { $_ eq $cxn } @$dead;
  10         39  
43             }
44              
45 2 50 33     45 if ( @$dead and $self->retries <= $self->max_retries ) {
46 2         23 $_->force_ping for @$dead;
47 2         5 return shift @$dead;
48             }
49 0         0 throw( "NoNodes", "No nodes are available: [" . $self->cxns_str . ']' );
50             }
51              
52             #===================================
53 4     4   39 sub _build_max_retries { @{ shift->cxns } - 1 }
  4         39  
54 11     11   187 sub _max_retries { shift->max_retries + 1 }
55             #===================================
56              
57             #===================================
58             sub BUILD {
59             #===================================
60 7     7 0 59 my $self = shift;
61 7         13 $self->set_cxns( @{ $self->seed_nodes } );
  7         36  
62             }
63              
64             #===================================
65             sub should_mark_dead {
66             #===================================
67 12     12 0 28 my ( $self, $error ) = @_;
68 12         33 return $error->is( 'Cxn', 'Timeout' );
69             }
70              
71             #===================================
72             after 'reset_retries' => sub {
73             #===================================
74             my $self = shift;
75             @{ $self->_dead_cxns } = ();
76              
77             };
78              
79             #===================================
80       12 0   sub schedule_check { }
81             #===================================
82              
83             1;
84              
85             # ABSTRACT: A CxnPool for connecting to a remote cluster without the ability to ping.
86              
87             __END__