more general way to connect to mongo

This commit is contained in:
Tamas Cseke 2011-09-12 09:16:56 +02:00
parent 2fa8f11002
commit 8547c5c995
4 changed files with 45 additions and 30 deletions

View File

@ -1,6 +1,12 @@
<configuration name="mongo.conf">
<settings>
<param name="host" value="127.0.0.1:27017"/>
<!--
connection-string handles different ways to connect to mongo
samples:
server:port
foo/server:port,server:port SET
-->
<param name="connection-string" value="127.0.0.1:27017"/>
<param name="min-connections" value="10"/>
<param name="max-connections" value="100"/>

View File

@ -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);

View File

@ -1,7 +1,6 @@
#ifndef MOD_MONGO_H
#define MOD_MONGO_H
#include <client/dbclient.h>
#include <client/connpool.h>
#include <db/json.h>
@ -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

View File

@ -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;