From 6238883dc97839b089fb36544252614c4d5860ff Mon Sep 17 00:00:00 2001 From: kercylan <61743331+kercylan98@users.noreply.github.com> Date: Fri, 21 Jul 2023 23:18:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20solo.guid=20?= =?UTF-8?q?=E7=9A=84=E4=BD=BF=E7=94=A8=EF=BC=8C=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E9=9C=80=E8=A6=81=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/sole/guid.go | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/utils/sole/guid.go b/utils/sole/guid.go index c5d84dd..f7199ff 100644 --- a/utils/sole/guid.go +++ b/utils/sole/guid.go @@ -1,37 +1,43 @@ package sole -import "sync" - -var ( - global int64 - namespace map[any]int64 - mutex sync.Mutex +import ( + "sync/atomic" ) -func init() { - namespace = map[any]int64{} +var ( + global atomic.Int64 // 全局唯一标识符 + namespace = map[any]*atomic.Int64{} // 唯一标识符命名空间 +) + +// RegNameSpace 注册特定命名空间的唯一标识符 +func RegNameSpace(name any) { + if namespace == nil { + namespace = map[any]*atomic.Int64{} + } + namespace[name] = new(atomic.Int64) } +// UnRegNameSpace 解除注销特定命名空间的唯一标识符 +func UnRegNameSpace(name any) { + delete(namespace, name) +} + +// Get 获取全局唯一标识符 func Get() int64 { - global++ - return global + return global.Add(1) } +// Reset 重置全局唯一标识符 +func Reset() { + global.Store(0) +} + +// GetWith 获取特定命名空间的唯一标识符 func GetWith(name any) int64 { - namespace[name]++ - return namespace[name] + return namespace[name].Add(1) } -func GetSync() int64 { - mutex.Lock() - defer mutex.Unlock() - global++ - return global -} - -func GetSyncWith(name any) int64 { - mutex.Lock() - defer mutex.Unlock() - namespace[name]++ - return namespace[name] +// ResetWith 重置特定命名空间的唯一标识符 +func ResetWith(name any) { + namespace[name].Store(0) }