Files
timerx/priority/option.go
T

52 lines
938 B
Go
Raw Normal View History

2025-07-24 17:13:17 +08:00
package priority
import (
"time"
"github.com/yuninks/timerx/logger"
)
type Options struct {
2025-09-18 15:34:01 +08:00
getInterval time.Duration // 查询周期
2025-07-24 17:13:17 +08:00
updateInterval time.Duration // 更新间隔
expireTime time.Duration // 有效时间
2025-07-24 17:13:17 +08:00
logger logger.Logger
}
func defaultOptions() Options {
return Options{
2025-09-18 15:34:01 +08:00
getInterval: time.Second * 2,
updateInterval: time.Second * 4,
expireTime: time.Second * 8,
2025-07-24 17:13:17 +08:00
logger: logger.NewLogger(),
}
}
type Option func(*Options)
func newOptions(opts ...Option) Options {
o := defaultOptions()
for _, opt := range opts {
opt(&o)
}
return o
}
func SetLogger(log logger.Logger) Option {
return func(o *Options) {
o.logger = log
}
}
2025-09-18 15:34:01 +08:00
// 更新周期
2025-07-24 17:13:17 +08:00
func SetUpdateInterval(d time.Duration) Option {
if d.Abs() < time.Second {
2025-09-18 15:34:01 +08:00
d = time.Second * 5
2025-07-24 17:13:17 +08:00
}
return func(o *Options) {
o.updateInterval = d
2025-09-18 15:34:01 +08:00
o.expireTime = d*2 + time.Second
o.getInterval = d / 3
2025-07-24 17:13:17 +08:00
}
}