5秒盾策略变化时只更新策略配置

This commit is contained in:
刘祥超
2023-04-03 15:59:45 +08:00
parent 3485db9a4a
commit c9ae3df3d3
5 changed files with 760 additions and 432 deletions

View File

@@ -19,9 +19,11 @@ import (
"reflect"
"strconv"
"strings"
"sync"
)
var sharedNodeConfig *NodeConfig = nil
var uamPolicyLocker = &sync.RWMutex{}
type ServerError struct {
Id int64
@@ -190,6 +192,9 @@ func CloneNodeConfig(nodeConfig *NodeConfig) (*NodeConfig, error) {
return nil, errors.New("node config should not be nil")
}
uamPolicyLocker.RLock()
defer uamPolicyLocker.RUnlock()
var newConfigValue = reflect.Indirect(reflect.ValueOf(&NodeConfig{}))
var oldValue = reflect.Indirect(reflect.ValueOf(nodeConfig))
var valueType = oldValue.Type()
@@ -374,14 +379,17 @@ func (this *NodeConfig) Init(ctx context.Context) (err error, serverErrors []*Se
}
// uam policy
uamPolicyLocker.RLock()
if this.UAMPolicies != nil {
for _, policy := range this.UAMPolicies {
err = policy.Init()
if err != nil {
uamPolicyLocker.RUnlock()
return
}
}
}
uamPolicyLocker.RUnlock()
// dns resolver
if this.DNSResolver != nil {
@@ -608,12 +616,21 @@ func (this *NodeConfig) FindWebPImagePolicyWithClusterId(clusterId int64) *WebPI
// FindUAMPolicyWithClusterId 使用集群ID查找UAM策略
func (this *NodeConfig) FindUAMPolicyWithClusterId(clusterId int64) *UAMPolicy {
uamPolicyLocker.RLock()
defer uamPolicyLocker.RUnlock()
if this.UAMPolicies == nil {
return nil
}
return this.UAMPolicies[clusterId]
}
// UpdateUAMPolicies 修改集群UAM策略
func (this *NodeConfig) UpdateUAMPolicies(policies map[int64]*UAMPolicy) {
uamPolicyLocker.Lock()
defer uamPolicyLocker.Unlock()
this.UAMPolicies = policies
}
// SecretHash 对Id和Secret的Hash计算
func (this *NodeConfig) SecretHash() string {
return this.secretHash

View File

@@ -97,5 +97,24 @@ func TestNodeConfig_AddServer(t *testing.T) {
for _, s := range config.Servers {
t.Log(s.Id)
}
}
func TestCloneNodeConfig_UAMPolicies(t *testing.T) {
var config = &NodeConfig{}
config.UAMPolicies = map[int64]*UAMPolicy{}
t.Logf("%p", config.UAMPolicies)
clonedConfig, err := CloneNodeConfig(config)
if err != nil {
t.Fatal(err)
}
t.Logf("%p", clonedConfig.UAMPolicies)
}
func BenchmarkNodeConfig(b *testing.B) {
var config = &NodeConfig{}
for i := 0; i < b.N; i++ {
_, _ = CloneNodeConfig(config)
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -169,6 +169,9 @@ service NodeService {
// 修改某个节点的API相关配置
rpc updateNodeAPIConfig(UpdateNodeAPIConfigRequest) returns (RPCSuccess);
// 查找节点的UAM策略
rpc findNodeUAMPolicies(FindNodeUAMPoliciesRequest) returns (FindNodeUAMPoliciesResponse);
}
// 创建节点
@@ -657,4 +660,18 @@ message FindNodeAPIConfigResponse {
message UpdateNodeAPIConfigRequest {
int64 nodeId = 1;
bytes apiNodeAddrsJSON = 2;
}
// 查找节点的UAM策略
message FindNodeUAMPoliciesRequest {
int64 nodeId = 1; // 节点ID
}
message FindNodeUAMPoliciesResponse {
repeated UAMPolicy uamPolicies = 1; // UAM策略列表
message UAMPolicy {
int64 nodeClusterId = 1; // 集群ID
bytes uamPolicyJSON = 2; // UAM策略配置
}
}