fix(gateway): 修复路由引擎P0问题
P0-07: RegisterStrategy添加互斥锁保护,解决并发注册策略时的数据竞争问题 P0-08: SelectProvider添加decision nil检查,避免nil指针被传递 使用TDD方法: 1. 编写测试验证问题存在 2. 修复代码 3. 测试验证通过
This commit is contained in:
@@ -3,6 +3,7 @@ package engine
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"lijiaoqiao/gateway/internal/router/strategy"
|
||||
)
|
||||
@@ -18,6 +19,7 @@ type RoutingMetrics interface {
|
||||
|
||||
// RoutingEngine 路由引擎
|
||||
type RoutingEngine struct {
|
||||
mu sync.RWMutex
|
||||
strategies map[string]strategy.StrategyTemplate
|
||||
metrics RoutingMetrics
|
||||
}
|
||||
@@ -32,6 +34,8 @@ func NewRoutingEngine() *RoutingEngine {
|
||||
|
||||
// RegisterStrategy 注册路由策略
|
||||
func (e *RoutingEngine) RegisterStrategy(name string, template strategy.StrategyTemplate) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
e.strategies[name] = template
|
||||
}
|
||||
|
||||
@@ -54,8 +58,11 @@ func (e *RoutingEngine) SelectProvider(ctx context.Context, req *strategy.Routin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录指标
|
||||
if e.metrics != nil && decision != nil {
|
||||
if decision == nil {
|
||||
return nil, ErrStrategyNotFound
|
||||
}
|
||||
|
||||
if e.metrics != nil {
|
||||
e.metrics.RecordSelection(decision.Provider, decision.Strategy, decision)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user