Skip to content

Commit 5f99582

Browse files
authored
Merge pull request #6 from 007hacky007/etcd-node-failover
Etcd node failover
2 parents 29da9dd + b6c9171 commit 5f99582

File tree

6 files changed

+395
-10
lines changed

6 files changed

+395
-10
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ $shardedClient = new Aternos\Etcd\ShardedClient($clients);
6969
$shardedClient->put("key", "value");
7070
$shardedClient->get("key");
7171
```
72+
73+
### Failover client
74+
75+
- automatically and transparently fails-over in case etcd host fails
76+
```php
77+
<?php
78+
79+
$clients = [
80+
new Aternos\Etcd\Client("hostA:2379"),
81+
new Aternos\Etcd\Client("hostB:2379"),
82+
new Aternos\Etcd\Client("hostC:2379")
83+
];
84+
$failoverClient = new Aternos\Etcd\FailoverClient($clients);
85+
86+
// set 60 seconds as a hold-off period between another connection attempt to the failing host
87+
// default is 120 seconds
88+
// failing host is being remembered within FailoverClient object instance
89+
$failoverClient->setHoldoffTime(60);
90+
$failoverClient->put("key", "value");
91+
$failoverClient->get("key");
92+
```

src/Client.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Aternos\Etcd;
44

5+
use Aternos\Etcd\Exception\InvalidLeaseException;
6+
use Aternos\Etcd\Exception\NoResponseException;
57
use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException;
68
use Aternos\Etcd\Exception\Status\ResponseStatusCodeExceptionFactory;
79
use Etcdserverpb\AuthClient;
@@ -27,7 +29,6 @@
2729
use Etcdserverpb\ResponseOp;
2830
use Etcdserverpb\TxnRequest;
2931
use Etcdserverpb\TxnResponse;
30-
use Exception;
3132
use Grpc\ChannelCredentials;
3233
use Mvccpb\KeyValue;
3334

@@ -197,7 +198,6 @@ public function delete(string $key)
197198
* @param bool $returnNewValueOnFail
198199
* @return bool|string
199200
* @throws InvalidResponseStatusCodeException
200-
* @throws \Exception
201201
*/
202202
public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false)
203203
{
@@ -217,7 +217,6 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew
217217
* @param bool $returnNewValueOnFail
218218
* @return bool|string
219219
* @throws InvalidResponseStatusCodeException
220-
* @throws \Exception
221220
*/
222221
public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false)
223222
{
@@ -270,8 +269,7 @@ public function revokeLeaseID(int $leaseID)
270269
*
271270
* @param int $leaseID
272271
* @return int lease TTL
273-
* @throws InvalidResponseStatusCodeException
274-
* @throws Exception
272+
* @throws InvalidResponseStatusCodeException|NoResponseException|InvalidLeaseException
275273
*/
276274
public function refreshLease(int $leaseID)
277275
{
@@ -285,18 +283,21 @@ public function refreshLease(int $leaseID)
285283
/** @var LeaseKeepAliveResponse $response */
286284
$response = $leaseBidi->read();
287285
$leaseBidi->cancel();
288-
if(empty($response->getID()) || (int)$response->getID() !== $leaseID)
289-
throw new Exception('Could not refresh lease ID: ' . $leaseID);
286+
if($response === null || empty($response->getID()) || (int)$response->getID() !== $leaseID)
287+
throw new NoResponseException('Could not refresh lease ID: ' . $leaseID);
288+
289+
if((int)$response->getTTL() === 0)
290+
throw new InvalidLeaseException('Invalid lease ID or expired lease');
290291

291292
return (int)$response->getTTL();
292293
}
293294

294295
/**
295296
* Execute $requestOperations if Compare succeeds, execute $failureOperations otherwise if defined
296297
*
297-
* @param array $requestOperations operations to perform on success, array of RequestOp objects
298-
* @param array|null $failureOperations operations to perform on failure, array of RequestOp objects
299-
* @param array $compare array of Compare objects
298+
* @param RequestOp[] $requestOperations operations to perform on success, array of RequestOp objects
299+
* @param RequestOp[]|null $failureOperations operations to perform on failure, array of RequestOp objects
300+
* @param Compare[] $compare array of Compare objects
300301
* @return TxnResponse
301302
* @throws InvalidResponseStatusCodeException
302303
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Aternos\Etcd\Exception;
4+
5+
/**
6+
* Class InvalidLeaseException
7+
*
8+
* @package Aternos\Etcd\Exception
9+
*/
10+
class InvalidLeaseException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Aternos\Etcd\Exception;
4+
5+
/**
6+
* Class NoClientAvailableException
7+
*
8+
* @package Aternos\Etcd\Exception
9+
*/
10+
class NoClientAvailableException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Aternos\Etcd\Exception;
4+
5+
/**
6+
* Class NoResponseException
7+
*
8+
* @package Aternos\Etcd\Exception
9+
*/
10+
class NoResponseException extends \Exception
11+
{
12+
13+
}

0 commit comments

Comments
 (0)