| 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::Transport::Async; | ||||||||||
| 6 | 50 | $Search::Elasticsearch::Transport::Async::VERSION = '7.30_1'; # TRIAL | |||||||||
| 7 | |||||||||||
| 8 | 50 | $Search::Elasticsearch::Transport::Async::VERSION = '7.301'; | |||||||||
| 9 | 50 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 50 | with 'Search::Elasticsearch::Role::Is_Async', | |||||||||
| 11 | 'Search::Elasticsearch::Role::Transport'; | ||||||||||
| 12 | |||||||||||
| 13 | 50 | ▶ | use Time::HiRes qw(time); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 50 | ▶ | use Search::Elasticsearch::Util qw(upgrade_error); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 50 | ▶ | use Promises qw(deferred); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 50 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | #=================================== | ||||||||||
| 19 | sub perform_request { | ||||||||||
| 20 | #=================================== | ||||||||||
| 21 | 32 | ▶ | my $self = shift; | ||||||||
|
Subroutine
perform_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | 32 | my $params = $self->tidy_request(@_); | |||||||||
| 23 | 32 | my $pool = $self->cxn_pool; | |||||||||
| 24 | 32 | my $logger = $self->logger; | |||||||||
| 25 | |||||||||||
| 26 | 32 | my $deferred = deferred; | |||||||||
| 27 | |||||||||||
| 28 | 32 | my ( $start, $cxn ); | |||||||||
| 29 | $pool->next_cxn | ||||||||||
| 30 | |||||||||||
| 31 | # perform request | ||||||||||
| 32 | ->then( | ||||||||||
| 33 | sub { | ||||||||||
| 34 | 9 | ▶ | $cxn = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 9 | $start = time(); | |||||||||
| 36 | 9 | $cxn->perform_request($params); | |||||||||
| 37 | } | ||||||||||
| 38 | ) | ||||||||||
| 39 | |||||||||||
| 40 | # log request regardless of success/failure | ||||||||||
| 41 | 32 | ▶ | ->finally( sub { $logger->trace_request( $cxn, $params ) } ) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | ->done( | ||||||||||
| 44 | # request succeeded | ||||||||||
| 45 | sub { | ||||||||||
| 46 | 0 | ▶ | my ( $code, $response ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 47 | 0 | $pool->request_ok($cxn); | |||||||||
| 48 | 0 | $logger->trace_response( $cxn, $code, $response, | |||||||||
| 49 | time() - $start ); | ||||||||||
| 50 | 0 | $deferred->resolve($response); | |||||||||
| 51 | }, | ||||||||||
| 52 | |||||||||||
| 53 | # request failed | ||||||||||
| 54 | sub { | ||||||||||
| 55 | 32 | ▶ | my $error = upgrade_error( shift(), { request => $params } ); | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 56 | 32 | ▶ | if ( $pool->request_failed( $cxn, $error ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | # log failed, then retry | ||||||||||
| 59 | 2 | $logger->debugf( "[%s] %s", $cxn->stringify, "$error" ); | |||||||||
| 60 | 2 | $logger->info('Retrying request on a new cxn'); | |||||||||
| 61 | return $self->perform_request($params)->done( | ||||||||||
| 62 | 0 | ▶ | sub { $deferred->resolve(@_) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 2 | ▶ | sub { $deferred->reject(@_) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 2 | ); | |||||||||
| 65 | } | ||||||||||
| 66 | 30 | ▶ | if ($cxn) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 7 | $logger->trace_request( $cxn, $params ); | |||||||||
| 68 | 7 | $logger->trace_error( $cxn, $error ); | |||||||||
| 69 | } | ||||||||||
| 70 | 30 | ▶ | $error->is('NoNodes') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | ? $logger->critical($error) | ||||||||||
| 72 | : $logger->error($error); | ||||||||||
| 73 | 30 | $deferred->reject($error); | |||||||||
| 74 | } | ||||||||||
| 75 | 32 | ); | |||||||||
| 76 | 32 | return $deferred->promise; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 50 | 1; | |||||||||
| 80 | |||||||||||
| 81 | #ABSTRACT: Provides async interface between the client class and the Elasticsearch cluster | ||||||||||
| 82 | |||||||||||
| 83 | __END__ | ||||||||||