MongoDB C 드라이버에는 단일 스레드와 풀의 두 가지 연결 모드가 있습니다. 단일 스레드 모드는 PHP와 같은 언어에 드라이버를 임베딩하는 데 최적화되어 있습니다. 멀티스레드 프로그램은 풀 모드를 사용해야 합니다. 이 모드는 총 연결 수를 최소화하고 풀링 모드에서는 백그라운드 스레드가 MongoDB 서버 토폴로지를 모니터링하므로 프로그램이 스캔을 차단할 필요가 없습니다.
싱글 모드
In single mode, your program creates a mongoc_client_t directly:
mongoc_client_t *client = mongoc_client_new ( "mongodb://hostA,hostB/?replicaSet=my_rs");
클라이언트 는 프로그램이 MongoDB 작업에 처음 사용할 때 온디맨드 방식으로 연결합니다. 서버 당 비블로킹 소켓을 사용하여 각 서버 에 대한 검사를 동시에 시작하고, 모든 서버가 응답하거나 시간 초과될 때까지 비동기 poll
또는 select
함수를 사용하여 소켓에서 이벤트를 수신합니다. 다시 말해, 단일 스레드 모드 에서 C 드라이버 는 모든 검사를 동시에 시작하기 위해 팬아웃한 다음 모든 검사가 완료되거나 시간 초과되면 팬인합니다. 스캔이 완료되면 클라이언트 는 프로그램 작업을 실행하고 반환합니다.
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 heartbeatFrequencyMS
in the connection string. (See mongoc_uri_t.)
단일 클라이언트는 토폴로지의 서버당 하나의 연결을 엽니다. 이러한 연결은 토폴로지를 스캔하고 일반 작업을 수행하는 데 모두 사용됩니다.
풀링 모드
To activate pooled mode, create a mongoc_client_pool_t:
mongoc_uri_t *uri = mongoc_uri_new ( "mongodb://hostA,hostB/?replicaSet=my_rs"); mongoc_client_pool_t *pool = mongoc_client_pool_new (uri);
When your program first calls mongoc_client_pool_pop, 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.
MongoDB 작업을 실행하는 각 스레드는 풀에서 클라이언트 를 체크아웃해야 합니다.
mongoc_client_t *client = mongoc_client_pool_pop (pool); /* use the client for operations ... */ mongoc_client_pool_push (pool, client);
The mongoc_client_t object is not thread-safe, only the mongoc_client_pool_t is.
운전자 가 풀링 모드 인 경우 모니터링 에서 사용 가능한 서버 를 발견하는 즉시 프로그램 작업이 차단 해제됩니다. 예를 예시, 프로그램의 스레드가 프라이머리 에서 '삽입'을 실행하기 위해 대기 중인 경우, 모든 세컨더리도 검사할 때까지 기다리지 않고 프라이머리 가 발견되는 즉시 차단이 해제됩니다.
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 heartbeatFrequencyMS
in the connection string. (See mongoc_uri_t.)
The connection string can also specify waitQueueTimeoutMS
to limit the time that mongoc_client_pool_pop will wait for a client from the pool. (See mongoc_uri_t.) If waitQueueTimeoutMS
is specified, then it is necessary to confirm that a client was actually returned:
mongoc_uri_t *uri = mongoc_uri_new ( "mongodb://hostA,hostB/?replicaSet=my_rs&waitQueueTimeoutMS=1000"); 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 */ }
See connection pool options to configure pool size and behavior, and see mongoc_client_pool_t for an extended example of a multi-threaded program that uses the driver in pooled mode.