Code violation fix and added locks in metriccache manipulation funcs 22/4822/2
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Thu, 8 Oct 2020 11:06:57 +0000 (14:06 +0300)
committerJuha Hyttinen <juha.hyttinen@nokia.com>
Fri, 9 Oct 2020 04:49:16 +0000 (07:49 +0300)
Change-Id: Ife874fdfb7ec561ace81ef73a433ad64ef1a818a
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
pkg/xapp/metrics.go
pkg/xapp/rmr.go

index c943db6..73a9b62 100644 (file)
@@ -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{}
index ee6b622..03fba67 100755 (executable)
@@ -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 ...