``` feat(pet): 重构宠物繁殖系统,添加蛋孵化功能
This commit is contained in:
53
common/utils/timer/timer.go
Normal file
53
common/utils/timer/timer.go
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
|
||||
//
|
||||
// mit license
|
||||
package timer
|
||||
|
||||
import "time"
|
||||
|
||||
type Next interface {
|
||||
Next(time.Time) time.Time
|
||||
}
|
||||
|
||||
// 定时器接口
|
||||
type Timer interface {
|
||||
// 一次性定时器
|
||||
AfterFunc(expire time.Duration, callback func()) TimeNoder
|
||||
|
||||
// 周期性定时器
|
||||
ScheduleFunc(expire time.Duration, callback func()) TimeNoder
|
||||
|
||||
// 自定义下次的时间
|
||||
CustomFunc(n Next, callback func()) TimeNoder
|
||||
|
||||
// 运行
|
||||
Run()
|
||||
|
||||
// 停止所有定时器
|
||||
Stop()
|
||||
}
|
||||
|
||||
// 停止单个定时器
|
||||
type TimeNoder interface {
|
||||
Stop() bool
|
||||
// 重置时间器
|
||||
Reset(expire time.Duration) bool
|
||||
}
|
||||
|
||||
// 定时器构造函数
|
||||
func NewTimer(opt ...Option) Timer {
|
||||
var o option
|
||||
for _, cb := range opt {
|
||||
cb(&o)
|
||||
}
|
||||
|
||||
if o.timeWheel {
|
||||
return newTimeWheel()
|
||||
}
|
||||
|
||||
if o.minHeap {
|
||||
return newMinHeap()
|
||||
}
|
||||
|
||||
return newTimeWheel()
|
||||
}
|
||||
Reference in New Issue
Block a user