Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #100 from bancodobrasil/fix/rulesheet-expiration
Browse files Browse the repository at this point in the history
fix: remove race condition during rules remove + eval
  • Loading branch information
eliasfeijo authored Aug 11, 2023
2 parents 223b3f8 + a370052 commit 7430d58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions controllers/v1/evalHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func EvalHandler() gin.HandlerFunc {
fmt.Fprint(c.Writer, "Error on json decode")
return
}
log.Debugln(t)
log.Traceln(t)

ctx := types.NewContextFromMap(t)
ctx.RawContext = c.Request.Context()
Expand All @@ -162,8 +162,8 @@ func EvalHandler() gin.HandlerFunc {
return
}

log.Debug("Context:\n\t", ctx.GetEntries(), "\n\n")
log.Debug("Features:\n\t", result.GetFeatures(), "\n\n")
log.Trace("Context:\n\t", ctx.GetEntries(), "\n\n")
log.Trace("Features:\n\t", result.GetFeatures(), "\n\n")

responseCode := http.StatusOK

Expand Down
24 changes: 21 additions & 3 deletions services/rulles.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ var evalMutex sync.Mutex

var getMutex sync.Mutex

var loadWg sync.WaitGroup

var evalWg sync.WaitGroup

// IEval interface defines methods for loading and evaluating knowledge bases in Go.
//
// Property
Expand Down Expand Up @@ -179,9 +183,15 @@ func (s Eval) GetKnowledgeBase(knowledgeBaseName string, version string) (*ast.K
return base, nil
}

loadWg.Add(1)
defer loadWg.Done()

log.Trace("Waiting: [evalWg]")
evalWg.Wait()
log.Trace("Pass: [evalWg]")

// If the version is expired, we must invalidate its rules
if expired {
log.Trace("GetKnowledgeBase: [expired]")
for key := range base.RuleEntries {
s.knowledgeLibrary.RemoveRuleEntry(key, knowledgeBaseName, version)
}
Expand Down Expand Up @@ -210,10 +220,18 @@ func (s Eval) GetKnowledgeBase(knowledgeBaseName string, version string) (*ast.K

// Eval ...
func (s Eval) Eval(ctx *types.Context, knowledgeBase *ast.KnowledgeBase) (result *types.Result, err error) {

// FIXME Remove synchronization on eval
evalMutex.Lock()
defer evalMutex.Unlock()

log.Trace("Waiting: [loadWg]")
loadWg.Wait()
log.Trace("Pass: [loadWg]")

evalWg.Add(1)
defer evalWg.Done()

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic: %v", r)
Expand Down Expand Up @@ -259,8 +277,8 @@ func (s Eval) Eval(ctx *types.Context, knowledgeBase *ast.KnowledgeBase) (result
result.Put("requiredParamErrors", ctx.GetMap("requiredParamErrors").GetEntries())
}

log.Debug("Context:\n\t", ctx.GetEntries(), "\n\n")
log.Debug("Features:\n\t", result.GetFeatures(), "\n\n")
log.Trace("Context:\n\t", ctx.GetEntries(), "\n\n")
log.Trace("Features:\n\t", result.GetFeatures(), "\n\n")

return
}
Expand Down

0 comments on commit 7430d58

Please sign in to comment.