简体   繁体   中英

set connect_timeout of elasticsearch php client

i want to configure aa small timeout between my elasticsearch php client to the my elasticsearch server.

i tried to pass some parameters to the guzzle client but it seems this doesn't work. here is the code:

$params = array();
$params['hosts'] = $hosts;
$params['guzzleOptions']['connect_timeout'] = 2.0;
$params['guzzleOptions']['timeout'] = 2.0;
$this->elastica_obj = new Elasticsearch\Client($params);

i searched and found that the problem might occured because the timeout is set in the cURL layer (that is lower than the guzzle) ( Limit connecting time with Guzzle HTTP PHP client )

i guess i need somehow to set CURLOPT_CONNECTTIMEOUT_MS parameter to the value i want (2000ms) but i don't see any good way to pass it through the elasticsearch php client.

Does someone knows how to do it?

Thanks Zack, I tried it but it doesn't work.

I debugged the client and the way the parameters are being pass from through the guzzle to the curl handle.

the way i find to accomplish it is to pass this parameter to the Elasticsearch client

$params['guzzleOptions']['curl.options'][CURLOPT_CONNECTTIMEOUT] = 2.0;  // this applies 2 seconds connection_timeout

hope it is helping :)

Niv

For the latest version 2.x it's done in a different way. Citing Zach :

In ES-PHP 2.x the timeout is now specified per-request. See the documentation here: https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_per_request_configuration.html#_curl_timeouts

$client = ClientBuilder::create()->build();

$params = [
    'index' => 'test',
    'type' => 'test',
    'id' => 1,
    'client' => [
        'timeout' => 10,        // ten second timeout
        'connect_timeout' => 10
    ]
];
$response = $client->get($params);

Though, it is not possible to do it on global level. Follow this issue for updates.

Assuming you mean Elasticsearch-PHP client (and not Elastica):

The guzzleOptions parameter accepts any Guzzle parameter, and follows the same array syntax that Guzzle uses. So you need to do:

$params = array();
$params['hosts'] = $hosts;
$params['guzzleOptions']['command.request_options']['connect_timeout'] = 2.0;
$params['guzzleOptions']['command.request_options']['timeout'] = 2.0;
$this->elastica_obj = new Elasticsearch\Client($params);

This will apply a 2s timeout to all requests sent through the client

There is a "shortcut" timeout parameter that is supposed to apply to all connection types (Guzzle, CurlMultiConnection, etc)...but I'm looking through the code now and I dont think it actually works for Guzzle. I'll open a ticket.

Since elasticsearch/elasticsearch v5.1.2, you also can use setConnectionParams() method which applies to all requests

use GuzzleHttp\RequestOptions;
use Elasticsearch\ClientBuilder;

ClientBuilder::create()
    ->setConnectionParams([
        'client' => [
            RequestOptions::TIMEOUT => 10,
            RequestOptions::CONNECT_TIMEOUT => 10,
        ],
    ])
    ->build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM