From 12b75b9d82c2e3a58c20dd856e2a9718b5c67d06 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 28 Dec 2023 20:42:11 +0800 Subject: [PATCH] optimize mutex lock fn --- cache/locks.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cache/locks.go b/cache/locks.go index d15c842..f4c5f33 100644 --- a/cache/locks.go +++ b/cache/locks.go @@ -43,13 +43,16 @@ func (l *Locks[K]) GetLock(ctx context.Context, gMut *sync.Mutex, keys ...K) *sy if ok { return lo } + num := l.numFn() + if num == 1 { + return gMut + } gMut.Lock() defer gMut.Unlock() lo, ok = l.m.Load(k) if ok { return lo } - num := l.numFn() if num <= 0 { lo = &sync.Mutex{} l.m.Store(k, lo)