From 8547c5c9957e7b395569a2222a020f80727d4792 Mon Sep 17 00:00:00 2001 From: Tamas Cseke Date: Mon, 12 Sep 2011 09:16:56 +0200 Subject: [PATCH] more general way to connect to mongo --- conf/autoload_configs/mongo.conf.xml | 8 +++- src/mod/applications/mod_mongo/mod_mongo.cpp | 15 ++++--- src/mod/applications/mod_mongo/mod_mongo.h | 13 +++---- src/mod/applications/mod_mongo/mongo_conn.cpp | 39 +++++++++++-------- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/conf/autoload_configs/mongo.conf.xml b/conf/autoload_configs/mongo.conf.xml index ff8daed72a..8423645e95 100644 --- a/conf/autoload_configs/mongo.conf.xml +++ b/conf/autoload_configs/mongo.conf.xml @@ -1,6 +1,12 @@ - + + diff --git a/src/mod/applications/mod_mongo/mod_mongo.cpp b/src/mod/applications/mod_mongo/mod_mongo.cpp index 52e1eadf31..e0b93d85b4 100644 --- a/src/mod/applications/mod_mongo/mod_mongo.cpp +++ b/src/mod/applications/mod_mongo/mod_mongo.cpp @@ -15,7 +15,7 @@ static struct { SWITCH_STANDARD_API(mongo_mapreduce_function) { switch_status_t status = SWITCH_STATUS_SUCCESS; - DBClientConnection *conn = NULL; + DBClientBase *conn = NULL; char *ns = NULL, *json_query = NULL; ns = strdup(cmd); @@ -88,7 +88,7 @@ SWITCH_STANDARD_API(mongo_find_one_function) if (!zstr(ns) && !zstr(json_query) && !zstr(json_fields)) { - DBClientConnection *conn = NULL; + DBClientBase *conn = NULL; try { BSONObj query = fromjson(json_query); @@ -124,7 +124,7 @@ static switch_status_t config(void) const char *cf = "mongo.conf"; switch_xml_t cfg, xml, settings, param; switch_status_t status = SWITCH_STATUS_SUCCESS; - const char *host = "127.0.0.1"; + const char *conn_str = "127.0.0.1"; switch_size_t min_connections = 1, max_connections = 1; if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { @@ -139,7 +139,10 @@ static switch_status_t config(void) int tmp; if (!strcmp(var, "host")) { - host = val; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "'host' is deprecated. use 'connection-string'\n"); + conn_str = val; + } else if (!strcmp(var, "connection-string")) { + conn_str = val; } else if (!strcmp(var, "min-connections")) { if ((tmp = atoi(val)) > 0) { min_connections = tmp; @@ -158,11 +161,11 @@ static switch_status_t config(void) } } - if (mongo_connection_pool_create(&globals.conn_pool, min_connections, max_connections, host) != SWITCH_STATUS_SUCCESS) { + if (mongo_connection_pool_create(&globals.conn_pool, min_connections, max_connections, conn_str) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Can't create connection pool\n"); status = SWITCH_STATUS_GENERR; } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mongo connection pool created [%s %d/%d]\n", host, (int)min_connections, (int)max_connections); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mongo connection pool created [%s %d/%d]\n", conn_str, (int)min_connections, (int)max_connections); } switch_xml_free(xml); diff --git a/src/mod/applications/mod_mongo/mod_mongo.h b/src/mod/applications/mod_mongo/mod_mongo.h index 91c14bb29b..d264464294 100644 --- a/src/mod/applications/mod_mongo/mod_mongo.h +++ b/src/mod/applications/mod_mongo/mod_mongo.h @@ -1,7 +1,6 @@ #ifndef MOD_MONGO_H #define MOD_MONGO_H - #include #include #include @@ -10,7 +9,7 @@ using namespace mongo; typedef struct { - char *host; + char *conn_str; switch_size_t min_connections; switch_size_t max_connections; @@ -22,16 +21,16 @@ typedef struct { } mongo_connection_pool_t; -switch_status_t mongo_connection_create(DBClientConnection **connection, const char *host); -void mongo_connection_destroy(DBClientConnection **conn); +switch_status_t mongo_connection_create(DBClientBase **connection, const char *conn_str); +void mongo_connection_destroy(DBClientBase **conn); switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool, switch_size_t min_connections, switch_size_t max_connections, - const char *host); + const char *conn_str); void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool); -DBClientConnection *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool); -switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientConnection *conn); +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); #endif diff --git a/src/mod/applications/mod_mongo/mongo_conn.cpp b/src/mod/applications/mod_mongo/mongo_conn.cpp index ee702b8470..ffcf1200d0 100644 --- a/src/mod/applications/mod_mongo/mongo_conn.cpp +++ b/src/mod/applications/mod_mongo/mongo_conn.cpp @@ -10,24 +10,31 @@ scoped_conn.done(); */ -switch_status_t mongo_connection_create(DBClientConnection **connection, const char *host) +switch_status_t mongo_connection_create(DBClientBase **connection, const char *conn_str) { - DBClientConnection *conn = new DBClientConnection(); + DBClientBase *conn = NULL; + string conn_string(conn_str), err_msg; + ConnectionString cs = ConnectionString::parse(conn_string, err_msg); + + 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; + } try { - conn->connect(host); + conn = cs.connect(err_msg); } catch (DBException &e) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't connect to mongo [%s]\n", host); + 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; } *connection = conn; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", host); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", conn_str); return SWITCH_STATUS_SUCCESS; } -void mongo_connection_destroy(DBClientConnection **conn) +void mongo_connection_destroy(DBClientBase **conn) { switch_assert(*conn != NULL); delete *conn; @@ -36,12 +43,12 @@ void mongo_connection_destroy(DBClientConnection **conn) } switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool, switch_size_t min_connections, switch_size_t max_connections, - const char *host) + const char *conn_str) { switch_memory_pool_t *pool = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; mongo_connection_pool_t *cpool = NULL; - DBClientConnection *conn = NULL; + DBClientBase *conn = NULL; if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) { return status; @@ -61,13 +68,13 @@ switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool cpool->min_connections = min_connections; cpool->max_connections = max_connections; - cpool->host = switch_core_strdup(pool, host); + cpool->conn_str = switch_core_strdup(pool, conn_str); cpool->pool = pool; for (cpool->size = 0; cpool->size < min_connections; cpool->size++) { - if (mongo_connection_create(&conn, host) == SWITCH_STATUS_SUCCESS) { + if (mongo_connection_create(&conn, conn_str) == SWITCH_STATUS_SUCCESS) { mongo_connection_pool_put(cpool, conn); } else { break; @@ -94,7 +101,7 @@ void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool) switch_assert(cpool != NULL); while (switch_queue_trypop(cpool->connections, &data) == SWITCH_STATUS_SUCCESS) { - mongo_connection_destroy((DBClientConnection **)&data); + mongo_connection_destroy((DBClientBase **)&data); } switch_mutex_destroy(cpool->mutex); @@ -104,9 +111,9 @@ void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool) } -DBClientConnection *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool) +DBClientBase *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool) { - DBClientConnection *conn = NULL; + DBClientBase *conn = NULL; void *data = NULL; switch_assert(conn_pool != NULL); @@ -114,8 +121,8 @@ DBClientConnection *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool switch_mutex_lock(conn_pool->mutex); if (switch_queue_trypop(conn_pool->connections, &data) == SWITCH_STATUS_SUCCESS) { - conn = (DBClientConnection *) data; - } else if (mongo_connection_create(&conn, conn_pool->host) == SWITCH_STATUS_SUCCESS) { + conn = (DBClientBase *) data; + } else if (mongo_connection_create(&conn, conn_pool->conn_str) == SWITCH_STATUS_SUCCESS) { if (++conn_pool->size > conn_pool->max_connections) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Connection pool is empty. You may want to increase 'max-connections'\n"); } @@ -130,7 +137,7 @@ DBClientConnection *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, DBClientConnection *conn) +switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn) { switch_status_t status = SWITCH_STATUS_SUCCESS;