File Coverage

blib/lib/OpenSearch/Client/Transport.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 55 55 100.0


line stmt bran cond sub pod time code
1             # OpenSearch::Client is an unofficial client for OpenSearch.
2             # It is derived from Search::Elasticsearch version 7.714
3             # License details from that work are contained in the NOTICE
4             # file distributed with this work.
5             #-----------------------------------------------------------------------
6             # OpenSearch::Client
7             #-----------------------------------------------------------------------
8             # Copyright 2026 Mark Dootson
9             #
10             # Licensed under the Apache License, Version 2.0 (the "License");
11             # you may not use this file except in compliance with the License.
12             # You may obtain a copy of the License at
13             #
14             # http://www.apache.org/licenses/LICENSE-2.0
15             #
16             # Unless required by applicable law or agreed to in writing, software
17             # distributed under the License is distributed on an "AS IS" BASIS,
18             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19             # See the License for the specific language governing permissions and
20             # limitations under the License.
21              
22             package OpenSearch::Client::Transport;
23             $OpenSearch::Client::Transport::VERSION = '3.007004';
24 55     55   26059 use Moo;
  55         111  
  55         300  
25              
26 55     55   19795 use URI();
  55         126  
  55         1374  
27 55     55   210 use Time::HiRes qw(time);
  55         108  
  55         421  
28 55     55   3538 use Try::Tiny;
  55         117  
  55         2877  
29 55     55   260 use OpenSearch::Client::Util qw(upgrade_error);
  55         134  
  55         388  
30 55     55   13497 use namespace::clean;
  55         183  
  55         366  
31              
32             with 'OpenSearch::Client::Role::Is_Sync',
33             'OpenSearch::Client::Role::Transport';
34              
35             #===================================
36             sub perform_request {
37             #===================================
38 160     160 1 1024 my $self = shift;
39 160         464 my $params = $self->tidy_request(@_);
40 160         337 my $pool = $self->cxn_pool;
41 160         295 my $logger = $self->logger;
42              
43 160         219 my ( $code, $response, $cxn, $error );
44              
45             try {
46 160     160   8729 $cxn = $pool->next_cxn;
47 148         1275 my $start = time();
48 148         514 $logger->trace_request( $cxn, $params );
49              
50 148         9211 ( $code, $response ) = $cxn->perform_request($params);
51 114         555 $pool->request_ok($cxn);
52 114         538 $logger->trace_response( $cxn, $code, $response, time() - $start );
53             }
54             catch {
55 46     46   867 $error = upgrade_error(
56             $_,
57             { request => $params,
58             status_code => $code,
59             body => $response
60             }
61             );
62 160         1070 };
63              
64 160 100       5791 if ($error) {
65 46 100       176 if ( $pool->request_failed( $cxn, $error ) ) {
66 19         97 $logger->debugf( "[%s] %s", $cxn->stringify, "$error" );
67 19         622 $logger->info('Retrying request on a new cxn');
68 19         430 return $self->perform_request($params);
69             }
70              
71 27         118 $logger->trace_error( $cxn, $error );
72 27 100       1171 $error->is('NoNodes')
73             ? $logger->throw_critical($error)
74             : $logger->throw_error($error);
75             }
76              
77 114         673 return $response;
78             }
79              
80             1;
81              
82             __END__