唯一的值改为uuid

This commit is contained in:
Yun
2025-06-25 16:44:44 +08:00
parent cd0273f4b2
commit f4e1e3fdd1
4 changed files with 29 additions and 14 deletions
+4 -1
View File
@@ -2,7 +2,10 @@ module github.com/yuninks/lockx
go 1.20 go 1.20
require github.com/go-redis/redis/v8 v8.11.5 require (
github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.6.0
)
require ( require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect
+2
View File
@@ -5,6 +5,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
+5 -4
View File
@@ -2,10 +2,10 @@ package lockx
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/google/uuid"
) )
// 全局锁 // 全局锁
@@ -17,16 +17,17 @@ type globalLock struct {
value string value string
} }
func NewGlobalLock(ctx context.Context, red redis.UniversalClient, uniqueKey string) *globalLock { func NewGlobalLock(ctx context.Context, red redis.UniversalClient, uniqueKey string) *globalLock {
ctx, cancel := context.WithTimeout(ctx, opt.lockTimeout) ctx, cancel := context.WithTimeout(ctx, opt.lockTimeout)
u, _ := uuid.NewV7()
return &globalLock{ return &globalLock{
redis: red, redis: red,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
uniqueKey: uniqueKey, uniqueKey: uniqueKey,
value: fmt.Sprintf("%d", time.Now().UnixNano()), value: u.String(),
} }
} }
+18 -9
View File
@@ -3,10 +3,11 @@ package lockx_test
import ( import (
"context" "context"
"fmt" "fmt"
"sync"
"testing" "testing"
"github.com/yuninks/lockx"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/yuninks/lockx"
) )
var Redis *redis.Client var Redis *redis.Client
@@ -28,7 +29,7 @@ var Redis *redis.Client
func TestLockx(t *testing.T) { func TestLockx(t *testing.T) {
client := redis.NewClient(&redis.Options{ client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1" + ":" + "6379", Addr: "127.0.0.1" + ":" + "6379",
Password: "", // no password set Password: "123456", // no password set
DB: 0, // use default DB DB: 0, // use default DB
}) })
if client == nil { if client == nil {
@@ -40,13 +41,21 @@ func TestLockx(t *testing.T) {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
wg := sync.WaitGroup{}
for i := 0; i < 10000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
lock := lockx.NewGlobalLock(ctx, client, "lockx:test") lock := lockx.NewGlobalLock(ctx, client, "lockx:test")
if !lock.Lock() {
fmt.Println("lock error")
}
defer lock.Unlock() defer lock.Unlock()
if !lock.Lock() {
fmt.Println("ssss") fmt.Println("lock error", i)
return
}
fmt.Println("ssss", i)
}(i)
}
wg.Wait()
} }