代码拉取完成,页面将自动刷新
package timer
import (
"gitee.com/xiaoxiaoweizia/leaf/conf"
"gitee.com/xiaoxiaoweizia/leaf/log"
"runtime"
"time"
)
// one dispatcher per goroutine (goroutine not safe)
type Dispatcher struct {
ChanTimer chan *Timer
}
func NewDispatcher(l int) *Dispatcher {
disp := new(Dispatcher)
disp.ChanTimer = make(chan *Timer, l)
return disp
}
// Timer
type Timer struct {
t *time.Timer
cb func()
}
func (t *Timer) Stop() {
t.t.Stop()
t.cb = nil
}
func (t *Timer) Cb() {
defer func() {
t.cb = nil
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
if t.cb != nil {
t.cb()
}
}
func (disp *Dispatcher) AfterFunc(d time.Duration, cb func()) *Timer {
t := new(Timer)
t.cb = cb
t.t = time.AfterFunc(d, func() {
disp.ChanTimer <- t
})
return t
}
// Cron
type Cron struct {
t *Timer
}
func (c *Cron) Stop() {
if c.t != nil {
c.t.Stop()
}
}
func (disp *Dispatcher) CronFunc(cronExpr *CronExpr, _cb func()) *Cron {
c := new(Cron)
now := time.Now()
nextTime := cronExpr.Next(now)
if nextTime.IsZero() {
return c
}
// callback
var cb func()
cb = func() {
defer _cb()
now := time.Now()
nextTime := cronExpr.Next(now)
if nextTime.IsZero() {
return
}
c.t = disp.AfterFunc(nextTime.Sub(now), cb)
}
c.t = disp.AfterFunc(nextTime.Sub(now), cb)
return c
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。