From 80974057e67ed9e68f8d96949dc69695f4812ed2 Mon Sep 17 00:00:00 2001 From: evsign Date: Thu, 24 Jan 2019 15:20:11 +0300 Subject: [PATCH] added possibility to configurate guzzle options for requests and more verbose exception message --- src/Exceptions/TransportException.php | 4 +-- src/Transport/HttpTransport.php | 36 +++++++++++++++++---------- tests/HttpTransportTest.php | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Exceptions/TransportException.php b/src/Exceptions/TransportException.php index 684faad..2094409 100644 --- a/src/Exceptions/TransportException.php +++ b/src/Exceptions/TransportException.php @@ -11,9 +11,9 @@ */ class TransportException extends \Exception { - public static function connectionError(Server $server) + public static function connectionError(Server $server, $reasonMessage) { - return new static('Can\'t connect to the server ['.$server->getHost().':'.$server->getPort().']'); + return new static('Can\'t connect to the server ['.$server->getHost().':'.$server->getPort().'] with error: ['.$reasonMessage.']'); } public static function serverReturnedError($exception, Query $query) diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index d30155a..a070ef6 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -30,22 +30,34 @@ class HttpTransport implements TransportInterface protected $httpClient; /** - * Connection timeout. + * Array with two keys (read and write) with guzzle options for corresponding requests. * - * @var float + * [ + * 'read' => [ + * 'timeout' => 50, + * 'connect_timeout => 10, + * ], + * 'write' => [ + * 'debug' => true, + * 'timeout' => 100, + * ], + * ] + * + * @var array */ - protected $connectionTimeout = 5.0; + private $options; /** * HttpTransport constructor. * * @param Client $client - * @param float $connectionTimeout + * @param array $options */ - public function __construct(Client $client = null, float $connectionTimeout = 5.0) + public function __construct(Client $client = null, array $options = []) { $this->setClient($client); - $this->connectionTimeout = $connectionTimeout; + + $this->options = $options; } /** @@ -135,10 +147,9 @@ public function write(array $queries, int $concurrency = 5) : array $queryResult[$index] = true; }, 'rejected' => $this->parseReason($query), - 'options' => [ - 'connect_timeout' => $this->connectionTimeout, + 'options' => array_merge([ 'expect' => false - ], + ], $this->options['write'] ?? []), ] ); @@ -222,10 +233,9 @@ public function read(array $queries, int $concurrency = 5) : array $this->parseReason($query)($response); }, - 'options' => [ - 'connect_timeout' => $this->connectionTimeout, + 'options' => array_merge([ 'expect' => false - ], + ], $this->options['read'] ?? []), ] ); @@ -307,7 +317,7 @@ protected function parseReason(Query $query) $response = $reason->getResponse(); if (is_null($response)) { - throw TransportException::connectionError($query->getServer()); + throw TransportException::connectionError($query->getServer(), $reason->getMessage()); } else { throw TransportException::serverReturnedError($reason, $query); } diff --git a/tests/HttpTransportTest.php b/tests/HttpTransportTest.php index d8e560b..e10d3c5 100644 --- a/tests/HttpTransportTest.php +++ b/tests/HttpTransportTest.php @@ -333,7 +333,7 @@ protected function getTempFileName(): string public function testConnectionError() { - $transport = new HttpTransport(null, 0.1); + $transport = new HttpTransport(null, ['read' => ['connect_timeout' => 0.1]]); $this->expectException(TransportException::class); $this->expectExceptionMessage('Can\'t connect to the server [KHGIUYhakljsfnk:8123]');