Skip to content

Commit a12649f

Browse files
CDRIVER-6053 do not buffer pooled streams (#2064)
* disable buffering for streams The stream buffer is tied to the lifetime of a stream. A large response may grow the buffer and result in higher memory usage until the stream is destroyed. A stream may live as long as the client/pool. The buffer appears unnecessary, since a separate buffer is allocated, and read directly into, for the server responses. * remove unnecessary `buffered` parameter from `mongoc_client_connect` * warn about allocation strategy of buffered stream Co-authored-by: Ezra Chung <[email protected]>
1 parent e8ea701 commit a12649f

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

src/libmongoc/doc/mongoc_stream_buffered_new.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ Synopsis
99
.. code-block:: c
1010
1111
mongoc_stream_t *
12-
mongoc_stream_buffered_new (mongoc_stream_t *base_stream,
13-
size_t buffer_size);
12+
mongoc_stream_buffered_new (mongoc_stream_t *base_stream, size_t buffer_size);
1413
1514
Parameters
1615
----------
@@ -22,6 +21,10 @@ This function shall create a new :symbol:`mongoc_stream_t` that buffers bytes to
2221

2322
``buffer_size`` will be used as the initial buffer size. It may grow past this size.
2423

24+
.. warning::
25+
26+
The internal buffer does not reduce in size once grown. Receiving a large message may result in a large allocation that persists until the returned :symbol:`mongoc_stream_t` is freed with :symbol:`mongoc_stream_destroy()`.
27+
2528
Returns
2629
-------
2730

src/libmongoc/src/mongoc/mongoc-client-private.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ mongoc_stream_t *
209209
mongoc_client_connect_tcp (int32_t connecttimeoutms, const mongoc_host_list_t *host, bson_error_t *error);
210210

211211
mongoc_stream_t *
212-
mongoc_client_connect (bool buffered,
213-
bool use_ssl,
212+
mongoc_client_connect (bool use_ssl,
214213
void *ssl_opts_void,
215214
const mongoc_uri_t *uri,
216215
const mongoc_host_list_t *host,

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,7 @@ mongoc_client_connect_unix (const mongoc_host_list_t *host, bson_error_t *error)
761761
}
762762

763763
mongoc_stream_t *
764-
mongoc_client_connect (bool buffered,
765-
bool use_ssl,
764+
mongoc_client_connect (bool use_ssl,
766765
void *ssl_opts_void,
767766
const mongoc_uri_t *uri,
768767
const mongoc_host_list_t *host,
@@ -851,9 +850,6 @@ mongoc_client_connect (bool buffered,
851850
if (!base_stream) {
852851
return NULL;
853852
}
854-
if (buffered) {
855-
return mongoc_stream_buffered_new (base_stream, 1024);
856-
}
857853
return base_stream;
858854
}
859855

@@ -897,13 +893,12 @@ mongoc_client_default_stream_initiator (const mongoc_uri_t *uri,
897893

898894
#if defined(MONGOC_ENABLE_SSL_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10100000L
899895
SSL_CTX *ssl_ctx = client->topology->scanner->openssl_ctx;
900-
return mongoc_client_connect (
901-
true, use_ssl, ssl_opts_void, uri, host, (void *) ssl_ctx, MONGOC_SHARED_PTR_NULL, error);
896+
return mongoc_client_connect (use_ssl, ssl_opts_void, uri, host, (void *) ssl_ctx, MONGOC_SHARED_PTR_NULL, error);
902897
#elif defined(MONGOC_ENABLE_SSL_SECURE_CHANNEL)
903898
return mongoc_client_connect (
904-
true, use_ssl, ssl_opts_void, uri, host, NULL, client->topology->scanner->secure_channel_cred_ptr, error);
899+
use_ssl, ssl_opts_void, uri, host, NULL, client->topology->scanner->secure_channel_cred_ptr, error);
905900
#else
906-
return mongoc_client_connect (true, use_ssl, ssl_opts_void, uri, host, NULL, MONGOC_SHARED_PTR_NULL, error);
901+
return mongoc_client_connect (use_ssl, ssl_opts_void, uri, host, NULL, MONGOC_SHARED_PTR_NULL, error);
907902
#endif
908903
}
909904

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,7 @@ _server_monitor_setup_connection (mongoc_server_monitor_t *server_monitor,
948948
secure_channel_cred_ptr = server_monitor->topology->scanner->secure_channel_cred_ptr;
949949
#endif
950950

951-
server_monitor->stream = mongoc_client_connect (false,
952-
ssl_opts_void != NULL,
951+
server_monitor->stream = mongoc_client_connect (ssl_opts_void != NULL,
953952
ssl_opts_void,
954953
server_monitor->uri,
955954
&server_monitor->description->host,

0 commit comments

Comments
 (0)