Skip to content

Commit b19e753

Browse files
committed
added examples
1 parent 39e626f commit b19e753

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Cloning the git repository](#cloning_git)
2323
- [How to use the PHP client](#howto_use)
2424
- [Setting up the connection options](#setting_up_connection_options)
25+
- [Setting up failover](#setting_up_failover)
2526
- [Creating a collection](#creating_collection)
2627
- [Creating a document](#creating_document)
2728
- [Adding exception handling](#adding_exception_handling)
@@ -204,6 +205,65 @@ When updating a document that was previously/concurrently updated by another use
204205
* fail with a conflict error: if you prefer that, set OPTION_UPDATE_POLICY to conflict
205206

206207

208+
<a name="setting_up_failover"></a>
209+
## Setting up failover
210+
211+
By default the PHP client will connect to a single endpoint only,
212+
by specifying a string value for the endpoint in the `ConnectionOptions`,
213+
e.g.
214+
215+
```php
216+
$connectionOptions = [
217+
ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529'
218+
];
219+
```
220+
221+
To set up multiple servers to connect to, it is also possible to specify
222+
an array of servers instead:
223+
224+
```php
225+
$connectionOptions = [
226+
ConnectionOptions::OPTION_ENDPOINT => [ 'tcp://localhost:8531', 'tcp://localhost:8532', 'tcp://localhost:8530' ]
227+
];
228+
```
229+
Using this option requires ArangoDB 3.3 or higher and the database running
230+
in active/passive failover mode.
231+
232+
The driver will by default try to connect to the first server endpoint in the
233+
endpoints array, and only try the following servers if no connection can be
234+
established. If no connection can be made to any server, the driver will throw
235+
an exception.
236+
237+
As it is unknown to the driver which server from the array is the current
238+
leader, the driver will connect to the specified servers in array order by
239+
default. However, to spare a few unnecessary connection attempts to failed
240+
servers, it is possible to set up caching (using Memcached) for the server list.
241+
The cached value will contain the last working server first, so that as few
242+
connection attempts as possible will need to be made.
243+
244+
In order to use this caching, it is required to install the Memcached module
245+
for PHP, and to set up the following relevant options in the `ConnectionOptions`:
246+
247+
```php
248+
$connectionOptions = [
249+
// memcached persistent id (will be passed to Memcached::__construct)
250+
ConnectionOptions::OPTION_MEMCACHED_PERSISTENT_ID => 'arangodb-php-pool',
251+
252+
// memcached servers to connect to (will be passed to Memcached::addServers)
253+
ConnectionOptions::OPTION_MEMCACHED_SERVERS => [ [ '127.0.0.1', 11211 ] ],
254+
255+
// memcached options (will be passed to Memcached::setOptions)
256+
ConnectionOptions::OPTION_MEMCACHED_OPTIONS => [ ],
257+
258+
// key to store the current endpoints array under
259+
ConnectionOptions::OPTION_MEMCACHED_ENDPOINTS_KEY => 'arangodb-php-endpoints'
260+
261+
// time-to-live for the endpoints array stored in memcached
262+
ConnectionOptions::OPTION_MEMCACHED_TTL => 600
263+
];
264+
```
265+
266+
207267
<a name="creating_collection"></a>
208268
## Creating a collection
209269
*This is just to show how a collection is created.*

examples/init.php

+17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616

1717
// normal unencrypted connection via TCP/IP
1818
ConnectionOptions::OPTION_ENDPOINT => 'tcp://localhost:8529', // endpoint to connect to
19+
20+
// // to use failover (requires ArangoDB 3.3 and the database running in active/passive failover mode)
21+
// // it is possible to specify an array of endpoints as follows:
22+
// ConnectionOptions::OPTION_ENDPOINT => [ 'tcp://localhost:8531', 'tcp://localhost:8532' ]
23+
24+
// // to use memcached for caching the currently active leader (to spare a few connection attempts
25+
// // to followers), it is possible to install the Memcached module for PHP and set the following options:
26+
// // memcached persistent id (will be passed to Memcached::__construct)
27+
// ConnectionOptions::OPTION_MEMCACHED_PERSISTENT_ID => 'arangodb-php-pool',
28+
// // memcached servers to connect to (will be passed to Memcached::addServers)
29+
// ConnectionOptions::OPTION_MEMCACHED_SERVERS => [ [ '127.0.0.1', 11211 ] ],
30+
// // memcached options (will be passed to Memcached::setOptions)
31+
// ConnectionOptions::OPTION_MEMCACHED_OPTIONS => [ ],
32+
// // key to store the current endpoints array under
33+
// ConnectionOptions::OPTION_MEMCACHED_ENDPOINTS_KEY => 'arangodb-php-endpoints'
34+
// // time-to-live for the endpoints array stored in memcached
35+
// ConnectionOptions::OPTION_MEMCACHED_TTL => 600
1936

2037
// // connection via SSL
2138
// ConnectionOptions::OPTION_ENDPOINT => 'ssl://localhost:8529', // SSL endpoint to connect to

0 commit comments

Comments
 (0)