gitlab.com/evatix-go/core@v1.3.55/mutexbykey/mutexMap.go (about) 1 package mutexbykey 2 3 import ( 4 "sync" 5 6 "gitlab.com/evatix-go/core/constants" 7 ) 8 9 type mutexMap struct { 10 items map[string]*sync.Mutex 11 } 12 13 var globalMutex = sync.Mutex{} 14 15 var items = make( 16 map[string]*sync.Mutex, 17 constants.ArbitraryCapacity10) 18 19 var internalMap = mutexMap{ 20 items: items, 21 } 22 23 func Get(key string) *sync.Mutex { 24 globalMutex.Lock() 25 defer globalMutex.Unlock() 26 27 mutex, has := internalMap.items[key] 28 29 if has { 30 return mutex 31 } 32 33 // not there 34 newMutex := &sync.Mutex{} 35 internalMap.items[key] = newMutex 36 37 return newMutex 38 } 39 40 func Delete(key string) bool { 41 globalMutex.Lock() 42 defer globalMutex.Unlock() 43 44 _, has := internalMap.items[key] 45 46 if has { 47 delete(internalMap.items, key) 48 } 49 50 return has 51 }