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.007002';
24 55     55   25842 use Moo;
  55         101  
  55         322  
25              
26 55     55   19210 use URI();
  55         119  
  55         1202  
27 55     55   214 use Time::HiRes qw(time);
  55         96  
  55         415  
28 55     55   3314 use Try::Tiny;
  55         132  
  55         2943  
29 55     55   234 use OpenSearch::Client::Util qw(upgrade_error);
  55         115  
  55         364  
30 55     55   13364 use namespace::clean;
  55         92  
  55         1860  
31              
32             with 'OpenSearch::Client::Role::Is_Sync',
33             'OpenSearch::Client::Role::Transport';
34              
35             #===================================
36             sub perform_request {
37             #===================================
38 160     160 1 1003 my $self = shift;
39 160         472 my $params = $self->tidy_request(@_);
40 160         354 my $pool = $self->cxn_pool;
41 160         301 my $logger = $self->logger;
42              
43 160         234 my ( $code, $response, $cxn, $error );
44              
45             try {
46 160     160   8583 $cxn = $pool->next_cxn;
47 148         1110 my $start = time();
48 148         489 $logger->trace_request( $cxn, $params );
49              
50 148         9299 ( $code, $response ) = $cxn->perform_request($params);
51 114         483 $pool->request_ok($cxn);
52 114         549 $logger->trace_response( $cxn, $code, $response, time() - $start );
53             }
54             catch {
55 46     46   793 $error = upgrade_error(
56             $_,
57             { request => $params,
58             status_code => $code,
59             body => $response
60             }
61             );
62 160         1095 };
63              
64 160 100       5505 if ($error) {
65 46 100       173 if ( $pool->request_failed( $cxn, $error ) ) {
66 19         89 $logger->debugf( "[%s] %s", $cxn->stringify, "$error" );
67 19         529 $logger->info('Retrying request on a new cxn');
68 19         372 return $self->perform_request($params);
69             }
70              
71 27         117 $logger->trace_error( $cxn, $error );
72 27 100       1197 $error->is('NoNodes')
73             ? $logger->throw_critical($error)
74             : $logger->throw_error($error);
75             }
76              
77 114         676 return $response;
78             }
79              
80             1;
81              
82             __END__