From b4624245c15935b3ee201f91865f2ff5050c25ad Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 17 Apr 2024 11:43:58 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lockx.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lockx.go b/lockx.go index 95b15c4..cf82215 100644 --- a/lockx.go +++ b/lockx.go @@ -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,