]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
fix connection leaking
authorTamas Cseke <tamas.cseke@virtual-call-center.eu>
Mon, 21 Nov 2011 13:58:08 +0000 (14:58 +0100)
committerTamas Cseke <tamas.cseke@virtual-call-center.eu>
Mon, 21 Nov 2011 13:58:08 +0000 (14:58 +0100)
src/mod/applications/mod_mongo/mod_mongo.cpp
src/mod/applications/mod_mongo/mod_mongo.h
src/mod/applications/mod_mongo/mongo_conn.cpp

index e0b93d85b43be7a1a4e4eb0b99957ffc460a9117..973827724d6b018be1fa47f84bf10259c6878fe1 100644 (file)
@@ -49,7 +49,7 @@ SWITCH_STANDARD_API(mongo_mapreduce_function)
                        conn = mongo_connection_pool_get(globals.conn_pool);
                        if (conn) {
                                conn->runCommand(conn->nsGetDB(ns), cmd.done(), out);
-                               mongo_connection_pool_put(globals.conn_pool, conn);
+                               mongo_connection_pool_put(globals.conn_pool, conn, SWITCH_FALSE);
 
                                stream->write_function(stream, "-OK\n%s\n", out.toString().c_str());
                        } else {
@@ -57,7 +57,7 @@ SWITCH_STANDARD_API(mongo_mapreduce_function)
                        }
                } catch (DBException &e) {
                        if (conn) {
-                               mongo_connection_destroy(&conn);
+                               mongo_connection_pool_put(globals.conn_pool, conn, SWITCH_TRUE);
                        }
                        stream->write_function(stream, "-ERR\n%s\n", e.toString().c_str());
                }
@@ -97,7 +97,7 @@ SWITCH_STANDARD_API(mongo_find_one_function)
                  conn = mongo_connection_pool_get(globals.conn_pool);
                  if (conn) {
                          BSONObj res = conn->findOne(ns, Query(query), &fields);
-                         mongo_connection_pool_put(globals.conn_pool, conn);
+                         mongo_connection_pool_put(globals.conn_pool, conn, SWITCH_FALSE);
 
                          stream->write_function(stream, "-OK\n%s\n", res.toString().c_str());
                  } else {
@@ -105,7 +105,7 @@ SWITCH_STANDARD_API(mongo_find_one_function)
                  }
          } catch (DBException &e) {
                  if (conn) {
-                         mongo_connection_destroy(&conn);
+                         mongo_connection_pool_put(globals.conn_pool, conn, SWITCH_TRUE);
                  }
                  stream->write_function(stream, "-ERR\n%s\n", e.toString().c_str());
          }
index d264464294fc39e64bf01100424c5499baa6dad0..57fdd09fdb447ec330a886e3e3fdc7a978c5190f 100644 (file)
@@ -30,7 +30,7 @@ void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool);
 
 
 DBClientBase *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool);
-switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn);
+switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn, switch_bool_t destroy);
 
 
 #endif
index ffcf1200d0673880ac45c025186827cd2e51d4a3..dd23b003109b6e9db54c532bb3f15221aede9a38 100644 (file)
@@ -15,23 +15,27 @@ switch_status_t mongo_connection_create(DBClientBase **connection, const char *c
   DBClientBase *conn = NULL;
   string conn_string(conn_str), err_msg;
   ConnectionString cs = ConnectionString::parse(conn_string, err_msg);
+  switch_status_t status = SWITCH_STATUS_FALSE;
  
   if (!cs.isValid()) {
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't parse url: %s\n", err_msg.c_str());
-    return SWITCH_STATUS_GENERR;
+    return status;
   }
 
   try {
     conn = cs.connect(err_msg);
   } catch (DBException &e) {
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't connect to mongo [%s]: %s\n", conn_str, err_msg.c_str());
-    return SWITCH_STATUS_GENERR;
+    return status;
   }
 
-  *connection = conn;
-  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", conn_str);
+  if (conn) {
+    *connection = conn;
+    status = SWITCH_STATUS_SUCCESS;
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", conn_str);
+  }
 
-  return SWITCH_STATUS_SUCCESS;
+  return status;
 }
 
 void mongo_connection_destroy(DBClientBase **conn) 
@@ -75,7 +79,7 @@ switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool
   for (cpool->size = 0; cpool->size < min_connections; cpool->size++) {
 
     if (mongo_connection_create(&conn, conn_str) == SWITCH_STATUS_SUCCESS) {
-      mongo_connection_pool_put(cpool, conn);
+      mongo_connection_pool_put(cpool, conn, SWITCH_FALSE);
     } else {
       break;
     }
@@ -89,7 +93,6 @@ switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool
     switch_core_destroy_memory_pool(&pool);
   }
 
-
   return status;
 }
 
@@ -137,7 +140,7 @@ DBClientBase *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool)
   return conn;
 }
 
-switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn)
+switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn, switch_bool_t destroy)
 {
   switch_status_t status = SWITCH_STATUS_SUCCESS;
 
@@ -145,7 +148,7 @@ switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DB
   switch_assert(conn != NULL);
 
   switch_mutex_lock(conn_pool->mutex);
-  if (conn_pool->size > conn_pool->max_connections) {
+  if (destroy || conn_pool->size > conn_pool->max_connections) {
 #ifdef MONGO_POOL_DEBUG
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "POOL: Destroy connection %p\n", conn);
 #endif