| 1 | package OpenSearch::Client::Transport::TryCatch; | ||
| 2 | $OpenSearch::Client::Transport::TryCatch::VERSION = '3.007011'; | ||
| 3 | use Moo; | ||
| 4 | |||
| 5 | use URI(); | ||
| 6 | use Time::HiRes qw(time); | ||
| 7 | use Try::Tiny; | ||
| 8 | use OpenSearch::Client::Util qw(upgrade_error); | ||
| 9 | use namespace::clean; | ||
| 10 | |||
| 11 | with 'OpenSearch::Client::Role::Is_Sync', | ||
| 12 | 'OpenSearch::Client::Role::Transport'; | ||
| 13 | |||
| 14 | #=================================== | ||
| 15 | sub perform_request { | ||
| 16 | #=================================== | ||
| 17 | my $self = shift; | ||
| 18 | my $params = $self->tidy_request(@_); | ||
| 19 | my $pool = $self->cxn_pool; | ||
| 20 | my $logger = $self->logger; | ||
| 21 | |||
| 22 | my ( $code, $response, $cxn, $error ); | ||
| 23 | |||
| 24 | try { | ||
| 25 | $cxn = $pool->next_cxn; | ||
| 26 | my $start = time(); | ||
| 27 | $logger->trace_request( $cxn, $params ); | ||
| 28 | |||
| 29 | ( $code, $response ) = $cxn->perform_request($params); | ||
| 30 | $pool->request_ok($cxn); | ||
| 31 | $logger->trace_response( $cxn, $code, $response, time() - $start ); | ||
| 32 | } | ||
| 33 | catch { | ||
| 34 | $error = upgrade_error( | ||
| 35 | $_, | ||
| 36 | { request => $params, | ||
| 37 | status_code => $code, | ||
| 38 | body => $response | ||
| 39 | } | ||
| 40 | ); | ||
| 41 | }; | ||
| 42 | |||
| 43 | if ($error) { | ||
| 44 | if ( $pool->request_failed( $cxn, $error ) ) { | ||
| 45 | $logger->debugf( "[%s] %s", $cxn->stringify, "$error" ); | ||
| 46 | $logger->info('Retrying request on a new cxn'); | ||
| 47 | return $self->perform_request($params); | ||
| 48 | } | ||
| 49 | |||
| 50 | $logger->trace_error( $cxn, $error ); | ||
| 51 | $response = undef; | ||
| 52 | } | ||
| 53 | |||
| 54 | if (wantarray) { | ||
| 55 | return( $response, $error ); | ||
| 56 | } else { | ||
| 57 | return ( $response ) ? $response : $error; | ||
| 58 | } | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | __END__ |