Skip to content

Commit

Permalink
added possibility to configurate guzzle options for requests and more…
Browse files Browse the repository at this point in the history
… verbose exception message
  • Loading branch information
evsign committed Jan 24, 2019
1 parent 91b11fa commit 8097405
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Exceptions/TransportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 23 additions & 13 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'] ?? []),
]
);

Expand Down Expand Up @@ -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'] ?? []),
]
);

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]');
Expand Down

0 comments on commit 8097405

Please sign in to comment.