From d74e28b79c2639daf9977b22d181a6df5296969b Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 12 Jul 2022 12:11:48 -0400 Subject: [PATCH 1/2] Add connection params timeout to prevent infinite timeouts --- config/elastic.client.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/config/elastic.client.php b/config/elastic.client.php index f627eba..f1ab21e 100644 --- a/config/elastic.client.php +++ b/config/elastic.client.php @@ -3,5 +3,12 @@ return [ 'hosts' => [ env('ELASTIC_HOST', 'localhost:9200'), - ] + ], + + 'connectionParams' => [ + 'client' => [ + 'timeout' => env('ELASTIC_TIMEOUT'), + 'connect_timeout' => env('ELASTIC_CONNECT_TIMEOUT'), + ], + ], ]; From 28ee86eef95623aa34774a5c27951b294ef10057 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 28 Sep 2023 11:04:13 -0400 Subject: [PATCH 2/2] Use ClientInterface instead of Client --- README.md | 12 +++++++----- src/ClientBuilder.php | 6 +++--- src/ClientBuilderInterface.php | 6 +++--- tests/Unit/ClientBuilderTest.php | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 39627dc..970e4bb 100644 --- a/README.md +++ b/README.md @@ -75,15 +75,17 @@ return [ If you need more control over the client creation, you can create your own client builder: ```php -// see Elastic\Client\ClientBuilder for the reference -class MyClientBuilder implements Elastic\Client\ClientBuilderInterface +use Elastic\Elasticsearch\ClientInterface; +use Elastic\Client\ClientBuilderInterface; + +class MyClientBuilder implements ClientBuilderInterface { - public function default(): Client + public function default(): ClientInterface { // should return a client instance for the default connection } - public function connection(string $name): Client + public function connection(string $name): ClientInterface { // should return a client instance for the connection with the given name } @@ -109,7 +111,7 @@ Use `Elastic\Client\ClientBuilderInterface` to get access to the client instance ```php namespace App\Console\Commands; -use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientInterface; use Elastic\Client\ClientBuilderInterface; use Illuminate\Console\Command; diff --git a/src/ClientBuilder.php b/src/ClientBuilder.php index 572bd1d..ea6584d 100644 --- a/src/ClientBuilder.php +++ b/src/ClientBuilder.php @@ -2,7 +2,7 @@ namespace Elastic\Client; -use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientInterface; use Elastic\Elasticsearch\ClientBuilder as BaseClientBuilder; use ErrorException; @@ -10,7 +10,7 @@ class ClientBuilder implements ClientBuilderInterface { protected array $cache; - public function default(): Client + public function default(): ClientInterface { $name = config('elastic.client.default'); @@ -21,7 +21,7 @@ public function default(): Client return $this->connection($name); } - public function connection(string $name): Client + public function connection(string $name): ClientInterface { if (isset($this->cache[$name])) { return $this->cache[$name]; diff --git a/src/ClientBuilderInterface.php b/src/ClientBuilderInterface.php index 029118a..b0d487a 100644 --- a/src/ClientBuilderInterface.php +++ b/src/ClientBuilderInterface.php @@ -2,11 +2,11 @@ namespace Elastic\Client; -use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientInterface; interface ClientBuilderInterface { - public function default(): Client; + public function default(): ClientInterface; - public function connection(string $name): Client; + public function connection(string $name): ClientInterface; } diff --git a/tests/Unit/ClientBuilderTest.php b/tests/Unit/ClientBuilderTest.php index 54bd5c1..c2c6e14 100644 --- a/tests/Unit/ClientBuilderTest.php +++ b/tests/Unit/ClientBuilderTest.php @@ -3,7 +3,7 @@ namespace Elastic\Client\Tests\Unit; use Elastic\Client\ClientBuilder; -use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientInterface; use ErrorException; use Orchestra\Testbench\TestCase; @@ -51,7 +51,7 @@ public function test_exception_is_thrown_when_building_client_with_non_existing_ $this->clientBuilder->connection('foo'); } - private function assertHost(Client $client, string $host): void + private function assertHost(ClientInterface $client, string $host): void { $transport = $client->getTransport(); $node = $transport->getNodePool()->nextNode();