1MONGOC_CLIENT_POOL_T(3)        MongoDB C Driver        MONGOC_CLIENT_POOL_T(3)
2
3
4

NAME

6       mongoc_client_pool_t - mongoc_client_pool_t
7
8       A connection pool for multi-threaded programs. See connection-pooling.
9

SYNOPSIS

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

EXAMPLE

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

AUTHOR

128       MongoDB, Inc
129
131       2017-present, MongoDB, Inc
132
133
134
135
1361.14.0                           Feb 22, 2019          MONGOC_CLIENT_POOL_T(3)
Impressum