Skip to content

Commit

Permalink
Merge pull request #2 from 007hacky007/timeout
Browse files Browse the repository at this point in the history
Added possibility to define gRPC timeout
  • Loading branch information
matthi4s authored Apr 30, 2020
2 parents ee1cf7b + 8bac25d commit 72a4575
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Client implements ClientInterface
*/
protected $password;

/**
* @var int
*/
protected $timeout;

/**
* @var string
*/
Expand All @@ -64,12 +69,14 @@ class Client implements ClientInterface
* @param string $hostname
* @param bool $username
* @param bool $password
* @param int $timeout in microseconds, default 1 second
*/
public function __construct($hostname = "localhost:2379", $username = false, $password = false)
public function __construct($hostname = "localhost:2379", $username = false, $password = false, $timeout = 1000000)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->timeout = $timeout;
}

/**
Expand Down Expand Up @@ -108,7 +115,7 @@ public function put(string $key, $value, bool $prevKv = false, int $lease = 0, b
$request->setLease($lease);

/** @var PutResponse $response */
list($response, $status) = $client->Put($request, $this->getMetaData())->wait();
list($response, $status) = $client->Put($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

if ($prevKv) {
Expand All @@ -131,7 +138,7 @@ public function get(string $key)
$request->setKey($key);

/** @var RangeResponse $response */
list($response, $status) = $client->Range($request, $this->getMetaData())->wait();
list($response, $status) = $client->Range($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

$field = $response->getKvs();
Expand All @@ -158,7 +165,7 @@ public function delete(string $key)
$request->setKey($key);

/** @var DeleteRangeResponse $response */
list($response, $status) = $client->DeleteRange($request, $this->getMetaData())->wait();
list($response, $status) = $client->DeleteRange($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

if ($response->getDeleted() > 0) {
Expand Down Expand Up @@ -251,7 +258,7 @@ protected function requestIf(string $key, $previousValue, RequestOp $requestOper
}

/** @var TxnResponse $response */
list($response, $status) = $client->Txn($request, $this->getMetaData())->wait();
list($response, $status) = $client->Txn($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

if ($returnNewValueOnFail && !$response->getSucceeded()) {
Expand Down Expand Up @@ -320,7 +327,7 @@ protected function getAuthenticationToken(): string
$request->setPassword($this->password);

/** @var AuthenticateResponse $response */
list($response, $status) = $client->Authenticate($request)->wait();
list($response, $status) = $client->Authenticate($request, [], $this->getOptions())->wait();
$this->validateStatus($status);

$this->token = $response->getToken();
Expand All @@ -345,6 +352,17 @@ protected function getMetaData($metadata = []): array
return $metadata;
}

/**
* Add timeout
*
* @param array $options
* @return array
*/
protected function getOptions($options = []): array
{
return array_merge(["timeout" => $this->timeout], $options);
}

/**
* @param $status
* @throws InvalidResponseStatusCodeException
Expand Down

0 comments on commit 72a4575

Please sign in to comment.