Fix panic in ListChildren

This commit is contained in:
Lou Knauer 2022-03-04 15:33:47 +01:00
parent 902fcf9510
commit af8a275088
2 changed files with 10 additions and 6 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require ( require (
github.com/golang-jwt/jwt/v4 v4.0.0 github.com/golang-jwt/jwt/v4 v4.0.0
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gops v0.3.22 // indirect github.com/google/gops v0.3.22
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/influxdata/line-protocol/v2 v2.2.0 github.com/influxdata/line-protocol/v2 v2.2.0
github.com/nats-io/nats-server/v2 v2.2.6 // indirect github.com/nats-io/nats-server/v2 v2.2.6 // indirect

View File

@ -254,7 +254,6 @@ type level struct {
// it does not exist. Example selector in the context of the // it does not exist. Example selector in the context of the
// ClusterCockpit could be: []string{ "emmy", "host123", "cpu0" }. // ClusterCockpit could be: []string{ "emmy", "host123", "cpu0" }.
// This function would probably benefit a lot from `level.children` beeing a `sync.Map`? // This function would probably benefit a lot from `level.children` beeing a `sync.Map`?
// If nMetrics is -1, do not create new levels.
func (l *level) findLevelOrCreate(selector []string, nMetrics int) *level { func (l *level) findLevelOrCreate(selector []string, nMetrics int) *level {
if len(selector) == 0 { if len(selector) == 0 {
return l return l
@ -267,9 +266,6 @@ func (l *level) findLevelOrCreate(selector []string, nMetrics int) *level {
if l.children == nil { if l.children == nil {
// Children map needs to be created... // Children map needs to be created...
l.lock.RUnlock() l.lock.RUnlock()
if nMetrics == -1 {
return nil
}
} else { } else {
child, ok := l.children[selector[0]] child, ok := l.children[selector[0]]
l.lock.RUnlock() l.lock.RUnlock()
@ -486,7 +482,15 @@ func (m *MemoryStore) FreeAll() error {
// Given a selector, return a list of all children of the level selected. // Given a selector, return a list of all children of the level selected.
func (m *MemoryStore) ListChildren(selector []string) []string { func (m *MemoryStore) ListChildren(selector []string) []string {
lvl := m.root.findLevelOrCreate(selector, -1) lvl := &m.root
for lvl != nil && len(selector) != 0 {
lvl.lock.RLock()
next := lvl.children[selector[0]]
lvl.lock.RUnlock()
lvl = next
selector = selector[1:]
}
if lvl == nil { if lvl == nil {
return nil return nil
} }