Skip to content

Commit cfac8b8

Browse files
committed
PHPC-1985: Server::getLatency() should return null if unset
1 parent 594e83b commit cfac8b8

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/MongoDB/Server.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,30 +350,31 @@ static PHP_METHOD(Server, getInfo)
350350
mongoc_server_description_destroy(sd);
351351
} /* }}} */
352352

353-
/* {{{ proto integer MongoDB\Driver\Server::getLatency()
354-
Returns the last measured latency for this Server */
353+
/* {{{ proto integer|null MongoDB\Driver\Server::getLatency()
354+
Returns the measured latency (i.e. round trip time in milliseconds) for
355+
this Server, or null if unset. */
355356
static PHP_METHOD(Server, getLatency)
356357
{
357-
zend_error_handling error_handling;
358358
php_phongo_server_t* intern;
359359
mongoc_server_description_t* sd;
360360

361+
PHONGO_PARSE_PARAMETERS_NONE();
362+
361363
intern = Z_SERVER_OBJ_P(getThis());
362364

363-
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
364-
if (zend_parse_parameters_none() == FAILURE) {
365-
zend_restore_error_handling(&error_handling);
365+
if (!(sd = mongoc_client_get_server_description(Z_MANAGER_OBJ_P(&intern->manager)->client, intern->server_id))) {
366+
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "Failed to get server description");
366367
return;
367368
}
368-
zend_restore_error_handling(&error_handling);
369369

370-
if ((sd = mongoc_client_get_server_description(Z_MANAGER_OBJ_P(&intern->manager)->client, intern->server_id))) {
370+
/* TODO: Use MONGOC_RTT_UNSET once it is added to libmongoc's public API (CDRIVER-4176) */
371+
if (mongoc_server_description_round_trip_time(sd) == -1) {
372+
RETVAL_NULL();
373+
} else {
371374
RETVAL_LONG((zend_long) mongoc_server_description_round_trip_time(sd));
372-
mongoc_server_description_destroy(sd);
373-
return;
374375
}
375376

376-
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "Failed to get server description");
377+
mongoc_server_description_destroy(sd);
377378
} /* }}} */
378379

379380
/* {{{ proto integer MongoDB\Driver\Server::getPort()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\Driver\Server::getLatency() returns a non-negative integer when set
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_load_balanced(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference('primary'));
13+
var_dump($server->getLatency());
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECTF--
19+
int(%d)
20+
===DONE===
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\Driver\Server::getLatency() returns null when unset (e.g. load balancer)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_not_load_balanced(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference('primary'));
13+
var_dump($server->getLatency());
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECT--
19+
NULL
20+
===DONE===

0 commit comments

Comments
 (0)