优化代码

This commit is contained in:
GoEdgeLab
2020-12-17 17:36:20 +08:00
parent 4ba539c687
commit 83d7528b88
9 changed files with 114 additions and 71 deletions

View File

@@ -228,18 +228,9 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
if err != nil {
return nil, err
}
for _, cacheRef := range cacheConfig.CacheRefs {
if cacheRef.CachePolicyId > 0 {
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(cacheRef.CachePolicyId)
if err != nil {
return nil, err
}
if cachePolicy != nil {
cacheRef.CachePolicy = cachePolicy
}
}
}
config.Cache = cacheConfig
// 暂不支持自定义缓存策略设置,因为同一个集群下的服务需要集中管理
}
// 防火墙配置
@@ -251,17 +242,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
}
config.FirewallRef = firewallRef
if firewallRef.FirewallPolicyId > 0 {
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(firewallRef.FirewallPolicyId)
if err != nil {
return nil, err
}
if firewallPolicy == nil {
config.FirewallRef = nil
} else {
config.FirewallPolicy = firewallPolicy
}
}
// 暂不支持自定义防火墙策略设置,因为同一个集群下的服务需要集中管理
}
// 路径规则

View File

@@ -609,6 +609,14 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClustersWithHTTPFirewallPolicyId(h
return
}
// 获取集群的WAF策略ID
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(clusterId int64) (int64, error) {
return this.Query().
Pk(clusterId).
Result("httpFirewallPolicyId").
FindInt64Col(0)
}
// 设置集群的缓存策略
func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(clusterId int64, httpCachePolicyId int64) error {
_, err := this.Query().
@@ -618,6 +626,14 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(clusterId int64,
return err
}
// 获取集群的缓存策略ID
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(clusterId int64) (int64, error) {
return this.Query().
Pk(clusterId).
Result("cachePolicyId").
FindInt64Col(0)
}
// 设置集群的WAF策略
func (this *NodeClusterDAO) UpdateNodeClusterHTTPFirewallPolicyId(clusterId int64, httpFirewallPolicyId int64) error {
_, err := this.Query().

View File

@@ -143,7 +143,7 @@ func (this *NodeDAO) UpdateNodeLatestVersion(nodeId int64) error {
}
// 批量更新节点版本
func (this *NodeDAO) UpdateAllNodesLatestVersionMatch(clusterId int64) error {
func (this *NodeDAO) IncreaseAllNodesLatestVersionMatch(clusterId int64) error {
_, err := this.Query().
Attr("clusterId", clusterId).
Set("latestVersion", dbs.SQL("latestVersion+1")).
@@ -459,6 +459,7 @@ func (this *NodeDAO) UpdateNodeInstallStatus(nodeId int64, status *NodeInstallSt
}
// 组合配置
// TODO 提升运行速度
func (this *NodeDAO) ComposeNodeConfig(nodeId int64) (*nodeconfigs.NodeConfig, error) {
node, err := this.FindEnabledNode(nodeId)
if err != nil {
@@ -513,8 +514,39 @@ func (this *NodeDAO) ComposeNodeConfig(nodeId int64) (*nodeconfigs.NodeConfig, e
config.GlobalConfig = globalConfig
}
// WAF
clusterId := int64(node.ClusterId)
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(clusterId)
if err != nil {
return nil, err
}
if httpFirewallPolicyId > 0 {
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(httpFirewallPolicyId)
if err != nil {
return nil, err
}
if firewallPolicy != nil {
config.HTTPFirewallPolicy = firewallPolicy
}
}
// 缓存策略
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(clusterId)
if err != nil {
return nil, err
}
if httpCachePolicyId > 0 {
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(httpCachePolicyId)
if err != nil {
return nil, err
}
if cachePolicy != nil {
config.HTTPCachePolicy = cachePolicy
}
}
// TOA
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(int64(node.ClusterId))
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(clusterId)
if err != nil {
return nil, err
}

View File

@@ -54,7 +54,7 @@ func (this *ServerChangeEvent) Run() error {
if !isOk {
continue
}
err = SharedNodeDAO.UpdateAllNodesLatestVersionMatch(clusterId)
err = SharedNodeDAO.IncreaseAllNodesLatestVersionMatch(clusterId)
if err != nil {
return err
}