Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
39f6d107
提交
39f6d107
authored
10月 21, 2015
作者:
William King
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FS-8377 Adding expanded support for limit_* functionality for mod_hiredis
上级
80111e7d
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
67 行增加
和
30 行删除
+67
-30
mod_hiredis.c
src/mod/applications/mod_hiredis/mod_hiredis.c
+58
-29
mod_hiredis.h
src/mod/applications/mod_hiredis/mod_hiredis.h
+9
-1
没有找到文件。
src/mod/applications/mod_hiredis/mod_hiredis.c
浏览文件 @
39f6d107
...
...
@@ -41,7 +41,7 @@ SWITCH_STANDARD_APP(raw_app)
switch_channel_t
*
channel
=
switch_core_session_get_channel
(
session
);
char
*
response
=
NULL
,
*
profile_name
=
NULL
,
*
cmd
=
NULL
;
hiredis_profile_t
*
profile
=
NULL
;
if
(
!
zstr
(
data
)
)
{
profile_name
=
strdup
(
data
);
}
else
{
...
...
@@ -63,7 +63,7 @@ SWITCH_STANDARD_APP(raw_app)
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: Unable to locate profile[%s]
\n
"
,
profile_name
);
return
;
}
if
(
hiredis_profile_execute_sync
(
profile
,
cmd
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] because [%s]
\n
"
,
profile_name
,
cmd
,
response
);
}
...
...
@@ -81,13 +81,13 @@ SWITCH_STANDARD_API(raw_api)
hiredis_profile_t
*
profile
=
NULL
;
char
*
data
=
NULL
,
*
input
=
NULL
,
*
response
=
NULL
;
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
if
(
!
zstr
(
cmd
)
)
{
input
=
strdup
(
cmd
);
}
else
{
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
if
(
(
data
=
strchr
(
input
,
' '
)))
{
*
data
=
'\0'
;
data
++
;
...
...
@@ -101,7 +101,7 @@ SWITCH_STANDARD_API(raw_api)
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: Unable to locate profile[%s]
\n
"
,
input
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
if
(
hiredis_profile_execute_sync
(
profile
,
data
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] reason:[%s]
\n
"
,
input
,
data
,
response
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
...
...
@@ -114,24 +114,26 @@ SWITCH_STANDARD_API(raw_api)
return
status
;
}
/*
SWITCH_LIMIT_INCR(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource,
/*
SWITCH_LIMIT_INCR(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource,
const int max, const int interval)
*/
SWITCH_LIMIT_INCR
(
hiredis_limit_incr
)
{
switch_channel_t
*
channel
=
switch_core_session_get_channel
(
session
);
hiredis_profile_t
*
profile
=
NULL
;
char
*
hashkey
=
NULL
,
*
response
=
NULL
;
char
*
hashkey
=
NULL
,
*
response
=
NULL
,
*
limit_key
=
NULL
;
int64_t
count
=
0
;
/* Redis defines the incr action as to be performed on a 64 bit signed integer */
time_t
now
=
switch_epoch_time_now
(
NULL
);
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
hiredis_limit_pvt_t
*
limit_pvt
=
NULL
;
switch_memory_pool_t
*
session_pool
=
switch_core_session_get_pool
(
session
);
if
(
zstr
(
realm
)
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: realm must be defined
\n
"
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
profile
=
switch_core_hash_find
(
mod_hiredis_globals
.
profiles
,
realm
);
if
(
!
profile
)
{
...
...
@@ -140,11 +142,13 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
}
if
(
interval
)
{
hashkey
=
switch_mprintf
(
"incr
%s_%d"
,
resource
,
now
%
interval
);
limit_key
=
switch_mprintf
(
"
%s_%d"
,
resource
,
now
%
interval
);
}
else
{
hashkey
=
switch_mprintf
(
"incr
%s"
,
resource
);
limit_key
=
switch_mprintf
(
"
%s"
,
resource
);
}
hashkey
=
switch_mprintf
(
"incr %s"
,
limit_key
);
if
(
hiredis_profile_execute_sync
(
profile
,
hashkey
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] because [%s]
\n
"
,
realm
,
hashkey
,
response
);
switch_channel_set_variable
(
channel
,
"hiredis_raw_response"
,
response
);
...
...
@@ -153,6 +157,14 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
switch_channel_set_variable
(
channel
,
"hiredis_raw_response"
,
response
);
limit_pvt
=
switch_core_alloc
(
session_pool
,
sizeof
(
hiredis_limit_pvt_t
));
limit_pvt
->
next
=
switch_channel_get_private
(
channel
,
"hiredis_limit_pvt"
);
limit_pvt
->
realm
=
switch_core_strdup
(
session_pool
,
realm
);
limit_pvt
->
resource
=
switch_core_strdup
(
session_pool
,
resource
);
limit_pvt
->
limit_key
=
switch_core_strdup
(
session_pool
,
limit_key
);
limit_pvt
->
inc
=
1
;
switch_channel_set_private
(
channel
,
"hiredis_limit_pvt"
,
limit_pvt
);
count
=
atoll
(
response
);
if
(
!
count
||
count
>
max
)
{
...
...
@@ -160,6 +172,7 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
}
done
:
switch_safe_free
(
limit_key
);
switch_safe_free
(
response
);
switch_safe_free
(
hashkey
);
return
status
;
...
...
@@ -174,32 +187,48 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release)
hiredis_profile_t
*
profile
=
switch_core_hash_find
(
mod_hiredis_globals
.
profiles
,
realm
);
char
*
hashkey
=
NULL
,
*
response
=
NULL
;
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
hiredis_limit_pvt_t
*
limit_pvt
=
switch_channel_get_private
(
channel
,
"hiredis_limit_pvt"
);
if
(
!
zstr
(
realm
)
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: realm must be defined
\n
"
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
if
(
!
profile
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: Unable to locate profile[%s]
\n
"
,
realm
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
/* If realm and resource are NULL, then clear all of the limits */
if
(
!
realm
&&
!
resource
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis does not yet support release on NULL realm[%s] and resource[%s]
\n
"
,
realm
,
resource
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
hiredis_limit_pvt_t
*
tmp
=
limit_pvt
;
hashkey
=
switch_mprintf
(
"decr %s"
,
resource
);
while
(
tmp
)
{
hashkey
=
switch_mprintf
(
"decr %s"
,
tmp
->
limit_key
);
limit_pvt
=
tmp
->
next
;
if
(
hiredis_profile_execute_sync
(
profile
,
hashkey
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] because [%s]
\n
"
,
tmp
->
realm
,
hashkey
,
response
);
}
tmp
=
limit_pvt
;
switch_safe_free
(
response
);
switch_safe_free
(
hashkey
);
}
}
else
{
hashkey
=
switch_mprintf
(
"decr %s"
,
limit_pvt
->
limit_key
);
if
(
hiredis_profile_execute_sync
(
profile
,
hashkey
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] because [%s]
\n
"
,
realm
,
hashkey
,
response
);
switch_channel_set_variable
(
channel
,
"hiredis_raw_response"
,
response
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
if
(
hiredis_profile_execute_sync
(
profile
,
hashkey
,
&
response
)
!=
SWITCH_STATUS_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: profile[%s] error executing [%s] because [%s]
\n
"
,
realm
,
hashkey
,
response
);
switch_channel_set_variable
(
channel
,
"hiredis_raw_response"
,
response
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
switch_channel_set_variable
(
channel
,
"hiredis_raw_response"
,
response
);
done
:
switch_safe_free
(
response
);
switch_safe_free
(
hashkey
);
...
...
@@ -212,14 +241,14 @@ SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resourc
SWITCH_LIMIT_USAGE
(
hiredis_limit_usage
)
{
hiredis_profile_t
*
profile
=
switch_core_hash_find
(
mod_hiredis_globals
.
profiles
,
realm
);
int64_t
count
=
0
;
/* Redis defines the incr action as to be performed on a 64 bit signed integer */
int64_t
count
=
0
;
/* Redis defines the incr action as to be performed on a 64 bit signed integer */
char
*
hashkey
=
NULL
,
*
response
=
NULL
;
if
(
!
zstr
(
realm
)
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: realm must be defined
\n
"
);
goto
err
;
}
if
(
!
profile
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: Unable to locate profile[%s]
\n
"
,
realm
);
goto
err
;
...
...
@@ -237,7 +266,7 @@ SWITCH_LIMIT_USAGE(hiredis_limit_usage)
switch_safe_free
(
response
);
switch_safe_free
(
hashkey
);
return
count
;
err
:
switch_safe_free
(
response
);
switch_safe_free
(
hashkey
);
...
...
@@ -251,7 +280,7 @@ SWITCH_LIMIT_RESET(hiredis_limit_reset)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: unable to globally reset hiredis limit resources. Use 'hiredis_raw set resource_name 0'
\n
"
);
return
SWITCH_STATUS_GENERR
;
}
...
...
@@ -263,12 +292,12 @@ SWITCH_LIMIT_INTERVAL_RESET(hiredis_limit_interval_reset)
hiredis_profile_t
*
profile
=
switch_core_hash_find
(
mod_hiredis_globals
.
profiles
,
realm
);
switch_status_t
status
=
SWITCH_STATUS_SUCCESS
;
char
*
hashkey
=
NULL
,
*
response
=
NULL
;
if
(
!
zstr
(
realm
)
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: realm must be defined
\n
"
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
}
if
(
!
profile
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"hiredis: Unable to locate profile[%s]
\n
"
,
realm
);
switch_goto_status
(
SWITCH_STATUS_GENERR
,
done
);
...
...
@@ -306,7 +335,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_hiredis_load)
mod_hiredis_globals
.
pool
=
pool
;
switch_core_hash_init
(
&
(
mod_hiredis_globals
.
profiles
));
if
(
mod_hiredis_do_config
()
!=
SWITCH_STATUS_SUCCESS
)
{
return
SWITCH_STATUS_GENERR
;
}
...
...
src/mod/applications/mod_hiredis/mod_hiredis.h
浏览文件 @
39f6d107
...
...
@@ -29,9 +29,17 @@ typedef struct hiredis_profile_s {
int
debug
;
hiredis_connection_t
*
conn
;
hiredis_connection_t
*
conn_head
;
hiredis_connection_t
*
conn_head
;
}
hiredis_profile_t
;
typedef
struct
hiredis_limit_pvt_s
{
char
*
realm
;
char
*
resource
;
char
*
limit_key
;
int
inc
;
struct
hiredis_limit_pvt_s
*
next
;
}
hiredis_limit_pvt_t
;
switch_status_t
mod_hiredis_do_config
();
switch_status_t
hiredis_profile_create
(
hiredis_profile_t
**
new_profile
,
char
*
name
,
uint8_t
port
);
switch_status_t
hiredis_profile_destroy
(
hiredis_profile_t
**
old_profile
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论