set connect_timeout of elasticsearch php client

半城伤御伤魂 提交于 2019-12-24 17:25:22

问题


i want to configure a a 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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/25491110/set-connect-timeout-of-elasticsearch-php-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!