Skip to content

Commit e5deba5

Browse files
mschoenlaubhanumantmk
authored andcommitted
Client pool tries to repair unhealthy connections before returning them to the pool. Also, if there are more than min_pool_size connections in the pool, the oldest one is destroyed.
1 parent a8c1da4 commit e5deba5

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/mongoc/mongoc-client-pool.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "mongoc-client-pool.h"
2020
#include "mongoc-queue-private.h"
2121
#include "mongoc-thread-private.h"
22+
#include "mongoc-cluster-private.h"
23+
#include "mongoc-client-private.h"
2224
#include "mongoc-trace.h"
2325

2426

@@ -196,20 +198,44 @@ mongoc_client_pool_push (mongoc_client_pool_t *pool,
196198
bson_return_if_fail(pool);
197199
bson_return_if_fail(client);
198200

199-
/*
200-
* TODO: Shutdown old client connections.
201-
*/
201+
if (pool->size > pool->min_pool_size) {
202+
mongoc_client_t *old_client;
203+
old_client = _mongoc_queue_pop_head (&pool->queue);
204+
mongoc_client_destroy (old_client);
205+
pool->size--;
206+
}
202207

203-
/*
204-
* TODO: We should try to make a client healthy again if it
205-
* is unhealthy since this is typically where a thread
206-
* is done with its work.
207-
*/
208+
if ((client->cluster.state == MONGOC_CLUSTER_STATE_HEALTHY) ||
209+
(client->cluster.state == MONGOC_CLUSTER_STATE_BORN)) {
210+
mongoc_mutex_lock (&pool->mutex);
211+
_mongoc_queue_push_tail (&pool->queue, client);
212+
} else if (_mongoc_cluster_reconnect (&client->cluster, NULL)) {
213+
mongoc_mutex_lock (&pool->mutex);
214+
_mongoc_queue_push_tail (&pool->queue, client);
215+
}else {
216+
mongoc_client_destroy (client);
217+
mongoc_mutex_lock (&pool->mutex);
218+
pool->size--;
219+
}
208220

209-
mongoc_mutex_lock(&pool->mutex);
210-
_mongoc_queue_push_head(&pool->queue, client);
211221
mongoc_cond_signal(&pool->cond);
212222
mongoc_mutex_unlock(&pool->mutex);
213223

214224
EXIT;
215225
}
226+
227+
size_t
228+
mongoc_client_pool_get_size (mongoc_client_pool_t *pool)
229+
{
230+
ENTRY;
231+
232+
bson_return_if_fail (pool);
233+
234+
size_t size = 0;
235+
236+
mongoc_mutex_lock (&pool->mutex);
237+
size = pool->size;
238+
mongoc_mutex_unlock (&pool->mutex);
239+
240+
RETURN (size);
241+
}

src/mongoc/mongoc-client-pool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void mongoc_client_pool_set_ssl_opts (mongoc_client_pool_t *p
4848
const mongoc_ssl_opt_t *opts);
4949
#endif
5050

51+
size_t mongoc_client_pool_get_size(mongoc_client_pool_t *pool);
5152

5253
BSON_END_DECLS
5354

0 commit comments

Comments
 (0)