This commit is contained in:
孟帅
2024-04-22 23:08:40 +08:00
parent 82483bd7b9
commit e144b12580
445 changed files with 17457 additions and 6708 deletions

View File

@@ -168,6 +168,7 @@ func (l *Lock) startWatchDog() {
}
case <-l.watchDog:
// 已经解锁
l.wg.Done()
return
}
}

View File

@@ -6,9 +6,12 @@
package lock_test
import (
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
"context"
"github.com/gogf/gf/v2/errors/gerror"
"hotgo/internal/library/hgrds/lock"
"runtime"
"sync"
"testing"
"time"
@@ -24,21 +27,43 @@ func TestDefaultLock(t *testing.T) {
if err != nil {
t.Error(err)
}
t.Log("A加锁成功")
time.Sleep(lock.DefaultTTL)
err = l.Unlock(context.Background())
if err != nil {
t.Error(err)
}
t.Log("A已释放锁")
}()
time.Sleep(time.Second)
go func() {
defer wg.Done()
l := lock.Mutex("test")
err := l.TryLock(context.Background())
if err != nil && !gerror.Is(err, lock.ErrLockFailed) {
t.Error(err)
for {
l := lock.Mutex("test")
err := l.TryLock(context.Background())
if err == nil {
t.Log("B加锁成功")
// 等待1s模拟业务
time.Sleep(time.Second)
err = l.Unlock(context.Background())
if err != nil {
t.Error(err)
}
t.Log("B已释放锁")
break
}
if gerror.Is(err, lock.ErrLockFailed) {
t.Log("B加锁失败等待1s重试...")
time.Sleep(time.Second)
} else {
t.Error(err)
return
}
}
}()
wg.Wait()
@@ -116,3 +141,12 @@ func TestNewLock2(t *testing.T) {
t.Errorf("count = %d", count)
}
}
func Test_Fix_watchDogMemoryLeak(t *testing.T) {
i := 0
for i < 5 {
TestDefaultLock(t)
t.Log("current goroutine num:", runtime.NumGoroutine())
i++
}
}