File Coverage

blib/lib/Elasticsearch/Transport.pm
Criterion Covered Total %
statement 40 40 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 56 56 100.0


line stmt bran cond sub pod time code
1             package Elasticsearch::Transport;
2             $Elasticsearch::Transport::VERSION = '1.05';
3 42     42   36424 use Moo;
  42         126  
  42         384  
4              
5 42     42   17030 use URI();
  42         108  
  42         1132  
6 42     42   382 use Time::HiRes qw(time);
  42         111  
  42         494  
7 42     42   9944 use Try::Tiny;
  42         294  
  42         5664  
8 42     42   286 use Elasticsearch::Util qw(upgrade_error);
  42         91  
  42         480  
9 42     42   15044 use namespace::clean;
  42         96  
  42         421  
10              
11             with 'Elasticsearch::Role::Is_Sync', 'Elasticsearch::Role::Transport';
12              
13             #===================================
14             sub perform_request {
15             #===================================
16 161     161 1 13083 my $self = shift;
17 161         728 my $params = $self->tidy_request(@_);
18 161         478 my $pool = $self->cxn_pool;
19 161         413 my $logger = $self->logger;
20              
21 161         219 my ( $code, $response, $cxn, $error );
22              
23             try {
24 161     161   9313 $cxn = $pool->next_cxn;
25 148         1437 my $start = time();
26 148         678 $logger->trace_request( $cxn, $params );
27              
28 148         14686 ( $code, $response ) = $cxn->perform_request($params);
29 114         752 $pool->request_ok($cxn);
30 114         990 $logger->trace_response( $cxn, $code, $response, time() - $start );
31             }
32             catch {
33 47     47   1077 $error = upgrade_error(
34             $_,
35             { request => $params,
36             status_code => $code,
37             body => $response
38             }
39             );
40 161         1527 };
41              
42 161 100       14471 if ($error) {
43 47 100       480 if ( $pool->request_failed( $cxn, $error ) ) {
44 19         678 $logger->debugf( "[%s] %s", $cxn->stringify, "$error" );
45 19         1240 $logger->info('Retrying request on a new cxn');
46 19         3160 return $self->perform_request($params);
47             }
48              
49 28         198 $logger->trace_error( $cxn, $error );
50 28         2034 delete $error->{vars}{body};
51 28 100       120 $error->is('NoNodes')
52             ? $logger->throw_critical($error)
53             : $logger->throw_error($error);
54             }
55              
56 114         885 return $response;
57             }
58              
59             1;
60              
61             #ABSTRACT: Provides interface between the client class and the Elasticsearch cluster
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Elasticsearch::Transport - Provides interface between the client class and the Elasticsearch cluster
72              
73             =head1 VERSION
74              
75             version 1.05
76              
77             =head1 DESCRIPTION
78              
79             The Transport class manages the request cycle. It receives parsed requests
80             from the (user-facing) client class, and tries to execute the request on a
81             node in the cluster, retrying a request if necessary.
82              
83             This class does L<Elasticsearch::Role::Transport> and
84             L<Elasticsearch::Role::Is_Sync>.
85              
86             =head1 CONFIGURATION
87              
88             =head2 C<send_get_body_as>
89              
90             $e = Elasticsearch->new(
91             send_get_body_as => 'POST'
92             );
93              
94             Certain endpoints like L<Elasticsearch::Client::Direct/search()> default to
95             using a C<GET> method, even when they include a request body. Some proxy
96             servers do not support C<GET> requests with a body. To work around this,
97             the C<send_get_body_as> parameter accepts the following:
98              
99             =over
100              
101             =item * C<GET>
102              
103             The default. Request bodies are sent as C<GET> requests.
104              
105             =item * C<POST>
106              
107             The method is changed to C<POST> when a body is present.
108              
109             =item * C<source>
110              
111             The body is encoded as JSON and added to the query string as the C<source>
112             parameter. This has the advantage of still being a C<GET> request (for those
113             filtering on request method) but has the disadvantage of being restricted
114             in size. The limit depends on the proxies between the client and
115             Elasticsearch, but usually is around 4kB.
116              
117             =back
118              
119             =head1 METHODS
120              
121             =head2 C<perform_request()>
122              
123             Raw requests can be executed using the transport class as follows:
124              
125             $result = $e->transport->perform_request(
126             method => 'POST',
127             path => '/_search',
128             qs => { from => 0, size => 10 },
129             body => {
130             query => {
131             match => {
132             title => "Elasticsearch clients"
133             }
134             }
135             }
136             );
137              
138             Other than the C<method>, C<path>, C<qs> and C<body> parameters, which
139             should be self-explanatory, it also accepts:
140              
141             =over
142              
143             =item C<ignore>
144              
145             The HTTP error codes which should be ignored instead of throwing an error,
146             eg C<404 NOT FOUND>:
147              
148             $result = $e->transport->perform_request(
149             method => 'GET',
150             path => '/index/type/id'
151             ignore => [404],
152             );
153              
154             =item C<serialize>
155              
156             Whether the C<body> should be serialized in the standard way (as plain
157             JSON) or using the special I<bulk> format: C<"std"> or C<"bulk">.
158              
159             =back
160              
161             =head1 AUTHOR
162              
163             Clinton Gormley <drtech@cpan.org>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is Copyright (c) 2014 by Elasticsearch BV.
168              
169             This is free software, licensed under:
170              
171             The Apache License, Version 2.0, January 2004
172              
173             =cut