1MONGOC_CLIENT_POOL_T(3)            libmongoc           MONGOC_CLIENT_POOL_T(3)
2
3
4
5A connection pool for multi-threaded programs. See Connection Pooling.
6

SYNOPSIS

8          typedef struct _mongoc_client_pool_t mongoc_client_pool_t
9
10       mongoc_client_pool_t  is the basis for multi-threading in the MongoDB C
11       driver. Since mongoc_client_t  structures  are  not  thread-safe,  this
12       structure is used to retrieve a new mongoc_client_t for a given thread.
13       This structure  is  thread-safe,  except  for  its  destructor  method,
14       mongoc_client_pool_destroy(), which is not thread-safe and must only be
15       called from one thread.
16

EXAMPLE

18       example-pool.c
19
20          /* gcc example-pool.c -o example-pool $(pkg-config --cflags --libs
21           * libmongoc-1.0) */
22
23          /* ./example-pool [CONNECTION_STRING] */
24
25          #include <mongoc/mongoc.h>
26          #include <pthread.h>
27          #include <stdio.h>
28
29          static pthread_mutex_t mutex;
30          static bool in_shutdown = false;
31
32          static void *
33          worker (void *data)
34          {
35             mongoc_client_pool_t *pool = data;
36             mongoc_client_t *client;
37             bson_t ping = BSON_INITIALIZER;
38             bson_error_t error;
39             bool r;
40
41             BSON_APPEND_INT32 (&ping, "ping", 1);
42
43             while (true) {
44                client = mongoc_client_pool_pop (pool);
45                /* Do something with client. If you are writing an HTTP server, you
46                 * probably only want to hold onto the client for the portion of the
47                 * request performing database queries.
48                 */
49                r = mongoc_client_command_simple (
50                   client, "admin", &ping, NULL, NULL, &error);
51
52                if (!r) {
53                   fprintf (stderr, "%s\n", error.message);
54                }
55
56                mongoc_client_pool_push (pool, client);
57
58                pthread_mutex_lock (&mutex);
59                if (in_shutdown || !r) {
60                   pthread_mutex_unlock (&mutex);
61                   break;
62                }
63
64                pthread_mutex_unlock (&mutex);
65             }
66
67             bson_destroy (&ping);
68             return NULL;
69          }
70
71          int
72          main (int argc, char *argv[])
73          {
74             const char *uri_string = "mongodb://127.0.0.1/?appname=pool-example";
75             mongoc_uri_t *uri;
76             bson_error_t error;
77             mongoc_client_pool_t *pool;
78             pthread_t threads[10];
79             unsigned i;
80             void *ret;
81
82             pthread_mutex_init (&mutex, NULL);
83             mongoc_init ();
84
85             if (argc > 1) {
86                uri_string = argv[1];
87             }
88
89             uri = mongoc_uri_new_with_error (uri_string, &error);
90             if (!uri) {
91                fprintf (stderr,
92                         "failed to parse URI: %s\n"
93                         "error message:       %s\n",
94                         uri_string,
95                         error.message);
96                return EXIT_FAILURE;
97             }
98
99             pool = mongoc_client_pool_new (uri);
100             mongoc_client_pool_set_error_api (pool, 2);
101
102             for (i = 0; i < 10; i++) {
103                pthread_create (&threads[i], NULL, worker, pool);
104             }
105
106             sleep (10);
107             pthread_mutex_lock (&mutex);
108             in_shutdown = true;
109             pthread_mutex_unlock (&mutex);
110
111             for (i = 0; i < 10; i++) {
112                pthread_join (threads[i], &ret);
113             }
114
115             mongoc_client_pool_destroy (pool);
116             mongoc_uri_destroy (uri);
117
118             mongoc_cleanup ();
119
120             return EXIT_SUCCESS;
121          }
122
123

AUTHOR

125       MongoDB, Inc
126
128       2017-present, MongoDB, Inc
129
130
131
132
1331.25.1                           Nov 08, 2023          MONGOC_CLIENT_POOL_T(3)
Impressum