添加option

This commit is contained in:
Administrator
2024-04-17 11:43:58 +00:00
parent 302914eb94
commit b4624245c1
+33 -1
View File
@@ -18,8 +18,40 @@ type globalLock struct {
value string
}
type option struct {
lockTimeout time.Time // 锁的超时时间
}
func defaultOption() *option {
return &option{
lockTimeout: time.Minute * 10 // 默认是10分钟
}
}
var opt *option
func init() {
opt = defaultOption()
}
// 设置
func InitOption(opts ...Option) {
for _,app := range opts {
app(opt)
}
}
type Option func(*option)
func SetTimeout(t time.Time) Option {
return func(o *option) {
o.lockTimeout = t
}
}
func NewGlobalLock(ctx context.Context, red *redis.Client, uniqueKey string) *globalLock {
ctx, cancel := context.WithTimeout(ctx, time.Minute*10)
ctx, cancel := context.WithTimeout(ctx, opt.lockTimeout)
return &globalLock{
redis: red,
ctx: ctx,