Skip to content

Commit

Permalink
perf: 调整 super 包 error 部分为使用后再申请内存
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Sep 9, 2023
1 parent de7b085 commit 83b2800
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions utils/super/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"sync"
)

var errorMapper = make(map[error]int)
var errorMapperRef = make(map[error]error)
var mutex sync.Mutex
var errorManagerInstance *errorManager

// RegError 通过错误码注册错误,返回错误的引用
func RegError(code int, message string) error {
if code == 0 {
return errors.New("error code can not be 0")
}
mutex.Lock()
defer mutex.Unlock()
errorManagerInstance.mutex.Lock()
defer errorManagerInstance.mutex.Unlock()
if errorManagerInstance == nil {
errorManagerInstance = new(errorManager).init()
}
err := &ser{code: code, message: message}
errorMapper[err] = code
errorManagerInstance.errorMapper[err] = code
return err
}

Expand All @@ -27,11 +28,14 @@ func RegErrorRef(code int, message string, ref error) error {
if code == 0 {
return errors.New("error code can not be 0")
}
mutex.Lock()
defer mutex.Unlock()
errorManagerInstance.mutex.Lock()
defer errorManagerInstance.mutex.Unlock()
if errorManagerInstance == nil {
errorManagerInstance = new(errorManager).init()
}
err := &ser{code: code, message: message}
errorMapper[err] = code
errorMapperRef[ref] = err
errorManagerInstance.errorMapper[err] = code
errorManagerInstance.errorMapperRef[ref] = err
return ref
}

Expand All @@ -41,17 +45,17 @@ func GetError(err error) (int, error) {
if unw == nil {
unw = err
}
mutex.Lock()
defer mutex.Unlock()
if ref, exist := errorMapperRef[unw]; exist {
errorManagerInstance.mutex.Lock()
defer errorManagerInstance.mutex.Unlock()
if ref, exist := errorManagerInstance.errorMapperRef[unw]; exist {
//err = fmt.Errorf("%w : %s", ref, err.Error())
err = ref
}
unw = errors.Unwrap(err)
if unw == nil {
unw = err
}
code, exist := errorMapper[unw]
code, exist := errorManagerInstance.errorMapper[unw]
if !exist {
return 0, errors.New("error not found")
}
Expand All @@ -66,3 +70,15 @@ type ser struct {
func (slf *ser) Error() string {
return slf.message
}

type errorManager struct {
errorMapper map[error]int
errorMapperRef map[error]error
mutex sync.Mutex
}

func (slf *errorManager) init() *errorManager {
slf.errorMapper = make(map[error]int)
slf.errorMapperRef = make(map[error]error)
return slf
}

0 comments on commit 83b2800

Please sign in to comment.