2 Star 2 Fork 8

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
periodic_once.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-01-22 10:36 . 这两都提示推荐使用RollingOnce
package coroutine
import (
"gitee.com/quant1x/gox/cron"
"gitee.com/quant1x/gox/logger"
"gitee.com/quant1x/gox/runtime"
"sync"
"sync/atomic"
"time"
)
const (
cronPreMinute = "*/1 * * * *"
cronPreSecond = "*/1 * * * * ?"
cronDefaultInterval = "@every 1s"
periodicInitTime = "09:00:00"
)
// PeriodicOnce 周期性懒加载机制
//
// Deprecated: 推荐 RollingOnce [wangfeng on 2024/1/22 10:33]
type PeriodicOnce struct {
done uint32
m sync.Mutex
once sync.Once
timer *cron.Cron
date string
lazyFunc func()
}
func (o *PeriodicOnce) Do(f func()) {
if o.lazyFunc == nil {
o.lazyFunc = f
}
o.once.Do(o.initTimer)
if atomic.LoadUint32(&o.done) == 0 {
o.doSlow(f)
}
}
func (o *PeriodicOnce) doSlow(f func()) {
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
defer atomic.StoreUint32(&o.done, 1)
f()
}
}
func (o *PeriodicOnce) isExpired() bool {
currentDate := o.date
if currentDate < onceDefaultDate {
currentDate = onceDefaultDate
}
now := time.Now()
timestamp := now.Format(time.TimeOnly)
if timestamp >= periodicInitTime {
currentDate = now.Format(time.DateOnly)
}
if currentDate > o.date {
return true
}
return false
}
func (o *PeriodicOnce) initTimer() {
if o.timer == nil {
funcName := runtime.FuncName(o.lazyFunc)
o.timer = cron.New(cron.WithSeconds())
_, err := o.timer.AddFuncWithSkipIfStillRunning(cronDefaultInterval, func() {
if o.isExpired() {
if runtime.Debug() {
logger.Infof("PeriodicOnce[%s]: reset begin", funcName)
}
o.Reset()
if runtime.Debug() {
logger.Infof("PeriodicOnce[%s]: reset end", funcName)
}
}
})
if err == nil {
o.timer.Start()
}
}
}
// Reset 被动的方式重置初始化done标志
func (o *PeriodicOnce) Reset() {
if atomic.LoadUint32(&o.done) == 1 {
o.resetSlow()
}
}
func (o *PeriodicOnce) resetSlow() {
o.m.Lock()
defer o.m.Unlock()
if o.done == 1 {
atomic.StoreUint32(&o.done, 0)
// 重置日期
now := time.Now()
o.date = now.Format(time.DateOnly)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.21.2

搜索帮助

D67c1975 1850385 1daf7b77 1850385