Skip to content

Commit c7fa88c

Browse files
CDRIVER-4615 use polling monitoring in FaaS environments (#1362)
Co-authored-by: Kevin Albertson <[email protected]> --------- Co-authored-by: Kevin Albertson <[email protected]>
1 parent 960760f commit c7fa88c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/libmongoc/src/mongoc/mongoc-server-monitor.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mongoc/mcd-rpc.h"
2121
#include "mongoc/mongoc-client-private.h"
2222
#include "mongoc/mongoc-error-private.h"
23+
#include "mongoc/mongoc-handshake-private.h"
2324
#include "mongoc/mongoc-ssl-private.h"
2425
#include "mongoc/mongoc-stream-private.h"
2526
#include "mongoc/mongoc-topology-background-monitoring-private.h"
@@ -1073,7 +1074,11 @@ _server_monitor_check_server (
10731074
GOTO (exit);
10741075
}
10751076

1076-
if (!bson_empty (&previous_description->topology_version)) {
1077+
if (!bson_empty (&previous_description->topology_version) &&
1078+
_mongoc_handshake_get ()->env == MONGOC_HANDSHAKE_ENV_NONE) {
1079+
// Use stream monitoring if:
1080+
// - Server supports stream monitoring (indicated by `topologyVersion`).
1081+
// - Application is not in an FaaS environment (e.g. AWS Lambda).
10771082
awaited = true;
10781083
_server_monitor_heartbeat_started (server_monitor, awaited);
10791084
MONITOR_LOG (server_monitor, "awaitable hello");

src/libmongoc/tests/test-mongoc-background-monitoring.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mongoc/mongoc.h"
1919
#include "mongoc/mongoc-client-pool-private.h"
2020
#include "mongoc/mongoc-client-private.h"
21+
#include "mongoc/mongoc-handshake-private.h"
2122
#include "mongoc/mongoc-server-description-private.h"
2223
#include "mongoc/mongoc-topology-background-monitoring-private.h"
2324
#include "mongoc/mongoc-topology-description-private.h"
@@ -390,6 +391,25 @@ test_connect_succeeds (void)
390391
tf_destroy (tf);
391392
}
392393

394+
void
395+
test_connect_faas_use_polling (void)
396+
{
397+
test_fixture_t *tf;
398+
mongoc_handshake_t *md = _mongoc_handshake_get ();
399+
md->env = MONGOC_HANDSHAKE_ENV_AWS;
400+
401+
/* This mock server will not respond to streaming hello, so OBSERVE_SOON
402+
will timeout if the server monitor doesn't detect ENV_AWS and switch to
403+
polling */
404+
tf = tf_new (TF_AUTO_RESPOND_POLLING_HELLO);
405+
OBSERVE_SOON (tf, tf->observations->n_heartbeat_succeeded == 2);
406+
OBSERVE_SOON (tf, tf->observations->n_heartbeat_failed == 0);
407+
OBSERVE (tf, !tf->observations->awaited);
408+
409+
tf_destroy (tf);
410+
md->env = MONGOC_HANDSHAKE_ENV_NONE;
411+
}
412+
393413
void
394414
test_connect_hangup (void)
395415
{
@@ -1134,6 +1154,10 @@ test_monitoring_install (TestSuite *suite)
11341154
/* Tests for initial connection. */
11351155
TestSuite_AddMockServerTest (
11361156
suite, "/server_monitor_thread/connect/succeeds", test_connect_succeeds);
1157+
TestSuite_AddMockServerTest (
1158+
suite,
1159+
"/server_monitor_thread/connect/faas_use_polling",
1160+
test_connect_faas_use_polling);
11371161
TestSuite_AddMockServerTest (
11381162
suite, "/server_monitor_thread/connect/hangup", test_connect_hangup);
11391163
TestSuite_AddMockServerTest (

0 commit comments

Comments
 (0)