rts-sim-module/repo/manage.go
walker 1101edd1d3 调整repo定义
城轨和国铁分开
2023-12-27 13:04:19 +08:00

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
}