Skip to content

Commit 0ab5d5c

Browse files
derickrjmikola
authored andcommitted
PHPC-752: Allow users to set a limit on acceptable staleness
1 parent 4c2e813 commit 0ab5d5c

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

php_phongo.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,9 @@ static mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options
10041004
!strcasecmp(key, "safe") ||
10051005
!strcasecmp(key, "slaveok") ||
10061006
!strcasecmp(key, "w") ||
1007-
!strcasecmp(key, "wtimeoutms")) {
1007+
!strcasecmp(key, "wtimeoutms") ||
1008+
!strcasecmp(key, "maxstalenessms")
1009+
) {
10081010
continue;
10091011
}
10101012

@@ -1090,7 +1092,9 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
10901092

10911093
if (!bson_iter_init_find_case(&iter, options, "slaveok") &&
10921094
!bson_iter_init_find_case(&iter, options, "readpreference") &&
1093-
!bson_iter_init_find_case(&iter, options, "readpreferencetags")) {
1095+
!bson_iter_init_find_case(&iter, options, "readpreferencetags") &&
1096+
!bson_iter_init_find_case(&iter, options, "maxstalenessms")
1097+
) {
10941098
return true;
10951099
}
10961100

@@ -1141,6 +1145,19 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
11411145
return false;
11421146
}
11431147

1148+
/* Handle maxStalenessMS, and make sure it is not combined with primary
1149+
* readPreference */
1150+
if (bson_iter_init_find_case(&iter, options, "maxstalenessms") && BSON_ITER_HOLDS_INT32(&iter)) {
1151+
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
1152+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessMS");
1153+
mongoc_read_prefs_destroy(new_rp);
1154+
1155+
return false;
1156+
}
1157+
1158+
mongoc_read_prefs_set_max_staleness_ms(new_rp, bson_iter_int32(&iter));
1159+
}
1160+
11441161
/* This may be redundant in light of the last check (primary with tags), but
11451162
* we'll check anyway in case additional validation is implemented. */
11461163
if (!mongoc_read_prefs_is_valid(new_rp)) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
MongoDB\Driver\Manager: maxStalenessMS
3+
--FILE--
4+
<?php
5+
// They both should work, and produce no output. We're not testing whether the flag actually does something
6+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxStalenessMS=1231");
7+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxStalenessMS' => 1231 ] );
8+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY&maxstalenessms=1231");
9+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?readPreference=SECONDARY", [ 'maxstalenessms' => 1231 ] );
10+
?>
11+
==DONE==
12+
--EXPECTF--
13+
==DONE==
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager: maxStalenessMS
3+
--FILE--
4+
<?php
5+
try {
6+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxstalenessms=1231");
7+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
8+
echo $e->getMessage(), "\n";
9+
}
10+
11+
try {
12+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/?maxStalenessMS=1231");
13+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
17+
try {
18+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxstalenessms' => 1231 ] );
19+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
23+
try {
24+
$manager = new MongoDB\Driver\Manager("mongodb://localhost/", [ 'maxStalenessMS' => 1231 ] );
25+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
?>
29+
--EXPECTF--
30+
Failed to parse MongoDB URI: 'mongodb://localhost/?maxstalenessms=1231'
31+
Failed to parse MongoDB URI: 'mongodb://localhost/?maxStalenessMS=1231'
32+
Primary read preference mode conflicts with maxStalenessMS
33+
Primary read preference mode conflicts with maxStalenessMS

0 commit comments

Comments
 (0)