Skip to content

Commit b2c1eaf

Browse files
committed
issue #281: split OPTION_TIMEOUT into separate options
OPTION_TIMEOUT is now superseded by the more specialized options - OPTION_CONNECT_TIMEOUT - OPTION_REQUEST_TIMEOUT The existing OPTION_TIMEOUT value can still be used as before. When used, it will automatically clobber values set in OPTION_CONNECT_TIMEOUT and OPTION_REQUEST_TIMEOUT. Using one of the more specialized options will remove OPTION_TIMEOUT from the connection's option.
1 parent 9f061fd commit b2c1eaf

File tree

7 files changed

+176
-12
lines changed

7 files changed

+176
-12
lines changed

examples/init.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
// in order to use an externally generated JWT, there is no need to specify user and passwd, but just the JWT value:
6060
// ConnectionOptions::OPTION_AUTH_JWT => '', // use an externally generated JWT for authorization
6161

62-
ConnectionOptions::OPTION_TIMEOUT => 30, // timeout in seconds
63-
// ConnectionOptions::OPTION_TRACE => $traceFunc, // tracer function, can be used for debugging
62+
ConnectionOptions::OPTION_CONNECT_TIMEOUT => 10, // connect timeout in seconds
63+
ConnectionOptions::OPTION_REQUEST_TIMEOUT => 30, // request timeout in seconds
64+
// ConnectionOptions::OPTION_TRACE => $traceFunc, // tracer function, can be used for debugging
6465
ConnectionOptions::OPTION_CREATE => false, // do not create unknown collections automatically
6566
ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST, // last update wins
6667
];

lib/ArangoDBClient/Connection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public function setOption($name, $value)
151151
$this->_options[$name] = $value;
152152

153153
// special handling for several options
154-
if ($name === ConnectionOptions::OPTION_TIMEOUT) {
154+
if ($name === ConnectionOptions::OPTION_TIMEOUT ||
155+
$name === ConnectionOptions::OPTION_REQUEST_TIMEOUT) {
155156
// set the timeout option: patch the stream of an existing connection
156157
if (is_resource($this->_handle)) {
157158
stream_set_timeout($this->_handle, $value);

lib/ArangoDBClient/ConnectionOptions.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,20 @@ class ConnectionOptions implements \ArrayAccess
6666

6767
/**
6868
* Timeout value index constant
69+
* @deprecated superseded by OPTION_CONNECT_TIMEOUT and OPTION_REQUEST_TIMEOUT
6970
*/
7071
const OPTION_TIMEOUT = 'timeout';
7172

73+
/**
74+
* Connect timeout value index constant
75+
*/
76+
const OPTION_CONNECT_TIMEOUT = 'connectTimeout';
77+
78+
/**
79+
* Request timeout value index constant
80+
*/
81+
const OPTION_REQUEST_TIMEOUT = 'requestTimeout';
82+
7283
/**
7384
* Number of servers tried in case of failover
7485
* if set to 0, then an unlimited amount of servers will be tried
@@ -293,6 +304,11 @@ public function getAll()
293304
public function offsetSet($offset, $value)
294305
{
295306
$this->_values[$offset] = $value;
307+
if ($offset === self::OPTION_CONNECT_TIMEOUT || $offset === self::OPTION_REQUEST_TIMEOUT) {
308+
// special handling for OPTION_TIMEOUT: it will be removed once
309+
// a more specialized option is used
310+
unset($this->_values[self::OPTION_TIMEOUT]);
311+
}
296312
$this->validate();
297313
}
298314

@@ -439,7 +455,8 @@ private static function getDefaults()
439455
self::OPTION_PORT => DefaultValues::DEFAULT_PORT,
440456
self::OPTION_FAILOVER_TRIES => DefaultValues::DEFAULT_FAILOVER_TRIES,
441457
self::OPTION_FAILOVER_TIMEOUT => DefaultValues::DEFAULT_FAILOVER_TIMEOUT,
442-
self::OPTION_TIMEOUT => DefaultValues::DEFAULT_TIMEOUT,
458+
self::OPTION_CONNECT_TIMEOUT => DefaultValues::DEFAULT_CONNECT_TIMEOUT,
459+
self::OPTION_REQUEST_TIMEOUT => DefaultValues::DEFAULT_REQUEST_TIMEOUT,
443460
self::OPTION_MEMCACHED_PERSISTENT_ID => 'arangodb-php-pool',
444461
self::OPTION_MEMCACHED_OPTIONS => [ ],
445462
self::OPTION_MEMCACHED_ENDPOINTS_KEY => 'arangodb-php-endpoints',
@@ -575,6 +592,12 @@ private function validate()
575592
)
576593
);
577594
}
595+
596+
if (isset($this->_values[self::OPTION_TIMEOUT])) {
597+
// propagate values from OPTION_TIMOEUT into OPTION_CONNECT_TIMEOUT and OPTION_REQUEST_TIMEOUT
598+
$this->_values[self::OPTION_CONNECT_TIMEOUT] = (float) $this->_values[self::OPTION_TIMEOUT];
599+
$this->_values[self::OPTION_REQUEST_TIMEOUT] = (float) $this->_values[self::OPTION_TIMEOUT];
600+
}
578601

579602
UpdatePolicy::validate($this->_values[self::OPTION_UPDATE_POLICY]);
580603
UpdatePolicy::validate($this->_values[self::OPTION_REPLACE_POLICY]);

lib/ArangoDBClient/DefaultValues.php

+11
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,20 @@ abstract class DefaultValues
2727

2828
/**
2929
* Default timeout value (used if no timeout value specified)
30+
* @deprecated superseded by DEFAULT_CONNECT_TIMEOUT and DEFAULT_REQUEST_TIMEOUT
3031
*/
3132
const DEFAULT_TIMEOUT = 30;
3233

34+
/**
35+
* Default connect timeout value (used if no timeout value specified)
36+
*/
37+
const DEFAULT_CONNECT_TIMEOUT = 30;
38+
39+
/**
40+
* Default request timeout value (used if no timeout value specified)
41+
*/
42+
const DEFAULT_REQUEST_TIMEOUT = 30;
43+
3344
/**
3445
* Default number of failover servers to try (used in case there is an automatic failover)
3546
* if set to 0, then an unlimited amount of servers will be tried

lib/ArangoDBClient/HttpHelper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static function createConnection(ConnectionOptions $options)
104104
Endpoint::normalize($endpoint),
105105
$errNo,
106106
$message,
107-
$options[ConnectionOptions::OPTION_TIMEOUT],
107+
$options[ConnectionOptions::OPTION_CONNECT_TIMEOUT],
108108
STREAM_CLIENT_CONNECT,
109109
$context
110110
);
@@ -116,7 +116,7 @@ public static function createConnection(ConnectionOptions $options)
116116
);
117117
}
118118

119-
stream_set_timeout($fp, $options[ConnectionOptions::OPTION_TIMEOUT]);
119+
stream_set_timeout($fp, $options[ConnectionOptions::OPTION_REQUEST_TIMEOUT]);
120120

121121
return $fp;
122122
}

tests/ConnectionTest.php

+130-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function testGetStatus()
152152
$response = $connection->get('/_admin/statistics');
153153
static::assertEquals(200, $response->getHttpCode(), 'Did not return http code 200');
154154
}
155-
155+
156156
/**
157157
* Test get options
158158
*/
@@ -208,6 +208,78 @@ public function testSetOptions()
208208
$value = $connection->getOption(ConnectionOptions::OPTION_RECONNECT);
209209
static::assertFalse($value);
210210
}
211+
212+
/**
213+
* Test timeout options handling
214+
*/
215+
public function testTimeoutOptions()
216+
{
217+
$connection = getConnection();
218+
219+
$oldTimeout = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
220+
$oldConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
221+
$oldRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
222+
223+
static::assertEquals($oldTimeout, $oldConnectTimeout);
224+
static::assertEquals($oldTimeout, $oldRequestTimeout);
225+
226+
$connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 12);
227+
$newTimeout = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
228+
$newConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
229+
$newRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
230+
231+
static::assertEquals(12, $newTimeout);
232+
static::assertEquals(12, $newConnectTimeout);
233+
static::assertEquals(12, $newRequestTimeout);
234+
235+
$connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 42);
236+
$newTimeout = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
237+
$newConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
238+
$newRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
239+
240+
static::assertEquals(42, $newTimeout);
241+
static::assertEquals(42, $newConnectTimeout);
242+
static::assertEquals(42, $newRequestTimeout);
243+
244+
$connection->setOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT, 1.5);
245+
try {
246+
$connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
247+
static::assertFalse(true);
248+
} catch (\Exception $e) {
249+
// OPTION_TIMEOUT is gone once OPTION_CONNECT_TIMEOUT is used
250+
}
251+
252+
$newConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
253+
$newRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
254+
255+
static::assertEquals(1.5, $newConnectTimeout);
256+
static::assertEquals(42, $newRequestTimeout);
257+
258+
$connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, 24.5);
259+
$newConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
260+
$newRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
261+
262+
try {
263+
$connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
264+
static::assertFalse(true);
265+
} catch (\Exception $e) {
266+
// OPTION_TIMEOUT is gone once OPTION_REQUEST_TIMEOUT is used
267+
}
268+
269+
static::assertEquals(1.5, $newConnectTimeout);
270+
static::assertEquals(24.5, $newRequestTimeout);
271+
272+
273+
$connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 8);
274+
$newTimeout = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
275+
$newConnectTimeout = $connection->getOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT);
276+
$newRequestTimeout = $connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
277+
278+
static::assertEquals(8, $newTimeout);
279+
static::assertEquals(8, $newConnectTimeout);
280+
static::assertEquals(8, $newRequestTimeout);
281+
}
282+
211283

212284
/**
213285
* Test set invalid options
@@ -340,7 +412,7 @@ public function testSetTimeoutException()
340412
throw $exception;
341413
}
342414
}
343-
415+
344416
/**
345417
* Test timeout, no exception
346418
*/
@@ -356,6 +428,62 @@ public function testSetTimeout()
356428
$cursor = $statement->execute();
357429
static::assertCount(1, $cursor->getAll());
358430
}
431+
432+
/**
433+
* Test connect timeout, no exception
434+
*/
435+
public function testSetConnectTimeout()
436+
{
437+
$connection = getConnection();
438+
$connection->setOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT, 5);
439+
$query = 'RETURN SLEEP(1)';
440+
441+
$statement = new Statement($connection, ['query' => $query]);
442+
443+
// should work
444+
$cursor = $statement->execute();
445+
static::assertCount(1, $cursor->getAll());
446+
}
447+
448+
/**
449+
* Test request timeout exception
450+
*
451+
* @expectedException \ArangoDBClient\ClientException
452+
*/
453+
public function testSetRequestTimeoutException()
454+
{
455+
$connection = getConnection();
456+
$connection->setOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT, 3);
457+
$connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, 2);
458+
$query = 'RETURN SLEEP(3)';
459+
460+
$statement = new Statement($connection, ['query' => $query]);
461+
462+
try {
463+
// this is expected to fail
464+
$statement->execute();
465+
} catch (ClientException $exception) {
466+
static::assertEquals(408, $exception->getCode());
467+
throw $exception;
468+
}
469+
}
470+
471+
/**
472+
* Test request timeout, no exception
473+
*/
474+
public function testSetRequestTimeout()
475+
{
476+
$connection = getConnection();
477+
$connection->setOption(ConnectionOptions::OPTION_CONNECT_TIMEOUT, 5);
478+
$connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, 5);
479+
$query = 'RETURN SLEEP(1)';
480+
481+
$statement = new Statement($connection, ['query' => $query]);
482+
483+
// should work
484+
$cursor = $statement->execute();
485+
static::assertCount(1, $cursor->getAll());
486+
}
359487

360488
/**
361489
* Test "connection: close"

tests/QueryTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ public function testGetSlow()
124124
*/
125125
public function testTimeoutException()
126126
{
127-
$old = $this->connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
128-
$this->connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 10);
127+
$old = $this->connection->getOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT);
128+
$this->connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, 10);
129129
$query = 'RETURN SLEEP(13)';
130130

131131
$statement = new Statement($this->connection, ['query' => $query]);
132132

133133
try {
134134
$statement->execute();
135-
$this->connection->setOption(ConnectionOptions::OPTION_TIMEOUT, $old);
135+
$this->connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, $old);
136136
} catch (ClientException $exception) {
137-
$this->connection->setOption(ConnectionOptions::OPTION_TIMEOUT, $old);
137+
$this->connection->setOption(ConnectionOptions::OPTION_REQUEST_TIMEOUT, $old);
138138
static::assertEquals($exception->getCode(), 408);
139139
throw $exception;
140140
}

0 commit comments

Comments
 (0)