Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
b85a465c
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b85a465c
编写于
3月 30, 2018
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modules Timer API: timer handling implemented.
上级
561039c1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
1 deletion
+58
-1
src/module.c
src/module.c
+58
-1
未找到文件。
src/module.c
浏览文件 @
b85a465c
...
...
@@ -4039,7 +4039,7 @@ int RM_GetClusterNodeInfo(RedisModuleCtx *ctx, const char *id, char *ip, char *m
static
rax
*
Timers
;
/* The radix tree of all the timers sorted by expire. */
long
long
aeTimer
=
-
1
;
/* Main event loop (ae.c) timer identifier. */
typedef
int64_t
(
*
RedisModuleTimerProc
)(
RedisModuleCtx
*
ctx
,
void
*
data
);
typedef
void
(
*
RedisModuleTimerProc
)(
RedisModuleCtx
*
ctx
,
void
*
data
);
/* The timer descriptor, stored as value in the radix tree. */
typedef
struct
RedisModuleTimer
{
...
...
@@ -4048,6 +4048,44 @@ typedef struct RedisModuleTimer {
void
*
data
;
/* Private data for the callback. */
}
RedisModuleTimer
;
/* This is the timer handler that is called by the main event loop. We schedule
* this timer to be called when the nearest of our module timers will expire. */
int
moduleTimerHandler
(
struct
aeEventLoop
*
eventLoop
,
long
long
id
,
void
*
clientData
)
{
UNUSED
(
eventLoop
);
UNUSED
(
id
);
UNUSED
(
clientData
);
/* To start let's try to fire all the timers already expired. */
raxIterator
ri
;
raxStart
(
&
ri
,
Timers
);
uint64_t
now
=
ustime
();
long
long
next_period
=
0
;
while
(
1
)
{
raxSeek
(
&
ri
,
"^"
,
NULL
,
0
);
if
(
!
raxNext
(
&
ri
))
break
;
uint64_t
expiretime
;
memcpy
(
&
expiretime
,
ri
.
key
,
sizeof
(
expiretime
));
expiretime
=
ntohu64
(
expiretime
);
if
(
now
>=
expiretime
)
{
RedisModuleTimer
*
timer
=
ri
.
data
;
RedisModuleCtx
ctx
=
REDISMODULE_CTX_INIT
;
ctx
.
module
=
timer
->
module
;
timer
->
callback
(
&
ctx
,
timer
->
data
);
moduleFreeContext
(
&
ctx
);
raxRemove
(
Timers
,(
unsigned
char
*
)
&
ri
.
key
,
ri
.
key_len
,
NULL
);
zfree
(
timer
);
}
else
{
next_period
=
expiretime
-
now
;
break
;
}
}
raxStop
(
&
ri
);
/* Reschedule the next timer or cancel it. */
return
(
raxSize
(
Timers
)
>
0
)
?
next_period
:
AE_NOMORE
;
}
/* Create a new timer that will fire after `period` milliseconds, and will call
* the specified function using `data` as argument. The returned timer ID can be
* used to get information from the timer or to stop it before it fires. */
...
...
@@ -4071,6 +4109,25 @@ RedisModuleTimerID RM_CreateTimer(RedisModuleCtx *ctx, mstime_t period, RedisMod
/* We need to install the main event loop timer if it's not already
* installed, or we may need to refresh its period if we just installed
* a timer that will expire sooner than any other else. */
if
(
aeTimer
!=
-
1
)
{
raxIterator
ri
;
raxStart
(
&
ri
,
Timers
);
raxSeek
(
&
ri
,
"^"
,
NULL
,
0
);
raxNext
(
&
ri
);
if
(
memcmp
(
ri
.
key
,
&
key
,
sizeof
(
key
))
==
0
)
{
/* This is the first key, we need to re-install the timer according
* to the just added event. */
aeDeleteTimeEvent
(
server
.
el
,
aeTimer
);
aeTimer
=
-
1
;
}
raxStop
(
&
ri
);
}
/* If we have no main timer (the old one was invalidated, or this is the
* first module timer we have), install one. */
if
(
aeTimer
==
-
1
)
aeTimer
=
aeCreateTimeEvent
(
server
.
el
,
period
,
moduleTimerHandler
,
NULL
,
NULL
);
return
key
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录