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