| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| 3 | # See the LICENSE file in the project root for more information | ||
| 4 | |||
| 5 | package Search::Elasticsearch::Cxn::Hijk; | ||
| 6 | $Search::Elasticsearch::Cxn::Hijk::VERSION = '7.30_1'; # TRIAL | ||
| 7 | |||
| 8 | $Search::Elasticsearch::Cxn::Hijk::VERSION = '7.301'; | ||
| 9 | use Moo; | ||
| 10 | with 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync'; | ||
| 11 | |||
| 12 | use Hijk 0.27; | ||
| 13 | use Try::Tiny; | ||
| 14 | use namespace::clean; | ||
| 15 | |||
| 16 | has 'connect_timeout' => ( is => 'ro', default => 2 ); | ||
| 17 | has '_socket_cache' => ( is => 'rw', default => sub { {} } ); | ||
| 18 | |||
| 19 | my $Cxn_Error = qr/ Connection.(?:timed.out|re(?:set|fused)) | ||
| 20 | | connect:.timeout | ||
| 21 | | Host.is.down | ||
| 22 | | No.route.to.host | ||
| 23 | | temporarily.unavailable | ||
| 24 | | Socket.is.not.connected | ||
| 25 | | Broken.pipe | ||
| 26 | | Failed.to | ||
| 27 | | select\(2\) | ||
| 28 | | connect\(2\) | ||
| 29 | | send.error | ||
| 30 | | zombie.error | ||
| 31 | /x; | ||
| 32 | |||
| 33 | #=================================== | ||
| 34 | sub perform_request { | ||
| 35 | #=================================== | ||
| 36 | my ( $self, $params ) = @_; | ||
| 37 | my $uri = $self->build_uri($params); | ||
| 38 | my $method = $params->{method}; | ||
| 39 | my $cache = $self->_socket_cache; | ||
| 40 | |||
| 41 | my %args = ( | ||
| 42 | host => $uri->host, | ||
| 43 | port => $uri->port, | ||
| 44 | socket_cache => $self->_socket_cache, | ||
| 45 | connect_timeout => $self->request_timeout, | ||
| 46 | read_timeout => $params->{timeout} || $self->request_timeout, | ||
| 47 | method => $method, | ||
| 48 | path => $uri->path, | ||
| 49 | query_string => $uri->query, | ||
| 50 | %{ $self->handle_args } | ||
| 51 | ); | ||
| 52 | if ( defined $params->{data} ) { | ||
| 53 | $args{body} = $params->{data}; | ||
| 54 | $args{head} = [ 'Content-Type', $params->{mime_type} ]; | ||
| 55 | push @{ $args{head} }, ( 'Content-Encoding', $params->{encoding} ) | ||
| 56 | if $params->{encoding}; | ||
| 57 | } | ||
| 58 | |||
| 59 | push @{ $args{head} }, %{$self->default_headers} | ||
| 60 | if %{$self->default_headers}; | ||
| 61 | |||
| 62 | my $response; | ||
| 63 | try { | ||
| 64 | local $SIG{PIPE} = sub { die $! }; | ||
| 65 | $response = Hijk::request( \%args ); | ||
| 66 | } | ||
| 67 | catch { | ||
| 68 | $response = { | ||
| 69 | status => 500, | ||
| 70 | error => $_ || 'Unknown error' | ||
| 71 | }; | ||
| 72 | }; | ||
| 73 | |||
| 74 | my $head = $response->{head} || {}; | ||
| 75 | my %head = map { lc($_) => $head->{$_} } keys %$head; | ||
| 76 | |||
| 77 | return $self->process_response( | ||
| 78 | $params, # request | ||
| 79 | $response->{status} || 500, # code | ||
| 80 | $response->{error}, # msg | ||
| 81 | $response->{body} | ||
| 82 | || $response->{error_message} | ||
| 83 | || $response->{errno_string}, # body | ||
| 84 | \%head # headers | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | #=================================== | ||
| 89 | sub clear_handle { | ||
| 90 | #=================================== | ||
| 91 | my $self = shift; | ||
| 92 | $self->_socket_cache( {} ); | ||
| 93 | } | ||
| 94 | #=================================== | ||
| 95 | sub error_from_text { | ||
| 96 | #=================================== | ||
| 97 | local $_ = $_[2]; | ||
| 98 | |||
| 99 | no warnings 'numeric'; | ||
| 100 | my $type | ||
| 101 | = 0 + $_ & Hijk::Error::TIMEOUT ? 'Timeout' | ||
| 102 | : 0 + $_ & Hijk::Error::CANNOT_RESOLVE ? 'Cxn' | ||
| 103 | : 0 + $_ & Hijk::Error::REQUEST_ERROR ? 'Cxn' | ||
| 104 | : 0 + $_ & Hijk::Error::RESPONSE_ERROR ? 'Cxn' | ||
| 105 | : /Connection reset by peer/ ? 'ContentLength' | ||
| 106 | : m/$Cxn_Error/ ? 'Cxn' | ||
| 107 | : 'Request'; | ||
| 108 | |||
| 109 | if ( $type eq 'Cxn' || $type eq 'Timeout' ) { | ||
| 110 | %{ $_[0]->_socket_cache } = (); | ||
| 111 | } | ||
| 112 | return $type; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | # ABSTRACT: A Cxn implementation which uses Hijk | ||
| 118 | |||
| 119 | __END__ |