package utils import "sync" // KeyedMutex 提供基于键的锁定 type KeyedMutex struct { mutexes sync.Map } // Lock 锁定指定键 func (km *KeyedMutex) Lock(key string) { mu, _ := km.mutexes.LoadOrStore(key, &sync.Mutex{}) mu.(*sync.Mutex).Lock() } // Unlock 解锁指定键 func (km *KeyedMutex) Unlock(key string) { mu, ok := km.mutexes.Load(key) if ok { mu.(*sync.Mutex).Unlock() km.mutexes.Delete(key) } }