-
Notifications
You must be signed in to change notification settings - Fork 16
/
ElasticsearchResurrect.php
55 lines (49 loc) · 1.64 KB
/
ElasticsearchResurrect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Elastic Transport
*
* @link https://github.com/elastic/elastic-transport-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types=1);
namespace Elastic\Transport\NodePool\Resurrect;
use Elastic\Transport\NodePool\Node;
use Exception;
use Psr\Http\Client\ClientInterface;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
class ElasticsearchResurrect implements ResurrectInterface
{
protected ClientInterface $client;
protected RequestFactoryInterface $requestFactory;
public function ping(Node $node): bool
{
$request = $this->getRequestFactory()->createRequest("HEAD", $node->getUri());
try {
$response = $this->getClient()->sendRequest($request);
return $response->getStatusCode() === 200;
} catch (Exception $e) {
return false;
}
}
public function getClient(): ClientInterface
{
if (empty($this->client)) {
$this->client = Psr18ClientDiscovery::find();
}
return $this->client;
}
public function getRequestFactory(): RequestFactoryInterface
{
if (empty($this->requestFactory)) {
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
}
return $this->requestFactory;
}
}