2024-04-17 19:52:43 +08:00
|
|
|
package lockx
|
|
|
|
|
|
2024-06-21 00:20:54 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2024-04-17 19:52:43 +08:00
|
|
|
|
|
|
|
|
type option struct {
|
2025-09-17 19:41:47 +08:00
|
|
|
lockTimeout time.Duration // 锁的超时时间
|
|
|
|
|
|
2025-09-17 19:12:55 +08:00
|
|
|
MaxRetryTimes int // 尝试次数
|
|
|
|
|
RetryInterval time.Duration // 尝试间隔
|
2025-09-17 19:41:47 +08:00
|
|
|
|
|
|
|
|
Expiry time.Duration // 单次刷新有效时间
|
2025-09-17 19:12:55 +08:00
|
|
|
RefreshPeriod time.Duration // 刷新间隔
|
2025-09-17 19:41:47 +08:00
|
|
|
|
|
|
|
|
logger Logger // 日志
|
2024-04-17 19:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defaultOption() *option {
|
|
|
|
|
return &option{
|
2025-09-17 19:12:55 +08:00
|
|
|
lockTimeout: time.Minute * 60,
|
|
|
|
|
Expiry: 5 * time.Second,
|
|
|
|
|
RefreshPeriod: 1 * time.Second,
|
|
|
|
|
MaxRetryTimes: 3,
|
|
|
|
|
RetryInterval: 100 * time.Millisecond,
|
|
|
|
|
logger: &print{},
|
2024-04-17 19:52:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:12:55 +08:00
|
|
|
var globalOpts []Option
|
2024-04-17 19:52:43 +08:00
|
|
|
|
|
|
|
|
// 设置
|
|
|
|
|
func InitOption(opts ...Option) {
|
2025-09-17 19:12:55 +08:00
|
|
|
globalOpts = opts
|
2024-04-17 19:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*option)
|
|
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// 最大锁定时间(默认1h)
|
2025-09-17 19:30:18 +08:00
|
|
|
func WithLockTimeout(t time.Duration) Option {
|
2024-04-17 19:52:43 +08:00
|
|
|
return func(o *option) {
|
|
|
|
|
o.lockTimeout = t
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-21 00:20:54 +08:00
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// 日志
|
2025-09-17 19:12:55 +08:00
|
|
|
func WithLogger(logger Logger) Option {
|
2024-06-21 00:20:54 +08:00
|
|
|
return func(o *option) {
|
|
|
|
|
o.logger = logger
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// key有效时间(会自动刷新)
|
2025-09-17 19:12:55 +08:00
|
|
|
func WithExpiry(expiry time.Duration) Option {
|
|
|
|
|
return func(o *option) {
|
|
|
|
|
o.Expiry = expiry
|
2025-09-17 19:41:47 +08:00
|
|
|
if o.Expiry/3 < o.RefreshPeriod {
|
|
|
|
|
o.RefreshPeriod = o.Expiry / 3
|
|
|
|
|
}
|
2025-09-17 19:12:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// 刷新间隔
|
2025-09-17 19:30:18 +08:00
|
|
|
func WithRefreshPeriod(period time.Duration) Option {
|
|
|
|
|
return func(o *option) {
|
|
|
|
|
o.RefreshPeriod = period
|
2025-09-17 19:41:47 +08:00
|
|
|
if o.RefreshPeriod*3 > o.Expiry {
|
|
|
|
|
o.Expiry = o.RefreshPeriod * 3
|
|
|
|
|
}
|
2025-09-17 19:30:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// 最大尝试次数
|
2025-09-17 19:30:18 +08:00
|
|
|
func WithMaxRetryTimes(times int) Option {
|
|
|
|
|
return func(o *option) {
|
|
|
|
|
o.MaxRetryTimes = times
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:41:47 +08:00
|
|
|
// 尝试间隔
|
2025-09-17 19:30:18 +08:00
|
|
|
func WithRetryInterval(interval time.Duration) Option {
|
|
|
|
|
return func(o *option) {
|
|
|
|
|
o.RetryInterval = interval
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 00:20:54 +08:00
|
|
|
type Logger interface {
|
|
|
|
|
Errorf(ctx context.Context, format string, v ...any)
|
2025-09-17 19:12:55 +08:00
|
|
|
Infof(ctx context.Context, format string, v ...any)
|
2024-06-21 00:20:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type print struct{}
|
|
|
|
|
|
|
|
|
|
func (*print) Errorf(ctx context.Context, format string, v ...any) {
|
|
|
|
|
log.Printf(format, v...)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 19:12:55 +08:00
|
|
|
func (*print) Infof(ctx context.Context, format string, v ...any) {
|
2024-06-21 00:20:54 +08:00
|
|
|
log.Printf(format, v...)
|
|
|
|
|
}
|