1 Star 0 Fork 0

jmesyan/impetus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
timers.go 962 Bytes
一键复制 编辑 原始数据 按行查看 历史
jmesyan 提交于 2020-12-12 22:03 . init
package timers
import (
"gitee.com/jmesyan/impetus/utils/timer"
"time"
)
type Timers struct {
opts *Options
dispatcher *timer.Dispatcher
}
func NewTimer(opts ...Option) *Timers {
options := newOptions(opts...)
return &Timers{
opts: options,
}
}
func (t *Timers) OnInit() {
}
func (t *Timers) OnDestroy() {
}
func (t *Timers) OnRun(closeSig chan bool) {
go func() {
t.dispatcher = timer.NewDispatcher(t.opts.TimerDispatcherLen)
for {
select {
case <-closeSig:
return
case tt := <-t.dispatcher.ChanTimer:
tt.Cb()
}
}
}()
}
func (t *Timers) AfterFunc(d time.Duration, cb func()) *timer.Timer {
if t.opts.TimerDispatcherLen == 0 {
panic("invalid TimerDispatcherLen")
}
return t.dispatcher.AfterFunc(d, cb)
}
func (t *Timers) CronFunc(cronExpr *timer.CronExpr, cb func()) *timer.Cron {
if t.opts.TimerDispatcherLen == 0 {
panic("invalid TimerDispatcherLen")
}
return t.dispatcher.CronFunc(cronExpr, cb)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jmesyan/impetus.git
git@gitee.com:jmesyan/impetus.git
jmesyan
impetus
impetus
v1.0.3

搜索帮助