Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
8547c5c9
提交
8547c5c9
authored
9月 12, 2011
作者:
Tamas Cseke
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more general way to connect to mongo
上级
2fa8f110
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
45 行增加
和
30 行删除
+45
-30
mongo.conf.xml
conf/autoload_configs/mongo.conf.xml
+7
-1
mod_mongo.cpp
src/mod/applications/mod_mongo/mod_mongo.cpp
+9
-6
mod_mongo.h
src/mod/applications/mod_mongo/mod_mongo.h
+6
-7
mongo_conn.cpp
src/mod/applications/mod_mongo/mongo_conn.cpp
+23
-16
没有找到文件。
conf/autoload_configs/mongo.conf.xml
浏览文件 @
8547c5c9
<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"
/>
...
...
src/mod/applications/mod_mongo/mod_mongo.cpp
浏览文件 @
8547c5c9
...
...
@@ -15,7 +15,7 @@ static struct {
SWITCH_STANDARD_API
(
mongo_mapreduce_function
)
{
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
DBClient
Connection
*
conn
=
NULL
;
DBClient
Base
*
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
))
{
DBClient
Connection
*
conn
=
NULL
;
DBClient
Base
*
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
);
...
...
src/mod/applications/mod_mongo/mod_mongo.h
浏览文件 @
8547c5c9
#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
(
DBClient
Connection
**
connection
,
const
char
*
host
);
void
mongo_connection_destroy
(
DBClient
Connection
**
conn
);
switch_status_t
mongo_connection_create
(
DBClient
Base
**
connection
,
const
char
*
conn_str
);
void
mongo_connection_destroy
(
DBClient
Base
**
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
);
DBClient
Connection
*
mongo_connection_pool_get
(
mongo_connection_pool_t
*
conn_pool
);
switch_status_t
mongo_connection_pool_put
(
mongo_connection_pool_t
*
conn_pool
,
DBClient
Connection
*
conn
);
DBClient
Base
*
mongo_connection_pool_get
(
mongo_connection_pool_t
*
conn_pool
);
switch_status_t
mongo_connection_pool_put
(
mongo_connection_pool_t
*
conn_pool
,
DBClient
Base
*
conn
);
#endif
...
...
src/mod/applications/mod_mongo/mongo_conn.cpp
浏览文件 @
8547c5c9
...
...
@@ -10,24 +10,31 @@
scoped_conn.done();
*/
switch_status_t
mongo_connection_create
(
DBClient
Connection
**
connection
,
const
char
*
host
)
switch_status_t
mongo_connection_create
(
DBClient
Base
**
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
(
DBClient
Connection
**
conn
)
void
mongo_connection_destroy
(
DBClient
Base
**
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
;
DBClient
Connection
*
conn
=
NULL
;
DBClient
Base
*
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
((
DBClient
Connection
**
)
&
data
);
mongo_connection_destroy
((
DBClient
Base
**
)
&
data
);
}
switch_mutex_destroy
(
cpool
->
mutex
);
...
...
@@ -104,9 +111,9 @@ void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool)
}
DBClient
Connection
*
mongo_connection_pool_get
(
mongo_connection_pool_t
*
conn_pool
)
DBClient
Base
*
mongo_connection_pool_get
(
mongo_connection_pool_t
*
conn_pool
)
{
DBClient
Connection
*
conn
=
NULL
;
DBClient
Base
*
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
=
(
DBClient
Connection
*
)
data
;
}
else
if
(
mongo_connection_create
(
&
conn
,
conn_pool
->
host
)
==
SWITCH_STATUS_SUCCESS
)
{
conn
=
(
DBClient
Base
*
)
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
,
DBClient
Connection
*
conn
)
switch_status_t
mongo_connection_pool_put
(
mongo_connection_pool_t
*
conn_pool
,
DBClient
Base
*
conn
)
{
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论