1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2026-07-14 08:17:26 +08:00

fix: 修复多处并发安全问题及锁实现重构

locker: 导出 RWLocker/EmptyLocker 接口, 新增 WithRWLocker, 重构
  SourceLocker 竞态、Synchronize 数据竞态

collections/linked_list: 补齐 ForEach/Size/GetAt/RemoveAt/ToList
  等方法的锁保护
collections/queue,stack: Peek/Size/IsEmpty 加锁
collections/list/linked_list: Front/Back/GetAt 加锁
maps: rwlock_map.Exist/Count, hash_map.Count 加锁
delay_queue/mem_store: Pop 原子化、Len/IsEmpty 加锁
cleanup_guard: Run() 检查 enable 标志
sets/ketama: 修复接口变更导致的 nil 空指针
This commit is contained in:
Charlie
2026-06-05 10:45:08 +08:00
parent b14262e9eb
commit c44a325384
17 changed files with 178 additions and 82 deletions
+6
View File
@@ -20,5 +20,11 @@ func (g *CleanupGuard) Enable() {
} }
func (g *CleanupGuard) Run() { func (g *CleanupGuard) Run() {
g.mutex.Lock()
enabled := g.enable
g.mutex.Unlock()
if enabled {
g.fn() g.fn()
} }
}
+43 -15
View File
@@ -95,20 +95,22 @@ func (l *LinkedList[T]) PushTail() {
} }
func (l *LinkedList[T]) Exist(v T) bool { func (l *LinkedList[T]) Exist(v T) bool {
ret := false l.mu.RLock()
l.ForEach(func(t T) bool { defer l.mu.RUnlock()
// if t == v {
// ret = true for current := l.head; current != nil; current = current.next {
// return true if any(current.item) == any(v) {
// } return true
}
}
return false return false
})
return ret
} }
func (l *LinkedList[T]) RemoveAt(index int) { func (l *LinkedList[T]) RemoveAt(index int) {
l.mu.Lock()
defer l.mu.Unlock()
var i int var i int
var prev *linkedNode[T] var prev *linkedNode[T]
@@ -130,15 +132,22 @@ func (l *LinkedList[T]) RemoveAt(index int) {
} }
func (l *LinkedList[T]) IsEmpty() bool { func (l *LinkedList[T]) IsEmpty() bool {
l.mu.RLock()
defer l.mu.RUnlock()
return l.size == 0 return l.size == 0
} }
func (l *LinkedList[T]) Size() int { func (l *LinkedList[T]) Size() int {
l.mu.RLock()
defer l.mu.RUnlock()
return l.size return l.size
} }
func (l *LinkedList[T]) GetAt(i int) T { func (l *LinkedList[T]) GetAt(i int) T {
if i <= l.Size() { l.mu.RLock()
defer l.mu.RUnlock()
if i < l.size {
var n int var n int
for current := l.head; current != nil; current = current.next { for current := l.head; current != nil; current = current.next {
if n == i { if n == i {
@@ -152,6 +161,9 @@ func (l *LinkedList[T]) GetAt(i int) T {
} }
func (l *LinkedList[T]) ForEach(fn func(T) bool) { func (l *LinkedList[T]) ForEach(fn func(T) bool) {
l.mu.RLock()
defer l.mu.RUnlock()
for current := l.head; current != nil; current = current.next { for current := l.head; current != nil; current = current.next {
if fn(current.item) { if fn(current.item) {
break break
@@ -160,26 +172,42 @@ func (l *LinkedList[T]) ForEach(fn func(T) bool) {
} }
func (l *LinkedList[T]) Clear() { func (l *LinkedList[T]) Clear() {
l.mu.Lock()
defer l.mu.Unlock()
l.head = nil l.head = nil
l.tail = nil l.tail = nil
l.size = 0 l.size = 0
} }
func (l *LinkedList[T]) ToList() []T { func (l *LinkedList[T]) ToList() []T {
l.mu.RLock()
defer l.mu.RUnlock()
ret := make([]T, 0, l.size) ret := make([]T, 0, l.size)
l.ForEach(func(t T) bool { for current := l.head; current != nil; current = current.next {
ret = append(ret, t) ret = append(ret, current.item)
return false }
})
return ret return ret
} }
func (l *LinkedList[T]) String() string { func (l *LinkedList[T]) String() string {
return fmt.Sprint(l.ToList()) l.mu.RLock()
defer l.mu.RUnlock()
ret := make([]T, 0, l.size)
for current := l.head; current != nil; current = current.next {
ret = append(ret, current.item)
}
return fmt.Sprint(ret)
} }
func (l *LinkedList[T]) remove(n *linkedNode[T]) { func (l *LinkedList[T]) remove(n *linkedNode[T]) {
l.mu.Lock()
defer l.mu.Unlock()
n.next = nil n.next = nil
l.size-- l.size--
} }
+22 -2
View File
@@ -40,21 +40,38 @@ func (l *LinkedList[T]) PushFront(v T) *LinkedList[T] {
} }
func (l *LinkedList[T]) FrontNode() *LinkedNode[T] { func (l *LinkedList[T]) FrontNode() *LinkedNode[T] {
l.mu.RLock()
defer l.mu.RUnlock()
return l.front return l.front
} }
func (l *LinkedList[T]) Front() T { func (l *LinkedList[T]) Front() T {
return l.FrontNode().Value l.mu.RLock()
defer l.mu.RUnlock()
if l.size == 0 {
panic(ErrorOutOffRange)
}
return l.front.Value
} }
func (l *LinkedList[T]) BackNode() *LinkedNode[T] { func (l *LinkedList[T]) BackNode() *LinkedNode[T] {
l.mu.RLock()
defer l.mu.RUnlock()
return l.tail return l.tail
} }
func (l *LinkedList[T]) Back() T { func (l *LinkedList[T]) Back() T {
l.mu.RLock()
defer l.mu.RUnlock()
if l.size == 0 { if l.size == 0 {
panic(ErrorOutOffRange) panic(ErrorOutOffRange)
} }
return l.tail.Value return l.tail.Value
} }
@@ -70,7 +87,10 @@ func (l *LinkedList[T]) ForEach(fn func(T) bool) {
} }
func (l *LinkedList[T]) GetAt(i int) T { func (l *LinkedList[T]) GetAt(i int) T {
if i <= l.Size() { l.mu.RLock()
defer l.mu.RUnlock()
if i < l.size {
for n, current := 0, l.front; current != nil; current, n = current.Next, n+1 { for n, current := 0, l.front; current != nil; current, n = current.Next, n+1 {
if n == i { if n == i {
return current.Value return current.Value
+13
View File
@@ -56,15 +56,28 @@ func (q *ArrayQueue[T]) Poll() T {
} }
func (q *ArrayQueue[T]) Peek() T { func (q *ArrayQueue[T]) Peek() T {
q.lock.Lock()
defer q.lock.Unlock()
if q.size == 0 {
panic("empty")
}
return q.array[0] return q.array[0]
} }
// 栈大小 // 栈大小
func (q *ArrayQueue[T]) Size() int { func (q *ArrayQueue[T]) Size() int {
q.lock.Lock()
defer q.lock.Unlock()
return q.size return q.size
} }
// 栈是否为空 // 栈是否为空
func (q *ArrayQueue[T]) IsEmpty() bool { func (q *ArrayQueue[T]) IsEmpty() bool {
q.lock.Lock()
defer q.lock.Unlock()
return q.size == 0 return q.size == 0
} }
+9 -2
View File
@@ -60,22 +60,29 @@ func (s *ArrayStack[T]) Pop() T {
// 获取栈顶元素 // 获取栈顶元素
func (s *ArrayStack[T]) Peek() T { func (s *ArrayStack[T]) Peek() T {
// 栈中元素已空 s.lock.Lock()
defer s.lock.Unlock()
if s.size == 0 { if s.size == 0 {
panic("empty") panic("empty")
} }
// 栈顶元素值
v := s.array[s.size-1] v := s.array[s.size-1]
return v return v
} }
// 栈大小 // 栈大小
func (s *ArrayStack[T]) Size() int { func (s *ArrayStack[T]) Size() int {
s.lock.Lock()
defer s.lock.Unlock()
return s.size return s.size
} }
// 栈是否为空 // 栈是否为空
func (s *ArrayStack[T]) IsEmpty() bool { func (s *ArrayStack[T]) IsEmpty() bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.size == 0 return s.size == 0
} }
+13 -4
View File
@@ -59,10 +59,13 @@ func (s *memStore[T]) Push(ctx context.Context, v T) error {
func (s *memStore[T]) Pop() (T, error) { func (s *memStore[T]) Pop() (T, error) {
for { for {
_, exist := s.Peek() s.mu.Lock()
if exist { if s.h.Len() > 0 {
return s.h.Pop().(T), nil v := s.h.Pop().(T)
s.mu.Unlock()
return v, nil
} }
s.mu.Unlock()
time.Sleep(time.Millisecond * 10) time.Sleep(time.Millisecond * 10)
} }
@@ -80,9 +83,15 @@ func (s *memStore[T]) Peek() (T, bool) {
} }
func (s *memStore[T]) Len() int { func (s *memStore[T]) Len() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.h.Len() return s.h.Len()
} }
func (s *memStore[T]) IsEmpty() bool { func (s *memStore[T]) IsEmpty() bool {
return s.Len() == 0 s.mu.Lock()
defer s.mu.Unlock()
return s.h.Len() == 0
} }
+1
View File
@@ -37,6 +37,7 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/hashicorp/go-version v1.9.0 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect
+2
View File
@@ -63,6 +63,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/go-version v1.9.0 h1:CeOIz6k+LoN3qX9Z0tyQrPtiB1DFYRPfCIBtaXPSCnA=
github.com/hashicorp/go-version v1.9.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
+3
View File
@@ -18,6 +18,9 @@ func New() *Ketama {
} }
func (k *Ketama) Synchronize() { func (k *Ketama) Synchronize() {
if k.mu == nil {
k.mu = &locker.RWLock{}
}
k.mu.Synchronize() k.mu.Synchronize()
} }
+2
View File
@@ -3,6 +3,8 @@ package locker
var _ rwLocker = &emptyLocker{} var _ rwLocker = &emptyLocker{}
var _ locker = &emptyLocker{} var _ locker = &emptyLocker{}
var EmptyLocker RWLocker = &emptyLocker{}
type emptyLocker struct { type emptyLocker struct {
} }
+2
View File
@@ -17,6 +17,8 @@ type rwLocker interface {
TryRLock() bool TryRLock() bool
} }
type RWLocker = rwLocker
func NewLocker() *sync.Mutex { func NewLocker() *sync.Mutex {
return &sync.Mutex{} return &sync.Mutex{}
} }
+25 -37
View File
@@ -2,20 +2,16 @@ package locker
import ( import (
"context" "context"
"fmt"
"sync/atomic"
redis_store "github.com/charlienet/go-mixed/locker/redis" redis_store "github.com/charlienet/go-mixed/locker/redis"
"github.com/charlienet/go-mixed/redis" "github.com/charlienet/go-mixed/redis"
) )
// 带计数器锁
type countLocker struct { type countLocker struct {
rw rwLocker rw rwLocker
Count int32 Count int32
} }
// SourceLocker 资源锁
type SourceLocker struct { type SourceLocker struct {
m RWLocker m RWLocker
distributedLocker DistributedLocker distributedLocker DistributedLocker
@@ -28,7 +24,7 @@ func NewSourceLocker() *SourceLocker {
locks: make(map[string]*countLocker), locks: make(map[string]*countLocker),
} }
l.m.Synchronize() l.m = NewRWLocker()
return l return l
} }
@@ -43,39 +39,25 @@ func (s *SourceLocker) WithDistributedLocker(distributed DistributedLocker) *Sou
} }
func (s *SourceLocker) Lock(key string) { func (s *SourceLocker) Lock(key string) {
s.m.RLock()
l, ok := s.locks[key]
s.m.RUnlock()
if ok {
atomic.AddInt32(&l.Count, 1)
l.rw.Lock()
fmt.Println("加锁")
} else {
// 加锁,再次检查是否已经具有锁
s.m.Lock() s.m.Lock()
if l2, ok := s.locks[key]; ok { l, ok := s.locks[key]
s.m.Unlock() if ok {
l.Count++
l2.rw.Lock()
fmt.Println("二次检查加锁")
} else { } else {
n := NewRWLocker() n := NewRWLocker()
s.locks[key] = &countLocker{rw: n, Count: 1} l = &countLocker{rw: n, Count: 1}
s.locks[key] = l
}
s.m.Unlock() s.m.Unlock()
n.Lock() l.rw.Lock()
}
}
} }
func (s *SourceLocker) Unlock(key string) { func (s *SourceLocker) Unlock(key string) {
s.m.Lock() s.m.Lock()
if l, ok := s.locks[key]; ok { if l, ok := s.locks[key]; ok {
atomic.AddInt32(&l.Count, -1) l.Count--
l.rw.Unlock() l.rw.Unlock()
if s.distributedLocker != nil { if s.distributedLocker != nil {
@@ -90,19 +72,25 @@ func (s *SourceLocker) Unlock(key string) {
} }
func (s *SourceLocker) TryLock(key string) bool { func (s *SourceLocker) TryLock(key string) bool {
// 加读锁
s.m.RLock()
l, ok := s.locks[key]
s.m.RUnlock()
if ok {
ret := l.rw.TryLock()
return ret
} else {
s.m.Lock() s.m.Lock()
l, ok := s.locks[key]
if ok {
l.Count++
} else {
n := NewRWLocker() n := NewRWLocker()
s.locks[key] = &countLocker{rw: n, Count: 1} l = &countLocker{rw: n, Count: 1}
s.locks[key] = l
}
s.m.Unlock() s.m.Unlock()
return n.TryLock() if !l.rw.TryLock() {
s.m.Lock()
l.Count--
if l.Count == 0 {
delete(s.locks, key)
} }
s.m.Unlock()
return false
}
return true
} }
+20 -15
View File
@@ -24,9 +24,9 @@ func (w *Locker) WithDistributedLocker(d DistributedLocker) *Locker {
} }
func (w *Locker) Synchronize() *Locker { func (w *Locker) Synchronize() *Locker {
if w.mu == nil || w.mu == empty { w.once.Do(func() {
w.mu = NewLocker() w.mu = NewLocker()
} })
return w return w
} }
@@ -59,49 +59,54 @@ type SpinLocker struct {
} }
func (w *SpinLocker) Synchronize() { func (w *SpinLocker) Synchronize() {
if w.mu == nil || w.mu == empty { w.once.Do(func() {
w.mu = NewSpinLocker() w.mu = NewSpinLocker()
} })
} }
type RWLocker struct { type WithRWLocker interface {
rwLocker
Synchronize() WithRWLocker
}
type RWLock struct {
once sync.Once once sync.Once
mu rwLocker mu rwLocker
} }
func (w *RWLocker) Synchronize() *RWLocker { func (w *RWLock) Synchronize() WithRWLocker {
if w.mu == nil || w.mu == empty { w.once.Do(func() {
w.mu = NewRWLocker() w.mu = NewRWLocker()
} })
return w return w
} }
func (w *RWLocker) Lock() { func (w *RWLock) Lock() {
w.ensureLocker().mu.Lock() w.ensureLocker().mu.Lock()
} }
func (w *RWLocker) TryLock() bool { func (w *RWLock) TryLock() bool {
return w.ensureLocker().mu.TryLock() return w.ensureLocker().mu.TryLock()
} }
func (w *RWLocker) Unlock() { func (w *RWLock) Unlock() {
w.ensureLocker().mu.Unlock() w.ensureLocker().mu.Unlock()
} }
func (w *RWLocker) RLock() { func (w *RWLock) RLock() {
w.ensureLocker().mu.RLock() w.ensureLocker().mu.RLock()
} }
func (w *RWLocker) TryRLock() bool { func (w *RWLock) TryRLock() bool {
return w.ensureLocker().mu.TryRLock() return w.ensureLocker().mu.TryRLock()
} }
func (w *RWLocker) RUnlock() { func (w *RWLock) RUnlock() {
w.ensureLocker().mu.RUnlock() w.ensureLocker().mu.RUnlock()
} }
func (w *RWLocker) ensureLocker() *RWLocker { func (w *RWLock) ensureLocker() *RWLock {
w.once.Do(func() { w.once.Do(func() {
if w.mu == nil { if w.mu == nil {
log.Println("初始化一个空锁") log.Println("初始化一个空锁")
+3 -3
View File
@@ -17,7 +17,7 @@ func TestLocker(t *testing.T) {
} }
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
var a locker.RWLocker var a locker.RWLock
a.Synchronize() a.Synchronize()
} }
@@ -30,8 +30,8 @@ func TestSpinLocker(t *testing.T) {
defer l.Unlock() defer l.Unlock()
} }
func TestRWLocker(t *testing.T) { func TestRWLock(t *testing.T) {
var l locker.RWLocker var l locker.RWLock
l.Lock() l.Lock()
} }
+3
View File
@@ -125,6 +125,9 @@ func (m *hashMap[K, V]) Clear() {
} }
func (m *hashMap[K, V]) Count() int { func (m *hashMap[K, V]) Count() int {
m.opt.mu.RLock()
defer m.opt.mu.RUnlock()
return len(m.m) return len(m.m)
} }
+6
View File
@@ -65,10 +65,16 @@ func (m *rw_map[K, V]) Shrink() map[K]V {
} }
func (m *rw_map[K, V]) Exist(key K) bool { func (m *rw_map[K, V]) Exist(key K) bool {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Exist(key) return m.m.Exist(key)
} }
func (m *rw_map[K, V]) Count() int { func (m *rw_map[K, V]) Count() int {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Count() return m.m.Count()
} }
+2 -1
View File
@@ -20,6 +20,7 @@ type hash_set[T constraints.Ordered] struct {
func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] { func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
set := hash_set[T]{ set := hash_set[T]{
m: make(map[T]struct{}, len(values)), m: make(map[T]struct{}, len(values)),
locker: locker.EmptyLocker,
} }
set.Add(values...) set.Add(values...)
@@ -27,7 +28,7 @@ func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
} }
func (s *hash_set[T]) Sync() *hash_set[T] { func (s *hash_set[T]) Sync() *hash_set[T] {
s.locker.Synchronize() s.locker = locker.NewRWLocker()
return s return s
} }