From 1ca02aab3ca9e4ea1ca6feedee17ae5f67412902 Mon Sep 17 00:00:00 2001 From: fnoopv Date: Tue, 8 Sep 2026 14:54:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(service):=20serviceTask=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E6=A0=A1=E9=AA=8C=E5=99=A8=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E5=8A=A0=E9=94=81=EF=BC=8C=E6=B6=88=E9=99=A4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=AB=9E=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 下验证无竞态)。 --- service/condition_validate.go | 21 +++++++++++++++--- service/condition_validate_test.go | 34 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/service/condition_validate.go b/service/condition_validate.go index 67784c4..896e842 100644 --- a/service/condition_validate.go +++ b/service/condition_validate.go @@ -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} } diff --git a/service/condition_validate_test.go b/service/condition_validate_test.go index edd4e13..32ac201 100644 --- a/service/condition_validate_test.go +++ b/service/condition_validate_test.go @@ -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() +}