Allow concurrent access to condition map

This commit is contained in:
Holger Obermaier 2022-02-16 14:30:11 +01:00
parent b44e226496
commit 154b56000e

View File

@ -6,6 +6,7 @@ import (
"math" "math"
"os" "os"
"strings" "strings"
"sync"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
@ -66,7 +67,12 @@ var language gval.Language = gval.NewLanguage(
gval.Full(), gval.Full(),
metricCacheLanguage, metricCacheLanguage,
) )
var evaluables map[string]gval.Evaluable = make(map[string]gval.Evaluable) var evaluables = struct {
mapping map[string]gval.Evaluable
mutex sync.Mutex
}{
mapping: make(map[string]gval.Evaluable),
}
func (c *metricAggregator) Init(output chan lp.CCMetric) error { func (c *metricAggregator) Init(output chan lp.CCMetric) error {
c.output = output c.output = output
@ -289,9 +295,10 @@ func (c *metricAggregator) AddFunction(name string, function func(args ...interf
} }
func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) { func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) {
var evaluable gval.Evaluable evaluables.mutex.Lock()
var ok bool evaluable, ok := evaluables.mapping[condition]
if evaluable, ok = evaluables[condition]; !ok { evaluables.mutex.Unlock()
if !ok {
newcond := newcond :=
strings.ReplaceAll( strings.ReplaceAll(
strings.ReplaceAll( strings.ReplaceAll(
@ -301,16 +308,19 @@ func EvalBoolCondition(condition string, params map[string]interface{}) (bool, e
if err != nil { if err != nil {
return false, err return false, err
} }
evaluables[condition] = evaluable evaluables.mutex.Lock()
evaluables.mapping[condition] = evaluable
evaluables.mutex.Unlock()
} }
value, err := evaluable.EvalBool(context.Background(), params) value, err := evaluable.EvalBool(context.Background(), params)
return value, err return value, err
} }
func EvalFloat64Condition(condition string, params map[string]interface{}) (float64, error) { func EvalFloat64Condition(condition string, params map[string]interface{}) (float64, error) {
var evaluable gval.Evaluable evaluables.mutex.Lock()
var ok bool evaluable, ok := evaluables.mapping[condition]
if evaluable, ok = evaluables[condition]; !ok { evaluables.mutex.Unlock()
if !ok {
newcond := newcond :=
strings.ReplaceAll( strings.ReplaceAll(
strings.ReplaceAll( strings.ReplaceAll(
@ -320,7 +330,9 @@ func EvalFloat64Condition(condition string, params map[string]interface{}) (floa
if err != nil { if err != nil {
return math.NaN(), err return math.NaN(), err
} }
evaluables[condition] = evaluable evaluables.mutex.Lock()
evaluables.mapping[condition] = evaluable
evaluables.mutex.Unlock()
} }
value, err := evaluable.EvalFloat64(context.Background(), params) value, err := evaluable.EvalFloat64(context.Background(), params)
return value, err return value, err