diff --git a/src/mod/applications/mod_mongo/mod_mongo.cpp b/src/mod/applications/mod_mongo/mod_mongo.cpp index e0b93d85b4..973827724d 100644 --- a/src/mod/applications/mod_mongo/mod_mongo.cpp +++ b/src/mod/applications/mod_mongo/mod_mongo.cpp @@ -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()); } diff --git a/src/mod/applications/mod_mongo/mod_mongo.h b/src/mod/applications/mod_mongo/mod_mongo.h index d264464294..57fdd09fdb 100644 --- a/src/mod/applications/mod_mongo/mod_mongo.h +++ b/src/mod/applications/mod_mongo/mod_mongo.h @@ -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 diff --git a/src/mod/applications/mod_mongo/mongo_conn.cpp b/src/mod/applications/mod_mongo/mongo_conn.cpp index ffcf1200d0..dd23b00310 100644 --- a/src/mod/applications/mod_mongo/mongo_conn.cpp +++ b/src/mod/applications/mod_mongo/mongo_conn.cpp @@ -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