40 lines
781 B
Go
40 lines
781 B
Go
package repo
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"joylink.club/rtsssimulation/repo/dto"
|
|
)
|
|
|
|
type repoManager struct {
|
|
repoMap map[string]CgRepo
|
|
lock sync.Mutex
|
|
}
|
|
|
|
var defaultManager = &repoManager{
|
|
repoMap: make(map[string]CgRepo),
|
|
}
|
|
|
|
// 获取或构建模型仓库
|
|
func GetOrBuildRepo(id string, dc func(errRecord *ErrorRecord) *dto.CgRepo) (CgRepo, *ErrorRecord) {
|
|
manager := defaultManager
|
|
manager.lock.Lock()
|
|
defer manager.lock.Unlock()
|
|
r, ok := manager.repoMap[id]
|
|
errRecord := NewErrorRecord()
|
|
if !ok {
|
|
// 所需protobuf数据转换
|
|
msgs := dc(errRecord)
|
|
// 数据转换出错直接返回
|
|
if errRecord.HasError() {
|
|
return nil, errRecord
|
|
}
|
|
// 构建模型Repo
|
|
r, errRecord = BuildFrom(msgs)
|
|
if r != nil {
|
|
manager.repoMap[id] = r
|
|
}
|
|
}
|
|
return r, errRecord
|
|
}
|