From: Juha Hyttinen Date: Thu, 8 Oct 2020 11:06:57 +0000 (+0300) Subject: Code violation fix and added locks in metriccache manipulation funcs X-Git-Tag: v0.5.6~1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=64924383d9229860a167370a785dd6de0a38786b;p=ric-plt%2Fxapp-frame.git Code violation fix and added locks in metriccache manipulation funcs Change-Id: Ife874fdfb7ec561ace81ef73a433ad64ef1a818a Signed-off-by: Juha Hyttinen --- diff --git a/pkg/xapp/metrics.go b/pkg/xapp/metrics.go index c943db6..73a9b62 100644 --- a/pkg/xapp/metrics.go +++ b/pkg/xapp/metrics.go @@ -24,6 +24,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" + "sync" ) //----------------------------------------------------------------------------- @@ -58,6 +59,7 @@ func (met *MetricGroupsCache) GSet(metric string, val float64) { // //----------------------------------------------------------------------------- type Metrics struct { + lock sync.Mutex Namespace string MetricGroupsCacheMap map[string]*MetricGroupsCache } @@ -255,6 +257,8 @@ func (m *Metrics) CombineGaugeGroups(srcs ...map[string]Gauge) map[string]Gauge * */ func (m *Metrics) GroupCacheGet(id string) *MetricGroupsCache { + m.lock.Lock() + defer m.lock.Unlock() entry, ok := m.MetricGroupsCacheMap[id] if ok == false { return nil @@ -263,6 +267,8 @@ func (m *Metrics) GroupCacheGet(id string) *MetricGroupsCache { } func (m *Metrics) GroupCacheAddCounters(id string, vals map[string]Counter) { + m.lock.Lock() + defer m.lock.Unlock() entry, ok := m.MetricGroupsCacheMap[id] if ok == false { entry = &MetricGroupsCache{} @@ -272,6 +278,8 @@ func (m *Metrics) GroupCacheAddCounters(id string, vals map[string]Counter) { } func (m *Metrics) GroupCacheAddGauges(id string, vals map[string]Gauge) { + m.lock.Lock() + defer m.lock.Unlock() entry, ok := m.MetricGroupsCacheMap[id] if ok == false { entry = &MetricGroupsCache{} diff --git a/pkg/xapp/rmr.go b/pkg/xapp/rmr.go index ee6b622..03fba67 100755 --- a/pkg/xapp/rmr.go +++ b/pkg/xapp/rmr.go @@ -570,8 +570,12 @@ func (m *RMRClient) GetRicMessageName(id int) (s string) { } func (m *RMRClient) LogMBufError(text string, mbuf *C.rmr_mbuf_t) int { - Logger.Debug(fmt.Sprintf("rmrClient: %s -> [tp=%v] %v - %s", text, mbuf.tp_state, mbuf.state, RMRErrors[int(mbuf.state)])) - return int(mbuf.state) + if mbuf != nil { + Logger.Debug(fmt.Sprintf("rmrClient: %s -> [tp=%v] %v - %s", text, mbuf.tp_state, mbuf.state, RMRErrors[int(mbuf.state)])) + return int(mbuf.state) + } + Logger.Debug(fmt.Sprintf("rmrClient: %s -> mbuf nil", text)) + return 0 } // To be removed ...