.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "MONGOC_CONNECTION_POOLING" "3" "May 07, 2024" "1.27.1" "libmongoc" .sp The MongoDB C driver has two connection modes: single\-threaded and pooled. Single\-threaded mode is optimized for embedding the driver within languages like PHP. Multi\-threaded programs should use pooled mode: this mode minimizes the total connection count, and in pooled mode background threads monitor the MongoDB server topology, so the program need not block to scan it. .SH SINGLE MODE .sp In single mode, your program creates a \fI\%mongoc_client_t\fP directly: .INDENT 0.0 .INDENT 3.5 .sp .EX mongoc_client_t *client = mongoc_client_new ( \(dqmongodb://hostA,hostB/?replicaSet=my_rs\(dq); .EE .UNINDENT .UNINDENT .sp The client connects on demand when your program first uses it for a MongoDB operation. Using a non\-blocking socket per server, it begins a check on each server concurrently, and uses the asynchronous \fBpoll\fP or \fBselect\fP function to receive events from the sockets, until all have responded or timed out. Put another way, in single\-threaded mode the C Driver fans out to begin all checks concurrently, then fans in once all checks have completed or timed out. Once the scan completes, the client executes your program\(aqs operation and returns. .sp In single mode, the client re\-scans the server topology roughly once per minute. If more than a minute has elapsed since the previous scan, the next operation on the client will block while the client completes its scan. This interval is configurable with \fBheartbeatFrequencyMS\fP in the connection string. (See \fI\%mongoc_uri_t\fP\&.) .sp A single client opens one connection per server in your topology: these connections are used both for scanning the topology and performing normal operations. .SH POOLED MODE .sp To activate pooled mode, create a \fI\%mongoc_client_pool_t\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX mongoc_uri_t *uri = mongoc_uri_new ( \(dqmongodb://hostA,hostB/?replicaSet=my_rs\(dq); mongoc_client_pool_t *pool = mongoc_client_pool_new (uri); .EE .UNINDENT .UNINDENT .sp When your program first calls \fI\%mongoc_client_pool_pop()\fP, the pool launches monitoring threads in the background. Monitoring threads independently connect to all servers in the connection string. As monitoring threads receive hello responses from the servers, they update the shared view of the server topology. Additional monitoring threads and connections are created as new servers are discovered. Monitoring threads are terminated when servers are removed from the shared view of the server topology. .sp Each thread that executes MongoDB operations must check out a client from the pool: .INDENT 0.0 .INDENT 3.5 .sp .EX mongoc_client_t *client = mongoc_client_pool_pop (pool); /* use the client for operations ... */ mongoc_client_pool_push (pool, client); .EE .UNINDENT .UNINDENT .sp The \fI\%mongoc_client_t\fP object is not thread\-safe, only the \fI\%mongoc_client_pool_t\fP is. .sp When the driver is in pooled mode, your program\(aqs operations are unblocked as soon as monitoring discovers a usable server. For example, if a thread in your program is waiting to execute an \(dqinsert\(dq on the primary, it is unblocked as soon as the primary is discovered, rather than waiting for all secondaries to be checked as well. .sp The pool opens one connection per server for monitoring, and each client opens its own connection to each server it uses for application operations. Background monitoring threads re\-scan servers independently roughly every 10 seconds. This interval is configurable with \fBheartbeatFrequencyMS\fP in the connection string. (See \fI\%mongoc_uri_t\fP\&.) .sp The connection string can also specify \fBwaitQueueTimeoutMS\fP to limit the time that \fI\%mongoc_client_pool_pop()\fP will wait for a client from the pool. (See \fI\%mongoc_uri_t\fP\&.) If \fBwaitQueueTimeoutMS\fP is specified, then it is necessary to confirm that a client was actually returned: .INDENT 0.0 .INDENT 3.5 .sp .EX mongoc_uri_t *uri = mongoc_uri_new ( \(dqmongodb://hostA,hostB/?replicaSet=my_rs&waitQueueTimeoutMS=1000\(dq); mongoc_client_pool_t *pool = mongoc_client_pool_new (uri); mongoc_client_t *client = mongoc_client_pool_pop (pool); if (client) { /* use the client for operations ... */ mongoc_client_pool_push (pool, client); } else { /* take appropriate action for a timeout */ } .EE .UNINDENT .UNINDENT .sp See \fI\%Connection Pool Options\fP to configure pool size and behavior, and see \fI\%mongoc_client_pool_t\fP for an extended example of a multi\-threaded program that uses the driver in pooled mode. .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .