| 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::Cxnscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/; |
| 19 | $Search::Elasticsearch::Role::Cxn::VERSIONscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$VERSION. = '8.12'; |
| 20 | our $PRODUCT_CHECK_HEADERscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$PRODUCT_CHECK_HEADER. = 'x-elastic-product'; |
| 21 | our $PRODUCT_CHECK_VALUEscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$PRODUCT_CHECK_VALUE. = 'Elasticsearch'; |
| 22 | |
| 23 | use Moo::Rolescip-perl cpan Search-Elasticsearch 8.12 Moo/Role/; |
| 24 | use Search::Elasticsearch::Utilscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/ qw(parse_params throw to_list); |
| 25 | use List::Utilscip-perl cpan Search-Elasticsearch 8.12 List/Util/ qw(min); |
| 26 | use Try::Tinyscip-perl cpan Search-Elasticsearch 8.12 Try/Tiny/; |
| 27 | use URIscip-perl cpan Search-Elasticsearch 8.12 URI/(); |
| 28 | use IO::Compress::Deflatescip-perl cpan Search-Elasticsearch 8.12 IO/Compress/Deflate/(); |
| 29 | use IO::Uncompress::Inflatescip-perl cpan Search-Elasticsearch 8.12 IO/Uncompress/Inflate/(); |
| 30 | use IO::Compress::Gzipscip-perl cpan Search-Elasticsearch 8.12 IO/Compress/Gzip/(); |
| 31 | use IO::Uncompress::Gunzipscip-perl cpan Search-Elasticsearch 8.12 IO/Uncompress/Gunzip/ qw(gunzip $GunzipError); |
| 32 | use Search::Elasticsearch::Utilscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/ qw(to_list); |
| 33 | use namespace::cleanscip-perl cpan Search-Elasticsearch 8.12 namespace/clean/; |
| 34 | use Net::IPscip-perl cpan Search-Elasticsearch 8.12 Net/IP/; |
| 35 | |
| 36 | requires qw(perform_request error_from_text handle); |
| 37 | |
| 38 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'client_version' => ( is => 'ro', required => 1, default => $Search::Elasticsearch::VERSIONscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/$VERSION. ); |
| 39 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'host' => ( is => 'ro', required => 1 ); |
| 40 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'port' => ( is => 'ro', required => 1 ); |
| 41 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'uri' => ( is => 'ro', required => 1 ); |
| 42 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'request_timeout' => ( is => 'ro', default => 30 ); |
| 43 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'ping_timeout' => ( is => 'ro', default => 2 ); |
| 44 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'sniff_timeout' => ( is => 'ro', default => 1 ); |
| 45 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'sniff_request_timeout' => ( is => 'ro', default => 2 ); |
| 46 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'next_ping' => ( is => 'rw', default => 0 ); |
| 47 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'ping_failures' => ( is => 'rw', default => 0 ); |
| 48 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'dead_timeout' => ( is => 'ro', default => 60 ); |
| 49 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'max_dead_timeout' => ( is => 'ro', default => 3600 ); |
| 50 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'serializer' => ( is => 'ro', required => 1 ); |
| 51 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'logger' => ( is => 'ro', required => 1 ); |
| 52 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'handle_args' => ( is => 'ro', default => sub { {} } ); |
| 53 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'default_qs_params' => ( is => 'ro', default => sub { {} } ); |
| 54 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'scheme' => ( is => 'ro' ); |
| 55 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'is_https' => ( is => 'ro' ); |
| 56 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'userinfo' => ( is => 'ro' ); |
| 57 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'max_content_length' => ( is => 'ro' ); |
| 58 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'default_headers' => ( is => 'ro' ); |
| 59 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'deflate' => ( is => 'ro' ); |
| 60 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'gzip' => ( is => 'ro' ); |
| 61 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'ssl_options' => ( is => 'ro', predicate => 'has_ssl_options' ); |
| 62 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). 'handle' => ( is => 'lazy', clearer => 1 ); |
| 63 | hasscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/has(). '_pid' => ( is => 'rw', default => $$scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$$. ); |
| 64 | |
| 65 | my %Code_To_Error = ( |
| 66 | 400 => 'Request', |
| 67 | 401 => 'Unauthorized', |
| 68 | 403 => 'Forbidden', |
| 69 | 404 => 'Missing', |
| 70 | 408 => 'RequestTimeout', |
| 71 | 409 => 'Conflict', |
| 72 | 413 => 'ContentLength', |
| 73 | 502 => 'BadGateway', |
| 74 | 503 => 'Unavailable', |
| 75 | 504 => 'GatewayTimeout' |
| 76 | ); |
| 77 | |
| 78 | #=================================== |
| 79 | sub stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify(). { shift->uriscip-perl cpan Search-Elasticsearch 8.12 shift/uri(). . '' } |
| 80 | #=================================== |
| 81 | |
| 82 | #=================================== |
| 83 | sub get_user_agentscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/get_user_agent(). { |
| 84 | #=================================== |
| 85 | return sprintfscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sprintf().("elasticsearch-perl/%s (%s; perl %s)", $Search::Elasticsearch::VERSIONscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/$VERSION., $^Oscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`$^O`., $]scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`$]`.); |
| 86 | } |
| 87 | |
| 88 | #=================================== |
| 89 | sub get_meta_headerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/get_meta_header(). { |
| 90 | #=================================== |
| 91 | return sprintfscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sprintf().("es=%s,pl=%s", $Search::Elasticsearch::VERSIONscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/$VERSION., $^Vscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`$^V`.); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | #=================================== |
| 96 | sub BUILDARGSscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/BUILDARGS(). { |
| 97 | #=================================== |
| 98 | my ( $class, $params ) = parse_paramsscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/parse_params().(@_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.); |
| 99 | |
| 100 | my $node = $params->{node} |
| 101 | || { host => 'localhost', port => '9200' }; |
| 102 | |
| 103 | unless ( refscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ref(). $node eq 'HASH' ) { |
| 104 | $node = "[$node]" if Net::IP::ip_is_ipv6scip-perl cpan Search-Elasticsearch 8.12 Net/IP/ip_is_ipv6().($node); |
| 105 | unless ( $node =~ m{^http(s)?://} ) { |
| 106 | $node = ( $params->{use_https} ? 'https://' : 'http://' ) . $node; |
| 107 | } |
| 108 | if ( $params->{port} && $node !~ m{//[^/\[]+:\d+} ) { |
| 109 | $node =~ s{(//[^/]+)}{$1:$params->{port}}; |
| 110 | } |
| 111 | my $uri = URI->newscip-perl cpan Search-Elasticsearch 8.12 URI/new().($node); |
| 112 | $node = { |
| 113 | scheme => $uri->schemescip-perl cpan Search-Elasticsearch 8.12 URI/scheme()., |
| 114 | host => $uri->hostscip-perl cpan Search-Elasticsearch 8.12 URI/host()., |
| 115 | port => $uri->portscip-perl cpan Search-Elasticsearch 8.12 URI/port()., |
| 116 | path => $uri->pathscip-perl cpan Search-Elasticsearch 8.12 URI/path()., |
| 117 | userinfo => $uri->userinfoscip-perl cpan Search-Elasticsearch 8.12 URI/userinfo(). |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | my $host = $node->{host} || 'localhost'; |
| 122 | my $userinfo = $node->{userinfo} || $params->{userinfo} || ''; |
| 123 | my $scheme |
| 124 | = $node->{scheme} || ( $params->{use_https} ? 'https' : 'http' ); |
| 125 | my $port |
| 126 | = $node->{port} |
| 127 | || $params->{port} |
| 128 | || ( $scheme eq 'http' ? 80 : 443 ); |
| 129 | my $path = $node->{path} || $params->{path_prefix} || ''; |
| 130 | $path =~ s{^/?}{/}g; |
| 131 | $path =~ s{/+$}{}; |
| 132 | |
| 133 | my %default_headers = %{ $params->{default_headers} || {} }; |
| 134 | |
| 135 | if ($userinfo) { |
| 136 | requirescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/require(). MIME::Base64; |
| 137 | my $auth = MIME::Base64::encode_base64scip-perl cpan Search-Elasticsearch 8.12 MIME/Base64/encode_base64().( $userinfo, "" ); |
| 138 | chompscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/chomp(). $auth; |
| 139 | $default_headers{Authorization} = "Basic $auth"; |
| 140 | } |
| 141 | |
| 142 | if ( $params->{gzip} ) { |
| 143 | $default_headers{'Accept-Encoding'} = "gzip"; |
| 144 | } |
| 145 | |
| 146 | elsif ( $params->{deflate} ) { |
| 147 | $default_headers{'Accept-Encoding'} = "deflate"; |
| 148 | } |
| 149 | |
| 150 | $default_headers{'User-Agent'} = $class->get_user_agentscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/get_user_agent().(); |
| 151 | |
| 152 | # Add Elastic meta header |
| 153 | $default_headers{'x-elastic-client-meta'} = $class->get_meta_headerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/get_meta_header().(); |
| 154 | |
| 155 | # Compatibility header |
| 156 | if (definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $ENVscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`%ENV`.{ELASTIC_CLIENT_APIVERSIONING} && |
| 157 | (lcscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/lc().($ENVscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`%ENV`.{ELASTIC_CLIENT_APIVERSIONING}) eq 'true' || $ENVscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`%ENV`.{ELASTIC_CLIENT_APIVERSIONING} eq '1')) { |
| 158 | $default_headers{'Accept'} = 'application/vnd.elasticsearch+json;compatible-with=7'; |
| 159 | $default_headers{'Content-Type'} = 'application/vnd.elasticsearch+json; compatible-with=7'; |
| 160 | } |
| 161 | |
| 162 | if (definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{elastic_cloud_api_key} && definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{token_api}) { |
| 163 | throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( 'Request', |
| 164 | "You cannot set elastic_cloud_api_key and token_api together" ); |
| 165 | } |
| 166 | |
| 167 | # Elastic cloud API key |
| 168 | if (definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{elastic_cloud_api_key}) { |
| 169 | $default_headers{'Authorization'} = sprintfscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sprintf().("ApiKey %s", $params->{elastic_cloud_api_key}); |
| 170 | } |
| 171 | |
| 172 | # Elasticsearch token API (https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-token.html) |
| 173 | if (definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{token_api}) { |
| 174 | $default_headers{'Authorization'} = sprintfscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sprintf().("Bearer %s", $params->{token_api}); |
| 175 | } |
| 176 | |
| 177 | # Elasticsearch |
| 178 | $params->{scheme} = $scheme; |
| 179 | $params->{is_https} = $scheme eq 'https'; |
| 180 | $params->{host} = $host; |
| 181 | $params->{port} = $port; |
| 182 | $params->{path} = $path; |
| 183 | $params->{userinfo} = $userinfo; |
| 184 | $host = "[$host]" if Net::IP::ip_is_ipv6scip-perl cpan Search-Elasticsearch 8.12 Net/IP/ip_is_ipv6().($host); |
| 185 | $params->{uri} = URI->newscip-perl cpan Search-Elasticsearch 8.12 URI/new().("$scheme://$host:$port$path"); |
| 186 | $params->{default_headers} = \%default_headers; |
| 187 | # Add the client version |
| 188 | if (definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{client}) { |
| 189 | $params->{client_version} = substrscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/substr().($params->{client}, 0, indexscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/index().($params->{client}, '_')); |
| 190 | } |
| 191 | |
| 192 | return $params; |
| 193 | } |
| 194 | |
| 195 | #=================================== |
| 196 | beforescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/before(). 'handle' => sub { |
| 197 | #=================================== |
| 198 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 199 | if ( $$scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$$. != $self->_pidscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_pid(). ) { |
| 200 | $self->clear_handlescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/clear_handle().; |
| 201 | $self->_pidscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_pid().($$scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$$.); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | #=================================== |
| 206 | sub is_livescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/is_live(). { !shift->next_pingscip-perl cpan Search-Elasticsearch 8.12 shift/next_ping(). } |
| 207 | sub is_deadscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/is_dead(). { !!shift->next_pingscip-perl cpan Search-Elasticsearch 8.12 shift/next_ping(). } |
| 208 | #=================================== |
| 209 | |
| 210 | #=================================== |
| 211 | sub mark_livescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/mark_live(). { |
| 212 | #=================================== |
| 213 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 214 | $self->ping_failuresscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ping_failures().(0); |
| 215 | $self->next_pingscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/next_ping().(0); |
| 216 | } |
| 217 | |
| 218 | #=================================== |
| 219 | sub mark_deadscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/mark_dead(). { |
| 220 | #=================================== |
| 221 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 222 | my $fails = $self->ping_failuresscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ping_failures().; |
| 223 | $self->ping_failuresscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ping_failures().( $fails + 1 ); |
| 224 | |
| 225 | my $timeout |
| 226 | = minscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/min().( $self->dead_timeoutscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/dead_timeout(). * 2**$fails, $self->max_dead_timeoutscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/max_dead_timeout(). ); |
| 227 | my $next = $self->next_pingscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/next_ping().( timescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/time().() + $timeout ); |
| 228 | |
| 229 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->infofscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/infof().( 'Marking [%s] as dead. Next ping at: %s', |
| 230 | $self->stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify()., scalarscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/scalar(). localtimescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/localtime().($next) ); |
| 231 | |
| 232 | } |
| 233 | |
| 234 | #=================================== |
| 235 | sub force_pingscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/force_ping(). { |
| 236 | #=================================== |
| 237 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 238 | $self->ping_failuresscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ping_failures().(0); |
| 239 | $self->next_pingscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/next_ping().(-1); |
| 240 | } |
| 241 | |
| 242 | #=================================== |
| 243 | sub pings_okscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/pings_ok(). { |
| 244 | #=================================== |
| 245 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 246 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->infofscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/infof().( 'Pinging [%s]', $self->stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify(). ); |
| 247 | return try { |
| 248 | $self->perform_requestscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/perform_request().( |
| 249 | { method => 'HEAD', |
| 250 | path => '/', |
| 251 | timeout => $self->ping_timeoutscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ping_timeout()., |
| 252 | } |
| 253 | ); |
| 254 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->infofscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/infof().( 'Marking [%s] as live', $self->stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify(). ); |
| 255 | $self->mark_livescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/mark_live().; |
| 256 | 1; |
| 257 | } |
| 258 | catch { |
| 259 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->debugscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/debug().("$_"); |
| 260 | $self->mark_deadscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/mark_dead().; |
| 261 | 0; |
| 262 | }; |
| 263 | } |
| 264 | |
| 265 | #=================================== |
| 266 | sub sniffscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sniff(). { |
| 267 | #=================================== |
| 268 | my $self = shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().; |
| 269 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->infofscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/infof().( 'Sniffing [%s]', $self->stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify(). ); |
| 270 | return try { |
| 271 | $self->perform_requestscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/perform_request().( |
| 272 | { method => 'GET', |
| 273 | path => '/_nodes/http', |
| 274 | qs => { timeout => $self->sniff_timeoutscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sniff_timeout(). . 's' }, |
| 275 | timeout => $self->sniff_request_timeoutscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/sniff_request_timeout()., |
| 276 | } |
| 277 | )->{nodes}; |
| 278 | } |
| 279 | catch { |
| 280 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->debugscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/debug().($_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$_.); |
| 281 | return; |
| 282 | }; |
| 283 | } |
| 284 | |
| 285 | #=================================== |
| 286 | sub build_uriscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/build_uri(). { |
| 287 | #=================================== |
| 288 | my ( $self, $params ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 289 | my $uri = $self->uriscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/uri().->clonescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/clone().; |
| 290 | $uri->pathscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/path().( $uri->pathscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/path(). . $params->{path} ); |
| 291 | my %qs = ( %{ $self->default_qs_paramsscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/default_qs_params(). }, %{ $params->{qs} || {} } ); |
| 292 | $uri->query_formscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/query_form().( \%qs ); |
| 293 | return $uri; |
| 294 | } |
| 295 | |
| 296 | #=================================== |
| 297 | beforescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/before(). 'perform_request' => sub { |
| 298 | #=================================== |
| 299 | my ( $self, $params ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 300 | return unless definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $params->{data}; |
| 301 | |
| 302 | $self->_compress_bodyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_compress_body().($params); |
| 303 | |
| 304 | my $max = $self->max_content_lengthscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/max_content_length(). |
| 305 | or return; |
| 306 | |
| 307 | return if lengthscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/length().( $params->{data} ) < $max; |
| 308 | |
| 309 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->throw_errorscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/throw_error().( 'ContentLength', |
| 310 | "Body is longer than max_content_length ($max)", |
| 311 | ); |
| 312 | }; |
| 313 | |
| 314 | #=================================== |
| 315 | sub _compress_bodyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_compress_body(). { |
| 316 | #=================================== |
| 317 | my ( $self, $params ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 318 | my $output; |
| 319 | if ( $self->gzipscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/gzip(). ) { |
| 320 | IO::Compress::Gzip::gzipscip-perl cpan Search-Elasticsearch 8.12 IO/Compress/Gzip/gzip().( \( $params->{data} ), \$output ) |
| 321 | or throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( 'Request', |
| 322 | "Couldn't gzip request: $IO::Compress::Gzip::GzipError" ); |
| 323 | $params->{data} = $output; |
| 324 | $params->{encoding} = 'gzip'; |
| 325 | } |
| 326 | elsif ( $self->deflatescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/deflate(). ) { |
| 327 | IO::Compress::Deflate::deflatescip-perl cpan Search-Elasticsearch 8.12 IO/Compress/Deflate/deflate().( \( $params->{data} ), \$output ) |
| 328 | or throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( 'Request', |
| 329 | "Couldn't deflate request: $IO::Compress::Deflate::DeflateError" ); |
| 330 | $params->{data} = $output; |
| 331 | $params->{encoding} = 'deflate'; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | #=================================== |
| 336 | sub _decompress_bodyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_decompress_body(). { |
| 337 | #=================================== |
| 338 | my ( $self, $body_ref, $headers ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 339 | if ( my $encoding = $headers->{'content-encoding'} ) { |
| 340 | my $output; |
| 341 | if ( $encoding eq 'gzip' ) { |
| 342 | IO::Uncompress::Gunzip::gunzipscip-perl cpan Search-Elasticsearch 8.12 IO/Uncompress/Gunzip/gunzip().( $body_ref, \$output ) |
| 343 | or throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( |
| 344 | 'Request', |
| 345 | "Couldn't gunzip response: $IO::Uncompress::Gunzip::GunzipError" |
| 346 | ); |
| 347 | } |
| 348 | elsif ( $encoding eq 'deflate' ) { |
| 349 | IO::Uncompress::Inflate::inflatescip-perl cpan Search-Elasticsearch 8.12 IO/Uncompress/Inflate/inflate().( $body_ref, \$output, |
| 350 | Transparent => 0 ) |
| 351 | or throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( |
| 352 | 'Request', |
| 353 | "Couldn't inflate response: $IO::Uncompress::Inflate::InflateError" |
| 354 | ); |
| 355 | } |
| 356 | else { |
| 357 | throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( 'Request', "Unknown content-encoding: $encoding" ); |
| 358 | } |
| 359 | ${$body_ref} = $output; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | #=================================== |
| 364 | sub process_responsescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/process_response(). { |
| 365 | #=================================== |
| 366 | my ( $self, $params, $code, $msg, $body, $headers ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 367 | |
| 368 | # Product check only for 8+ client API version |
| 369 | if ( $self->client_versionscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/client_version(). >= 8 and $code >= 200 and $code < 300 ) { |
| 370 | my $product = $headers->{$PRODUCT_CHECK_HEADERscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$PRODUCT_CHECK_HEADER.} // ''; |
| 371 | if ($product ne $PRODUCT_CHECK_VALUEscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$PRODUCT_CHECK_VALUE.) { |
| 372 | throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( "ProductCheck", "The client noticed that the server is not Elasticsearch and we do not support this unknown product" ); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | $self->_decompress_bodyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_decompress_body().( \$body, $headers ); |
| 377 | |
| 378 | my ($mime_type) = splitscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/split(). /\s*;\s*/, ( $headers->{'content-type'} || '' ); |
| 379 | |
| 380 | my $is_encoded = $mime_type && $mime_type ne 'text/plain'; |
| 381 | |
| 382 | # Deprecation warnings |
| 383 | if ( my $warnings = $headers->{warning} ) { |
| 384 | my $warning_string = _parse_warningsscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_parse_warnings().($warnings); |
| 385 | my %temp = (%$params); |
| 386 | deletescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/delete(). $temp{data}; |
| 387 | $self->loggerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/logger().->deprecationscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/deprecation().( $warning_string, \%temp ); |
| 388 | } |
| 389 | |
| 390 | # Request is successful |
| 391 | if ( $code >= 200 and $code <= 209 ) { |
| 392 | if ( definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $body and lengthscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/length(). $body ) { |
| 393 | $body = $self->serializerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/serializer().->decodescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/decode().($body) |
| 394 | if $is_encoded; |
| 395 | return $code, $body; |
| 396 | } |
| 397 | return ( $code, 1 ) if $params->{method} eq 'HEAD'; |
| 398 | return ( $code, '' ); |
| 399 | } |
| 400 | |
| 401 | # Check if the error should be ignored |
| 402 | my @ignore = to_listscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/to_list().( $params->{ignore} ); |
| 403 | pushscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/push(). @ignore, 404 if $params->{method} eq 'HEAD'; |
| 404 | return ($code) if grepscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/grep(). { $_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$_. eq $code } @ignore; |
| 405 | |
| 406 | # Determine error type |
| 407 | my $error_type = $Code_To_Error{$code}; |
| 408 | unless ($error_type) { |
| 409 | if ( definedscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/defined(). $body and lengthscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/length(). $body ) { |
| 410 | $msg = $body; |
| 411 | $body = undef; |
| 412 | } |
| 413 | $error_type = $self->error_from_textscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/error_from_text().( $code, $msg ); |
| 414 | } |
| 415 | |
| 416 | deletescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/delete(). $params->{data} if $params->{body}; |
| 417 | my %error_args = ( status_code => $code, request => $params ); |
| 418 | |
| 419 | # Extract error message from the body, if present |
| 420 | |
| 421 | if ( $body = $self->serializerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/serializer().->decodescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/decode().($body) ) { |
| 422 | $error_args{body} = $body; |
| 423 | $msg = $self->_munge_elasticsearch_exceptionscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_munge_elasticsearch_exception().($body) || $msg; |
| 424 | |
| 425 | $error_args{current_version} = $1scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$1. |
| 426 | if $error_type eq 'Conflict' |
| 427 | and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/; |
| 428 | } |
| 429 | $msg ||= $error_type; |
| 430 | |
| 431 | chompscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/chomp(). $msg; |
| 432 | throwscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Util/throw().( $error_type, "[" . $self->stringifyscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/stringify(). . "]-[$code] $msg", |
| 433 | \%error_args ); |
| 434 | } |
| 435 | |
| 436 | #=================================== |
| 437 | sub _parse_warningsscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_parse_warnings(). { |
| 438 | #=================================== |
| 439 | my @warnings = refscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ref(). $_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.[0] eq 'ARRAY' ? @{ shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().() } : shiftscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/shift().(); |
| 440 | my @str; |
| 441 | for (@warnings) { |
| 442 | if ( $_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$_. =~ /^\d+\s+\S+\s+"((?:\\"|[^"])+)"/ ) { |
| 443 | my $msg = $1scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$1.; |
| 444 | $msg =~ s/\\"/"/g, pushscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/push(). @str, $msg; |
| 445 | } |
| 446 | else { |
| 447 | pushscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/push(). @str, $_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$_.; |
| 448 | } |
| 449 | } |
| 450 | return joinscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/join(). "; ", @str; |
| 451 | } |
| 452 | |
| 453 | #=================================== |
| 454 | sub _munge_elasticsearch_exceptionscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/_munge_elasticsearch_exception(). { |
| 455 | #=================================== |
| 456 | my ( $self, $body ) = @_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/`@_`.; |
| 457 | return $body unless refscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ref(). $body eq 'HASH'; |
| 458 | my $error = $body->{error} || return; |
| 459 | return $error unless refscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/ref(). $error eq 'HASH'; |
| 460 | |
| 461 | my $root_causes = $error->{root_cause} || []; |
| 462 | unless (@$root_causes) { |
| 463 | my $msg = "[" . $error->{type} . "] " if $error->{type}; |
| 464 | $msg .= $error->{reason} if $error->{reason}; |
| 465 | return $msg; |
| 466 | } |
| 467 | |
| 468 | my $json = $self->serializerscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/serializer().; |
| 469 | my @msgs; |
| 470 | for (@$root_causes) { |
| 471 | my %cause = (%$_scip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/$_.); |
| 472 | my $msg |
| 473 | = "[" . ( deletescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/delete(). $cause{type} ) . "] " . ( deletescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/delete(). $cause{reason} ); |
| 474 | if ( keysscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/keys(). %cause ) { |
| 475 | $msg .= ", with: " . $json->encodescip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/encode().( \%cause ); |
| 476 | } |
| 477 | pushscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/push(). @msgs, $msg; |
| 478 | } |
| 479 | return ( joinscip-perl cpan Search-Elasticsearch 8.12 Search/Elasticsearch/Role/Cxn/join(). ", ", @msgs ); |
| 480 | } |
| 481 | |
| 482 | 1; |
| 483 | |
| 484 | # ABSTRACT: Provides common functionality to HTTP Cxn implementations |
| 485 | |
| 486 | __END__ |
| 487 | |
| 488 | =pod |
| 489 | |
| 490 | =encoding UTF-8 |
| 491 | |
| 492 | =head1 NAME |
| 493 | |
| 494 | Search::Elasticsearch::Role::Cxn - Provides common functionality to HTTP Cxn implementations |
| 495 | |
| 496 | =head1 VERSION |
| 497 | |
| 498 | version 8.12 |
| 499 | |
| 500 | =head1 DESCRIPTION |
| 501 | |
| 502 | L<Search::Elasticsearch::Role::Cxn> provides common functionality to Cxn |
| 503 | implementations. Cxn instances are created by a |
| 504 | L<Search::Elasticsearch::Role::CxnPool> implementation, using the |
| 505 | L<Search::Elasticsearch::Cxn::Factory> class. |
| 506 | |
| 507 | =head1 CONFIGURATION |
| 508 | |
| 509 | The configuration options are as follows: |
| 510 | |
| 511 | =head2 C<node> |
| 512 | |
| 513 | A single C<node> is passed to C<new()> by the L<Search::Elasticsearch::Cxn::Factory> |
| 514 | class. It can either be a URI or a hash containing each part. For instance: |
| 515 | |
| 516 | node => 'localhost'; # equiv of 'http://localhost:80' |
| 517 | node => 'localhost:9200'; # equiv of 'http://localhost:9200' |
| 518 | node => 'http://localhost:9200'; |
| 519 | |
| 520 | node => 'https://localhost'; # equiv of 'https://localhost:443' |
| 521 | node => 'localhost/path'; # equiv of 'http://localhost:80/path' |
| 522 | |
| 523 | |
| 524 | node => 'http://user:pass@localhost'; # equiv of 'http://localhost:80' |
| 525 | # with userinfo => 'user:pass' |
| 526 | |
| 527 | Alternatively, a C<node> can be specified as a hash: |
| 528 | |
| 529 | { |
| 530 | scheme => 'http', |
| 531 | host => 'search.domain.com', |
| 532 | port => '9200', |
| 533 | path => '/path', |
| 534 | userinfo => 'user:pass' |
| 535 | } |
| 536 | |
| 537 | Similarly, default values can be specified with C<port>, C<path_prefix>, |
| 538 | C<userinfo> and C<use_https>: |
| 539 | |
| 540 | $e = Search::Elasticsearch->new( |
| 541 | port => 9201, |
| 542 | path_prefix => '/path', |
| 543 | userinfo => 'user:pass', |
| 544 | use_https => 1, |
| 545 | nodes => [ 'search1', 'search2' ] |
| 546 | ) |
| 547 | |
| 548 | =head2 C<ssl_options> |
| 549 | |
| 550 | By default, all backends that support HTTPS disable verification of |
| 551 | the host they are connecting to. Use C<ssl_options> to configure |
| 552 | the type of verification that you would like the client to perform, |
| 553 | or to configure the client to present its own certificate. |
| 554 | |
| 555 | The values accepted by C<ssl_options> depend on the C<Cxn> class. See the |
| 556 | documentation for the C<Cxn> class that you are using. |
| 557 | |
| 558 | =head2 C<max_content_length> |
| 559 | |
| 560 | By default, Elasticsearch nodes accept a maximum post body of 100MB or |
| 561 | C<104_857_600> bytes. This client enforces that limit. The limit can |
| 562 | be customised with the C<max_content_length> parameter (specified in bytes). |
| 563 | |
| 564 | If you're using the L<Search::Elasticsearch::CxnPool::Sniff> module, then the |
| 565 | C<max_content_length> will be automatically retrieved from the live cluster, |
| 566 | unless you specify a custom C<max_content_length>: |
| 567 | |
| 568 | # max_content_length retrieved from cluster |
| 569 | $e = Search::Elasticsearch->new( |
| 570 | cxn_pool => 'Sniff' |
| 571 | ); |
| 572 | |
| 573 | # max_content_length fixed at 10,000 bytes |
| 574 | $e = Search::Elasticsearch->new( |
| 575 | cxn_pool => 'Sniff', |
| 576 | max_content_length => 10_000 |
| 577 | ); |
| 578 | |
| 579 | =head2 C<gzip> |
| 580 | |
| 581 | Enable Gzip compression of requests to and responses from Elasticsearch |
| 582 | as follows: |
| 583 | |
| 584 | $e = Search::Elasticsearch->new( |
| 585 | gzip => 1 |
| 586 | ); |
| 587 | |
| 588 | =head2 C<deflate> |
| 589 | |
| 590 | Enable Inflate/Deflate compression of requests to and responses from Elasticsearch |
| 591 | as follows: |
| 592 | |
| 593 | $e = Search::Elasticsearch->new( |
| 594 | deflate => 1 |
| 595 | ); |
| 596 | |
| 597 | B<IMPORTANT:> The L</request_timeout>, L</ping_timeout>, L</sniff_timeout>, |
| 598 | and L</sniff_request_timeout> parameters default to values that allow |
| 599 | this module to function with low powered hardware and slow networks. |
| 600 | When you use Elasticsearch in production, you will probably want to reduce |
| 601 | these timeout parameters to values that suit your environment. |
| 602 | |
| 603 | The configuration parameters are as follows: |
| 604 | |
| 605 | =head2 C<request_timeout> |
| 606 | |
| 607 | $e = Search::Elasticsearch->new( |
| 608 | request_timeout => 30 |
| 609 | ); |
| 610 | |
| 611 | How long a normal request (ie not a ping or sniff request) should wait |
| 612 | before throwing a C<Timeout> error. Defaults to C<30> seconds. |
| 613 | |
| 614 | B<Note:> In production, no CRUD or search request should take 30 seconds to run, |
| 615 | although admin tasks like C<upgrade()>, C<optimize()>, or snapshot C<create()> |
| 616 | may take much longer. A more reasonable value for production would be |
| 617 | C<10> seconds or lower. |
| 618 | |
| 619 | =head2 C<ping_timeout> |
| 620 | |
| 621 | $e = Search::Elasticsearch->new( |
| 622 | ping_timeout => 2 |
| 623 | ); |
| 624 | |
| 625 | How long a ping request should wait before throwing a C<Timeout> error. |
| 626 | Defaults to C<2> seconds. The L<Search::Elasticsearch::CxnPool::Static> module |
| 627 | pings nodes on first use, after any failure, and periodically to ensure |
| 628 | that nodes are healthy. The C<ping_timeout> should be long enough to allow |
| 629 | nodes respond in time, but not so long that sick nodes cause delays. |
| 630 | A reasonable value for use in production on reasonable hardware |
| 631 | would be C<0.3>-C<1> seconds. |
| 632 | |
| 633 | =head2 C<dead_timeout> |
| 634 | |
| 635 | $e = Search::Elasticsearch->new( |
| 636 | dead_timeout => 60 |
| 637 | ); |
| 638 | |
| 639 | How long a Cxn should be considered to be I<dead> (not used to serve requests), |
| 640 | before it is retried. The default is C<60> seconds. This value is increased |
| 641 | by powers of 2 for each time a request fails. In other words, the delay |
| 642 | after each failure is as follows: |
| 643 | |
| 644 | Failure Delay |
| 645 | 1 60 * 1 = 60 seconds |
| 646 | 2 60 * 2 = 120 seconds |
| 647 | 3 60 * 4 = 240 seconds |
| 648 | 4 60 * 8 = 480 seconds |
| 649 | 5 60 * 16 = 960 seconds |
| 650 | |
| 651 | =head2 C<max_dead_timeout> |
| 652 | |
| 653 | $e = Search::Elasticsearch->new( |
| 654 | max_dead_timeout => 3600 |
| 655 | ); |
| 656 | |
| 657 | The maximum delay that should be applied to a failed node. If the |
| 658 | L</dead_timeout> calculation results in a delay greater than |
| 659 | C<max_dead_timeout> (default C<3,600> seconds) then the C<max_dead_timeout> |
| 660 | is used instead. In other words, dead nodes will be retried at least once |
| 661 | every hour by default. |
| 662 | |
| 663 | =head2 C<sniff_request_timeout> |
| 664 | |
| 665 | $e = Search::Elasticsearch->new( |
| 666 | sniff_request_timeout => 2 |
| 667 | ); |
| 668 | |
| 669 | How long a sniff request should wait before throwing a C<Timeout> error. |
| 670 | Defaults to C<2> seconds. A reasonable value for production would be |
| 671 | C<0.5>-C<2> seconds. |
| 672 | |
| 673 | =head2 C<sniff_timeout> |
| 674 | |
| 675 | $e = Search::Elasticsearch->new( |
| 676 | sniff_timeout => 1 |
| 677 | ); |
| 678 | |
| 679 | How long the node being sniffed should wait for responses from other nodes |
| 680 | before responding to the client. Defaults to C<1> second. A reasonable |
| 681 | value in production would be C<0.3>-C<1> seconds. |
| 682 | |
| 683 | B<Note:> The C<sniff_timeout> is distinct from the L</sniff_request_timeout>. |
| 684 | For example, let's say you have a cluster with 5 nodes, 2 of which are |
| 685 | unhealthy (taking a long time to respond): |
| 686 | |
| 687 | =over |
| 688 | |
| 689 | =item * |
| 690 | |
| 691 | If you sniff an unhealthy node, the request will throw a C<Timeout> error |
| 692 | after C<sniff_request_timeout> seconds. |
| 693 | |
| 694 | =item * |
| 695 | |
| 696 | If you sniff a healthy node, it will gather responses from the other nodes, |
| 697 | and give up after C<sniff_timeout> seconds, returning just the information it |
| 698 | has managed to gather from the healthy nodes. |
| 699 | |
| 700 | =back |
| 701 | |
| 702 | B<NOTE:> The C<sniff_request_timeout> must be longer than the C<sniff_timeout> |
| 703 | to ensure that you get information about healthy nodes from the cluster. |
| 704 | |
| 705 | =head2 C<handle_args> |
| 706 | |
| 707 | Any default arguments which should be passed when creating a new instance of |
| 708 | the class which handles the network transport, eg L<HTTP::Tiny>. |
| 709 | |
| 710 | =head2 C<default_qs_params> |
| 711 | |
| 712 | $e = Search::Elasticsearch->new( |
| 713 | default_qs_params => { |
| 714 | session_key => 'my_session_key' |
| 715 | } |
| 716 | ); |
| 717 | |
| 718 | Any values passed to C<default_qs_params> will be added to the query string |
| 719 | of every request. Also see L<Search::Elasticsearch::Role::Cxn::HTTP/default_headers()>. |
| 720 | |
| 721 | =head1 METHODS |
| 722 | |
| 723 | None of the methods listed below are useful to the user. They are |
| 724 | documented for those who are writing alternative implementations only. |
| 725 | |
| 726 | =head2 C<scheme()> |
| 727 | |
| 728 | $scheme = $cxn->scheme; |
| 729 | |
| 730 | Returns the scheme of the connection, ie C<http> or C<https>. |
| 731 | |
| 732 | =head2 C<is_https()> |
| 733 | |
| 734 | $bool = $cxn->is_https; |
| 735 | |
| 736 | Returns C<true> or C<false> depending on whether the C</scheme()> is C<https> |
| 737 | or not. |
| 738 | |
| 739 | =head2 C<userinfo()> |
| 740 | |
| 741 | $userinfo = $cxn->userinfo |
| 742 | |
| 743 | Returns the username and password of the cxn, if any, eg C<"user:pass">. |
| 744 | If C<userinfo> is provided, then a Basic Authorization header is added |
| 745 | to each request. |
| 746 | |
| 747 | =head2 C<default_headers()> |
| 748 | |
| 749 | $headers = $cxn->default_headers |
| 750 | |
| 751 | The default headers that are passed with each request. This includes |
| 752 | the C<Accept-Encoding> header if C</deflate> is true, and the C<Authorization> |
| 753 | header if C</userinfo> has a value. |
| 754 | Also see L<Search::Elasticsearch::Role::Cxn/default_qs_params>. |
| 755 | |
| 756 | =head2 C<max_content_length()> |
| 757 | |
| 758 | $int = $cxn->max_content_length; |
| 759 | |
| 760 | Returns the maximum length in bytes that the HTTP body can have. |
| 761 | |
| 762 | =head2 C<build_uri()> |
| 763 | |
| 764 | $uri = $cxn->build_uri({ path => '/_search', qs => { size => 10 }}); |
| 765 | |
| 766 | Returns the HTTP URI to use for a particular request, combining the passed |
| 767 | in C<path> parameter with any defined C<path_prefix>, and adding the |
| 768 | query-string parameters. |
| 769 | |
| 770 | =head1 METHODS |
| 771 | |
| 772 | None of the methods listed below are useful to the user. They are |
| 773 | documented for those who are writing alternative implementations only. |
| 774 | |
| 775 | =head2 C<host()> |
| 776 | |
| 777 | $host = $cxn->host; |
| 778 | |
| 779 | The value of the C<host> parameter, eg C<search.domain.com>. |
| 780 | |
| 781 | =head2 C<port()> |
| 782 | |
| 783 | $port = $cxn->port; |
| 784 | |
| 785 | The value of the C<port> parameter, eg C<9200>. |
| 786 | |
| 787 | =head2 C<uri()> |
| 788 | |
| 789 | $uri = $cxn->uri; |
| 790 | |
| 791 | A L<URI> object representing the node, eg C<https://search.domain.com:9200/path>. |
| 792 | |
| 793 | =head2 C<is_dead()> |
| 794 | |
| 795 | $bool = $cxn->is_dead |
| 796 | |
| 797 | Is the current node marked as I<dead>. |
| 798 | |
| 799 | =head2 C<is_live()> |
| 800 | |
| 801 | $bool = $cxn->is_live |
| 802 | |
| 803 | Is the current node marked as I<live>. |
| 804 | |
| 805 | =head2 C<next_ping()> |
| 806 | |
| 807 | $time = $cxn->next_ping($time) |
| 808 | |
| 809 | Get/set the time for the next scheduled ping. If zero, no ping is scheduled |
| 810 | and the cxn is considered to be alive. If -1, a ping is scheduled before |
| 811 | the next use. |
| 812 | |
| 813 | =head2 C<ping_failures()> |
| 814 | |
| 815 | $num = $cxn->ping_failures($num) |
| 816 | |
| 817 | The number of times that a cxn has been marked as dead. |
| 818 | |
| 819 | =head2 C<mark_dead()> |
| 820 | |
| 821 | $cxn->mark_dead |
| 822 | |
| 823 | Mark the cxn as I<dead>, set L</next_ping()> and increment L</ping_failures()>. |
| 824 | |
| 825 | =head2 C<mark_live()> |
| 826 | |
| 827 | Mark the cxn as I<live>, set L</next_ping()> and L</ping_failures()> to zero. |
| 828 | |
| 829 | =head2 C<force_ping()> |
| 830 | |
| 831 | Set L</next_ping()> to -1 (ie before next use) and L</ping_failures()> to zero. |
| 832 | |
| 833 | =head2 C<pings_ok()> |
| 834 | |
| 835 | $bool = $cxn->pings_ok |
| 836 | |
| 837 | Try to ping the node and call L</mark_live()> or L</mark_dead()> depending on |
| 838 | the success or failure of the ping. |
| 839 | |
| 840 | =head2 C<sniff()> |
| 841 | |
| 842 | $response = $cxn->sniff; |
| 843 | |
| 844 | Send a sniff request to the node and return the response. |
| 845 | |
| 846 | =head2 C<process_response()> |
| 847 | |
| 848 | ($code,$result) = $cxn->process_response($params, $code, $msg, $body ); |
| 849 | |
| 850 | Processes the response received from an Elasticsearch node and either |
| 851 | returns the HTTP status code and the response body (deserialized from JSON) |
| 852 | or throws an error of the appropriate type. |
| 853 | |
| 854 | The C<$params> are the original params passed to |
| 855 | L<Search::Elasticsearch::Transport/perform_request()>, the C<$code> is the HTTP |
| 856 | status code, the C<$msg> is the error message returned by the backend |
| 857 | library and the C<$body> is the HTTP response body returned by |
| 858 | Elasticsearch. |
| 859 | |
| 860 | =head1 AUTHOR |
| 861 | |
| 862 | Enrico Zimuel <[email protected]> |
| 863 | |
| 864 | =head1 COPYRIGHT AND LICENSE |
| 865 | |
| 866 | This software is Copyright (c) 2024 by Elasticsearch BV. |
| 867 | |
| 868 | This is free software, licensed under: |
| 869 | |
| 870 | The Apache License, Version 2.0, January 2004 |
| 871 | |
| 872 | =cut |