Merge pull request #38 from vincentbernat/fix/defer-unlock

style: defer unlock when possible/not trivial
This commit is contained in:
Louis
2021-09-23 20:44:05 -07:00
committed by GitHub
4 changed files with 6 additions and 13 deletions

View File

@@ -285,6 +285,7 @@ func (ts *BasicTemplateSystem) GetTemplates() map[uint16]map[uint32]map[uint16]i
func (ts *BasicTemplateSystem) AddTemplate(version uint16, obsDomainId uint32, template interface{}) {
ts.templateslock.Lock()
defer ts.templateslock.Unlock()
_, exists := ts.templates[version]
if exists != true {
ts.templates[version] = make(map[uint32]map[uint16]interface{})
@@ -303,27 +304,21 @@ func (ts *BasicTemplateSystem) AddTemplate(version uint16, obsDomainId uint32, t
templateId = templateIdConv.TemplateId
}
ts.templates[version][obsDomainId][templateId] = template
ts.templateslock.Unlock()
}
func (ts *BasicTemplateSystem) GetTemplate(version uint16, obsDomainId uint32, templateId uint16) (interface{}, error) {
ts.templateslock.RLock()
defer ts.templateslock.RUnlock()
templatesVersion, okver := ts.templates[version]
if okver {
templatesObsDom, okobs := templatesVersion[obsDomainId]
if okobs {
template, okid := templatesObsDom[templateId]
if okid {
ts.templateslock.RUnlock()
return template, nil
}
ts.templateslock.RUnlock()
return nil, NewErrorTemplateNotFound(version, obsDomainId, templateId, "info")
}
ts.templateslock.RUnlock()
return nil, NewErrorTemplateNotFound(version, obsDomainId, templateId, "info")
}
ts.templateslock.RUnlock()
return nil, NewErrorTemplateNotFound(version, obsDomainId, templateId, "info")
}

View File

@@ -53,12 +53,12 @@ func FindFormat(ctx context.Context, name string) (*Format, error) {
func GetFormats() []string {
lock.RLock()
defer lock.RUnlock()
t := make([]string, len(formatDrivers))
var i int
for k, _ := range formatDrivers {
t[i] = k
i++
}
lock.RUnlock()
return t
}

View File

@@ -33,27 +33,25 @@ func CreateSamplingSystem() SamplingRateSystem {
func (s *basicSamplingRateSystem) AddSamplingRate(version uint16, obsDomainId uint32, samplingRate uint32) {
s.samplinglock.Lock()
defer s.samplinglock.Unlock()
_, exists := s.sampling[version]
if exists != true {
s.sampling[version] = make(map[uint32]uint32)
}
s.sampling[version][obsDomainId] = samplingRate
s.samplinglock.Unlock()
}
func (s *basicSamplingRateSystem) GetSamplingRate(version uint16, obsDomainId uint32) (uint32, error) {
s.samplinglock.RLock()
defer s.samplinglock.RUnlock()
samplingVersion, okver := s.sampling[version]
if okver {
samplingRate, okid := samplingVersion[obsDomainId]
if okid {
s.samplinglock.RUnlock()
return samplingRate, nil
}
s.samplinglock.RUnlock()
return 0, errors.New("") // TBC
}
s.samplinglock.RUnlock()
return 0, errors.New("") // TBC
}

View File

@@ -57,12 +57,12 @@ func FindTransport(ctx context.Context, name string) (*Transport, error) {
func GetTransports() []string {
lock.RLock()
defer lock.RUnlock()
t := make([]string, len(transportDrivers))
var i int
for k, _ := range transportDrivers {
t[i] = k
i++
}
lock.RUnlock()
return t
}