mirror of
https://github.com/rulego/gflow-engine.git
synced 2026-12-05 08:00:59 +00:00
fix(service): serviceTask 函数名校验器读写加锁,消除数据竞态
serviceFunctionChecker 是进程级可注册运行时状态,写侧在宿主装配 (components.Register → SetServiceFunctionChecker)注入,读侧在 Deploy/Update 请求处理路径(ValidateChainExpressions → validateServiceTaskFunction)并发执行。 原来直接读写包级函数变量、无同步:宿主运行中重装配组件或测试并行 Set/读时, go test -race 会报数据竞态,读侧可能拿到半装配状态的函数指针。 补 sync.RWMutex: - SetServiceFunctionChecker 写锁写入 - 读侧经 serviceFunctionCheckerForRead 读锁读取 与同包 DefaultIDGenerator(defaultIDGenMu)和 DialectRegistry(mutex)等 可注册运行时单例的并发安全口径对齐。 新增 ConcurrentAccess 用例(并发 Set/读,go test -race 下验证无竞态)。
This commit is contained in:
2 files changed
+52
-3
No files matched your search
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rulego/gflow-engine/types/constants"
|
||||
"github.com/rulego/rulego/api/types"
|
||||
@@ -33,13 +34,27 @@ type ConditionIssue struct {
|
||||
// serviceFunctionChecker serviceTask 函数名存在性校验器,由组件层
|
||||
// (components.Services 所在包)经 SetServiceFunctionChecker 注入,避免
|
||||
// service→components 循环依赖。nil 时跳过函数名校验(宿主未完成装配的场景)。
|
||||
var serviceFunctionChecker func(name string) bool
|
||||
// 读侧在 Deploy/Update 请求处理路径(ValidateChainExpressions)并发执行,
|
||||
// 写侧在宿主装配(components.Register)时注入,故须加锁避免并发读写竞态。
|
||||
var (
|
||||
serviceFunctionChecker func(name string) bool
|
||||
serviceFunctionCheckerMu sync.RWMutex
|
||||
)
|
||||
|
||||
// SetServiceFunctionChecker 注入 serviceTask 函数名校验器;传 nil 恢复跳过。
|
||||
// SetServiceFunctionChecker 注入 serviceTask 函数名校验器;传 nil 恢复跳过。并发安全。
|
||||
func SetServiceFunctionChecker(fn func(name string) bool) {
|
||||
serviceFunctionCheckerMu.Lock()
|
||||
defer serviceFunctionCheckerMu.Unlock()
|
||||
serviceFunctionChecker = fn
|
||||
}
|
||||
|
||||
// serviceFunctionCheckerForRead 并发安全地读取当前校验器(读锁)。
|
||||
func serviceFunctionCheckerForRead() func(name string) bool {
|
||||
serviceFunctionCheckerMu.RLock()
|
||||
defer serviceFunctionCheckerMu.RUnlock()
|
||||
return serviceFunctionChecker
|
||||
}
|
||||
|
||||
// ValidateChainExpressions 校验链内所有分支条件表达式可编译、serviceTask
|
||||
// 函数名已注册,返回问题列表(空=通过)。
|
||||
//
|
||||
@@ -107,7 +122,7 @@ func validateServiceTaskFunction(node *types.RuleNode, cfg map[string]interface{
|
||||
issue.Error = "functionName is empty"
|
||||
return []ConditionIssue{issue}
|
||||
}
|
||||
if serviceFunctionChecker != nil && !serviceFunctionChecker(name) {
|
||||
if checker := serviceFunctionCheckerForRead(); checker != nil && !checker(name) {
|
||||
issue.Error = "function is not registered"
|
||||
return []ConditionIssue{issue}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/rulego/gflow-engine/model"
|
||||
@@ -162,3 +163,36 @@ func TestValidateExpressions(t *testing.T) {
|
||||
t.Errorf("valid expression should pass: %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestServiceFunctionChecker_ConcurrentAccess 验证校验器读写并发安全:
|
||||
// 宿主装配(SetServiceFunctionChecker)与 Deploy/Update 请求路径的读取
|
||||
// validateServiceTaskFunction 可能并发,须经 RWMutex 保护;在 go test -race
|
||||
// 下可捕获漏同步,普通运行下仅演练读写路径。
|
||||
func TestServiceFunctionChecker_ConcurrentAccess(t *testing.T) {
|
||||
defer SetServiceFunctionChecker(nil)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 200; j++ {
|
||||
SetServiceFunctionChecker(func(name string) bool { return name == "serialNo" })
|
||||
if fn := serviceFunctionCheckerForRead(); fn == nil {
|
||||
t.Errorf("checker should be set during write phase")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 200; j++ {
|
||||
_ = serviceFunctionCheckerForRead()
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in new issue
Block a user