优化节点配置生成速度

This commit is contained in:
刘祥超
2021-11-11 14:16:42 +08:00
parent 8afd21f2f4
commit 5b32343a2c
49 changed files with 527 additions and 198 deletions

View File

@@ -2,9 +2,9 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
) )
// DecodeHTTP 解析HTTP配置 // DecodeHTTP 解析HTTP配置
@@ -27,11 +27,7 @@ func (this *APINode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
} }
// DecodeHTTPS 解析HTTPS配置 // DecodeHTTPS 解析HTTPS配置
func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) { func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
if !IsNotNull(this.Https) { if !IsNotNull(this.Https) {
return nil, nil return nil, nil
} }
@@ -123,9 +119,9 @@ func (this *APINode) DecodeRestHTTP() (*serverconfigs.HTTPProtocolConfig, error)
} }
// DecodeRestHTTPS 解析HTTPS配置 // DecodeRestHTTPS 解析HTTPS配置
func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) { func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
if this.RestIsOn != 1 { if this.RestIsOn != 1 {
return nil, nil return nil, nil

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes" "github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
@@ -58,15 +59,14 @@ func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
} }
// FindEnabledDNSDomain 查找启用中的条目 // FindEnabledDNSDomain 查找启用中的条目
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap maps.Map) (*DNSDomain, error) { func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cacheMap *utils.CacheMap) (*DNSDomain, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":record:" + types.String(domainId) var cacheKey = this.Table + ":record:" + types.String(domainId)
var cache = cacheMap.Get(cacheKey) if cacheMap != nil {
cache, _ := cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*DNSDomain), nil return cache.(*DNSDomain), nil
} }
}
result, err := this.Query(tx). result, err := this.Query(tx).
Pk(domainId). Pk(domainId).
@@ -75,7 +75,9 @@ func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, domainId int64, cache
if result == nil { if result == nil {
return nil, err return nil, err
} }
cacheMap[cacheKey] = result if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*DNSDomain), err return result.(*DNSDomain), err
} }

View File

@@ -3,11 +3,11 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -96,12 +96,12 @@ func (this *HTTPAuthPolicyDAO) UpdateHTTPAuthPolicy(tx *dbs.Tx, policyId int64,
} }
// ComposePolicyConfig 组合配置 // ComposePolicyConfig 组合配置
func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPAuthPolicy, error) { func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPAuthPolicy, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(policyId) var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.HTTPAuthPolicy), nil return cache.(*serverconfigs.HTTPAuthPolicy), nil
} }
@@ -130,7 +130,9 @@ func (this *HTTPAuthPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, c
} }
config.Params = params config.Params = params
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,12 +3,12 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -225,12 +225,12 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
} }
// ComposeCachePolicy 组合配置 // ComposeCachePolicy 组合配置
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*serverconfigs.HTTPCachePolicy, error) { func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPCachePolicy, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(policyId) var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.HTTPCachePolicy), nil return cache.(*serverconfigs.HTTPCachePolicy), nil
} }
@@ -292,7 +292,9 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64, c
config.CacheRefs = refs config.CacheRefs = refs
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -297,12 +298,12 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, keywo
} }
// ComposeFirewallPolicy 组合策略配置 // ComposeFirewallPolicy 组合策略配置
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*firewallconfigs.HTTPFirewallPolicy, error) { func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*firewallconfigs.HTTPFirewallPolicy, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(policyId) var cacheKey = this.Table + ":config:" + types.String(policyId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*firewallconfigs.HTTPFirewallPolicy), nil return cache.(*firewallconfigs.HTTPFirewallPolicy), nil
} }
@@ -392,7 +393,9 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
config.BlockOptions = blockAction config.BlockOptions = blockAction
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@@ -131,12 +132,12 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
} }
// ComposeLocationConfig 组合配置 // ComposeLocationConfig 组合配置
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap maps.Map) (*serverconfigs.HTTPLocationConfig, error) { func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPLocationConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(locationId) var cacheKey = this.Table + ":config:" + types.String(locationId)
var cacheConfig = cacheMap.Get(cacheKey) var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil { if cacheConfig != nil {
return cacheConfig.(*serverconfigs.HTTPLocationConfig), nil return cacheConfig.(*serverconfigs.HTTPLocationConfig), nil
} }
@@ -194,7 +195,9 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64,
config.Conds = conds config.Conds = conds
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }
@@ -259,7 +262,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
} }
// ConvertLocationRefs 转换引用为配置 // ConvertLocationRefs 转换引用为配置
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap maps.Map) (locations []*serverconfigs.HTTPLocationConfig, err error) { func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef, cacheMap *utils.CacheMap) (locations []*serverconfigs.HTTPLocationConfig, err error) {
for _, ref := range refs { for _, ref := range refs {
config, err := this.ComposeLocationConfig(tx, ref.LocationId, cacheMap) config, err := this.ComposeLocationConfig(tx, ref.LocationId, cacheMap)
if err != nil { if err != nil {

View File

@@ -3,12 +3,12 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -133,12 +133,12 @@ func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []strin
} }
// ComposePageConfig 组合配置 // ComposePageConfig 组合配置
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap maps.Map) (*serverconfigs.HTTPPageConfig, error) { func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPPageConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(pageId) var cacheKey = this.Table + ":config:" + types.String(pageId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.HTTPPageConfig), nil return cache.(*serverconfigs.HTTPPageConfig), nil
} }
@@ -175,7 +175,9 @@ func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap ma
} }
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,12 +3,12 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -77,12 +77,12 @@ func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64)
} }
// ComposeRewriteRule 构造配置 // ComposeRewriteRule 构造配置
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap maps.Map) (*serverconfigs.HTTPRewriteRule, error) { func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPRewriteRule, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(rewriteRuleId) var cacheKey = this.Table + ":config:" + types.String(rewriteRuleId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.HTTPRewriteRule), nil return cache.(*serverconfigs.HTTPRewriteRule), nil
} }
@@ -116,7 +116,9 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
config.Conds = conds config.Conds = conds
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
@@ -75,12 +76,12 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, erro
} }
// ComposeWebConfig 组合配置 // ComposeWebConfig 组合配置
func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.Map) (*serverconfigs.HTTPWebConfig, error) { func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPWebConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(webId) var cacheKey = this.Table + ":config:" + types.String(webId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.HTTPWebConfig), nil return cache.(*serverconfigs.HTTPWebConfig), nil
} }
@@ -418,7 +419,9 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.
config.RemoteAddr = remoteAddrConfig config.RemoteAddr = remoteAddrConfig
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
@@ -55,12 +56,12 @@ func (this *MessageMediaInstanceDAO) DisableMessageMediaInstance(tx *dbs.Tx, id
} }
// FindEnabledMessageMediaInstance 查找启用中的条目 // FindEnabledMessageMediaInstance 查找启用中的条目
func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap maps.Map) (*MessageMediaInstance, error) { func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, instanceId int64, cacheMap *utils.CacheMap) (*MessageMediaInstance, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":record:" + types.String(instanceId) var cacheKey = this.Table + ":record:" + types.String(instanceId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*MessageMediaInstance), nil return cache.(*MessageMediaInstance), nil
} }
@@ -73,7 +74,9 @@ func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx,
return nil, err return nil, err
} }
cacheMap[cacheKey] = result if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*MessageMediaInstance), err return result.(*MessageMediaInstance), err
} }

View File

@@ -3,11 +3,11 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils" "github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"regexp" "regexp"
) )
@@ -57,13 +57,13 @@ func (this *MessageRecipientDAO) DisableMessageRecipient(tx *dbs.Tx, id int64) e
} }
// FindEnabledMessageRecipient 查找启用中的条目 // FindEnabledMessageRecipient 查找启用中的条目
func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap maps.Map, func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipientId int64, cacheMap *utils.CacheMap,
) (*MessageRecipient, error) { ) (*MessageRecipient, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":record:" + types.String(recipientId) var cacheKey = this.Table + ":record:" + types.String(recipientId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*MessageRecipient), nil return cache.(*MessageRecipient), nil
} }
@@ -76,7 +76,9 @@ func (this *MessageRecipientDAO) FindEnabledMessageRecipient(tx *dbs.Tx, recipie
return nil, err return nil, err
} }
cacheMap[cacheKey] = result if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*MessageRecipient), err return result.(*MessageRecipient), err
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@@ -229,7 +230,16 @@ func (this *MetricItemDAO) ListEnabledItems(tx *dbs.Tx, category serverconfigs.M
} }
// FindAllPublicItems 取得公用的指标 // FindAllPublicItems 取得公用的指标
func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (result []*MetricItem, err error) { func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string, cacheMap *utils.CacheMap) (result []*MetricItem, err error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":FindAllPublicItems:" + category
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]*MetricItem), nil
}
_, err = this.Query(tx). _, err = this.Query(tx).
State(MetricItemStateEnabled). State(MetricItemStateEnabled).
Attr("userId", 0). Attr("userId", 0).
@@ -238,6 +248,14 @@ func (this *MetricItemDAO) FindAllPublicItems(tx *dbs.Tx, category string) (resu
DescPk(). DescPk().
Slice(&result). Slice(&result).
FindAll() FindAll()
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return return
} }

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns" "github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -407,15 +408,14 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
} }
// FindClusterDNSInfo 查找DNS信息 // FindClusterDNSInfo 查找DNS信息
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (*NodeCluster, error) { func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*NodeCluster, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId) var cacheKey = this.Table + ":FindClusterDNSInfo:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey) if cacheMap != nil {
if cache != nil { cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*NodeCluster), nil return cache.(*NodeCluster), nil
} }
}
one, err := this.Query(tx). one, err := this.Query(tx).
Pk(clusterId). Pk(clusterId).
@@ -427,7 +427,9 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64, cach
if one == nil { if one == nil {
return nil, nil return nil, nil
} }
cacheMap[cacheKey] = one if cacheMap != nil {
cacheMap.Put(cacheKey, one)
}
return one.(*NodeCluster), nil return one.(*NodeCluster), nil
} }
@@ -487,7 +489,15 @@ func (this *NodeClusterDAO) FindClusterAdminId(tx *dbs.Tx, clusterId int64) (int
} }
// FindClusterTOAConfig 查找集群的TOA设置 // FindClusterTOAConfig 查找集群的TOA设置
func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*nodeconfigs.TOAConfig, error) { func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.TOAConfig, error) {
var cacheKey = this.Table + ":FindClusterTOAConfig:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*nodeconfigs.TOAConfig), nil
}
}
toa, err := this.Query(tx). toa, err := this.Query(tx).
Pk(clusterId). Pk(clusterId).
Result("toa"). Result("toa").
@@ -504,6 +514,11 @@ func (this *NodeClusterDAO) FindClusterTOAConfig(tx *dbs.Tx, clusterId int64) (*
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }
@@ -587,12 +602,12 @@ func (this *NodeClusterDAO) FindAllEnabledNodeClusterIdsWithCachePolicyId(tx *db
} }
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID // FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) { func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":FindClusterHTTPFirewallPolicyId:" + types.String(clusterId) var cacheKey = this.Table + ":FindClusterHTTPFirewallPolicyId:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(int64), nil return cache.(int64), nil
} }
@@ -605,7 +620,9 @@ func (this *NodeClusterDAO) FindClusterHTTPFirewallPolicyId(tx *dbs.Tx, clusterI
return 0, err return 0, err
} }
cacheMap[cacheKey] = firewallPolicyId if cacheMap != nil {
cacheMap.Put(cacheKey, firewallPolicyId)
}
return firewallPolicyId, nil return firewallPolicyId, nil
} }
@@ -623,12 +640,12 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPCachePolicyId(tx *dbs.Tx, clust
} }
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID // FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (int64, error) { func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (int64, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":FindClusterHTTPCachePolicyId:" + types.String(clusterId) var cacheKey = this.Table + ":FindClusterHTTPCachePolicyId:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(int64), nil return cache.(int64), nil
} }
@@ -641,7 +658,9 @@ func (this *NodeClusterDAO) FindClusterHTTPCachePolicyId(tx *dbs.Tx, clusterId i
return 0, err return 0, err
} }
cacheMap[cacheKey] = cachePolicyId if cacheMap != nil {
cacheMap.Put(cacheKey, cachePolicyId)
}
return cachePolicyId, nil return cachePolicyId, nil
} }
@@ -720,10 +739,19 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clust
} }
// FindNodeClusterSystemServices 查找集群的所有服务设置 // FindNodeClusterSystemServices 查找集群的所有服务设置
func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64) (services map[string]maps.Map, err error) { func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (services map[string]maps.Map, err error) {
if clusterId <= 0 { if clusterId <= 0 {
return nil, errors.New("invalid clusterId") return nil, errors.New("invalid clusterId")
} }
var cacheKey = this.Table + ":FindNodeClusterSystemServices:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(map[string]maps.Map), nil
}
}
service, err := this.Query(tx). service, err := this.Query(tx).
Pk(clusterId). Pk(clusterId).
Result("systemServices"). Result("systemServices").
@@ -738,6 +766,11 @@ func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId
return nil, err return nil, err
} }
} }
if cacheMap != nil {
cacheMap.Put(cacheKey, servicesMap)
}
return servicesMap, nil return servicesMap, nil
} }
@@ -816,15 +849,14 @@ func (this *NodeClusterDAO) ExistsEnabledCluster(tx *dbs.Tx, clusterId int64) (b
} }
// FindClusterTimezone 查找时区 // FindClusterTimezone 查找时区
func (this *NodeClusterDAO) FindClusterTimezone(tx *dbs.Tx, clusterId int64, cacheMap maps.Map) (string, error) { func (this *NodeClusterDAO) FindClusterTimezone(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (string, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":FindEnabledTimeZone:" + types.String(clusterId) var cacheKey = this.Table + ":FindEnabledTimeZone:" + types.String(clusterId)
var cache = cacheMap.Get(cacheKey) if cacheMap != nil {
if cache != nil { cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(string), nil return cache.(string), nil
} }
}
timeZone, err := this.Query(tx). timeZone, err := this.Query(tx).
Pk(clusterId). Pk(clusterId).
@@ -833,7 +865,9 @@ func (this *NodeClusterDAO) FindClusterTimezone(tx *dbs.Tx, clusterId int64, cac
if err != nil { if err != nil {
return "", err return "", err
} }
cacheMap[cacheKey] = timeZone if cacheMap != nil {
cacheMap.Put(cacheKey, timeZone)
}
return timeZone, nil return timeZone, nil
} }

View File

@@ -2,11 +2,13 @@ package models
import ( import (
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
) )
const ( const (
@@ -35,7 +37,7 @@ func init() {
}) })
} }
// 启用条目 // EnableFirewallAction 启用条目
func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error { func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx). _, err := this.Query(tx).
Pk(id). Pk(id).
@@ -44,7 +46,7 @@ func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id ui
return err return err
} }
// 禁用条目 // DisableFirewallAction 禁用条目
func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error { func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error {
_, err := this.Query(tx). _, err := this.Query(tx).
Pk(actionId). Pk(actionId).
@@ -56,7 +58,7 @@ func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, acti
return this.NotifyUpdate(tx, actionId) return this.NotifyUpdate(tx, actionId)
} }
// 查找启用中的条目 // FindEnabledFirewallAction 查找启用中的条目
func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) { func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) {
result, err := this.Query(tx). result, err := this.Query(tx).
Pk(actionId). Pk(actionId).
@@ -68,7 +70,7 @@ func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx,
return result.(*NodeClusterFirewallAction), err return result.(*NodeClusterFirewallAction), err
} }
// 根据主键查找名称 // FindFirewallActionName 根据主键查找名称
func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) { func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx). return this.Query(tx).
Pk(id). Pk(id).
@@ -76,7 +78,7 @@ func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id
FindStringCol("") FindStringCol("")
} }
// 创建动作 // CreateFirewallAction 创建动作
func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) { func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) {
if params == nil { if params == nil {
params = maps.Map{} params = maps.Map{}
@@ -101,7 +103,7 @@ func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, admin
return actionId, nil return actionId, nil
} }
// 修改动作 // UpdateFirewallAction 修改动作
func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error { func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error {
if actionId <= 0 { if actionId <= 0 {
return errors.New("invalid actionId") return errors.New("invalid actionId")
@@ -124,17 +126,33 @@ func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actio
return this.NotifyUpdate(tx, actionId) return this.NotifyUpdate(tx, actionId)
} }
// 查找所有集群的动作 // FindAllEnabledFirewallActions 查找所有集群的动作
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (result []*NodeClusterFirewallAction, err error) { func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []*NodeClusterFirewallAction, err error) {
var cacheKey = this.Table + ":FindAllEnabledFirewallActions:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]*NodeClusterFirewallAction), nil
}
}
_, err = this.Query(tx). _, err = this.Query(tx).
Attr("clusterId", clusterId). Attr("clusterId", clusterId).
State(NodeClusterFirewallActionStateEnabled). State(NodeClusterFirewallActionStateEnabled).
Slice(&result). Slice(&result).
FindAll() FindAll()
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return return
} }
// 组合配置 // ComposeFirewallActionConfig 组合配置
func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) { func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) {
if action == nil { if action == nil {
return nil, nil return nil, nil
@@ -153,7 +171,7 @@ func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx
return config, nil return config, nil
} }
// 计算动作数量 // CountAllEnabledFirewallActions 计算动作数量
func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) { func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx). return this.Query(tx).
State(NodeClusterFirewallActionStateEnabled). State(NodeClusterFirewallActionStateEnabled).
@@ -161,7 +179,7 @@ func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs
Count() Count()
} }
// 通知更新 // NotifyUpdate 通知更新
func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error { func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error {
clusterId, err := this.Query(tx). clusterId, err := this.Query(tx).
Pk(actionId). Pk(actionId).

View File

@@ -2,10 +2,12 @@ package models
import ( import (
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
) )
const ( const (
@@ -112,16 +114,32 @@ func (this *NodeClusterMetricItemDAO) FindAllClusterItems(tx *dbs.Tx, clusterId
} }
// FindAllClusterItemIds 查找某个集群的指标Ids // FindAllClusterItemIds 查找某个集群的指标Ids
func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64) (result []int64, err error) { func (this *NodeClusterMetricItemDAO) FindAllClusterItemIds(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []int64, err error) {
var cacheKey = this.Table + ":FindAllClusterItemIds:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]int64), nil
}
}
ones, err := this.Query(tx). ones, err := this.Query(tx).
Attr("clusterId", clusterId). Attr("clusterId", clusterId).
State(NodeClusterMetricItemStateEnabled). State(NodeClusterMetricItemStateEnabled).
Result("itemId"). Result("itemId").
DescPk(). DescPk().
FindAll() FindAll()
if err != nil {
return nil, err
}
for _, one := range ones { for _, one := range ones {
result = append(result, int64(one.(*NodeClusterMetricItem).ItemId)) result = append(result, int64(one.(*NodeClusterMetricItem).ItemId))
} }
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return return
} }

View File

@@ -701,7 +701,11 @@ func (this *NodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *N
// ComposeNodeConfig 组合配置 // ComposeNodeConfig 组合配置
// TODO 提升运行速度 // TODO 提升运行速度
func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.Map) (*nodeconfigs.NodeConfig, error) { func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils.CacheMap) (*nodeconfigs.NodeConfig, error) {
if cacheMap == nil {
cacheMap = utils.NewCacheMap()
}
node, err := this.FindEnabledNode(tx, nodeId) node, err := this.FindEnabledNode(tx, nodeId)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -745,10 +749,19 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
// 全局设置 // 全局设置
// TODO 根据用户的不同读取不同的全局设置 // TODO 根据用户的不同读取不同的全局设置
settingJSON, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig) var settingCacheKey = "SharedSysSettingDAO:" + systemconfigs.SettingCodeServerGlobalConfig
settingJSONCache, ok := cacheMap.Get(settingCacheKey)
var settingJSON = []byte{}
if ok {
settingJSON = settingJSONCache.([]byte)
} else {
settingJSON, err = SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cacheMap.Put(settingCacheKey, settingJSON)
}
if len(settingJSON) > 0 { if len(settingJSON) > 0 {
globalConfig := &serverconfigs.GlobalConfig{} globalConfig := &serverconfigs.GlobalConfig{}
err = json.Unmarshal(settingJSON, globalConfig) err = json.Unmarshal(settingJSON, globalConfig)
@@ -825,14 +838,14 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
} }
// TOA // TOA
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId) toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.TOA = toaConfig config.TOA = toaConfig
// 系统服务 // 系统服务
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId) services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -841,7 +854,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
} }
// 防火墙动作 // 防火墙动作
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId) actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -856,7 +869,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
} }
// 集群指标 // 集群指标
metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId)) metricItemIds, err := SharedNodeClusterMetricItemDAO.FindAllClusterItemIds(tx, int64(node.ClusterId), cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -872,7 +885,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
} }
// 公用指标 // 公用指标
publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP) publicMetricItems, err := SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1427,7 +1440,7 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
return err return err
} }
if clusterId > 0 { if clusterId > 0 {
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged) return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged, 0)
} }
return nil return nil
} }

View File

@@ -1,9 +1,9 @@
package models package models
import ( import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"testing" "testing"
"time" "time"
) )
@@ -46,13 +46,13 @@ func TestNodeDAO_ComposeNodeConfig(t *testing.T) {
}() }()
var tx *dbs.Tx var tx *dbs.Tx
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
nodeConfig, err := SharedNodeDAO.ComposeNodeConfig(tx, 48, cacheMap) nodeConfig, err := SharedNodeDAO.ComposeNodeConfig(tx, 48, cacheMap)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(len(nodeConfig.Servers), "servers") t.Log(len(nodeConfig.Servers), "servers")
t.Log(len(cacheMap), "items") t.Log(cacheMap.Len(), "items")
// old: 77ms => new: 56ms // old: 77ms => new: 56ms
} }

View File

@@ -49,7 +49,7 @@ func init() {
} }
// CreateNodeTask 创建单个节点任务 // CreateNodeTask 创建单个节点任务
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType) error { func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType, version int64) error {
if clusterId <= 0 || nodeId <= 0 { if clusterId <= 0 || nodeId <= 0 {
return nil return nil
} }
@@ -66,6 +66,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
"isDone": 0, "isDone": 0,
"isOk": 0, "isOk": 0,
"error": "", "error": "",
"version": version,
}, maps.Map{ }, maps.Map{
"clusterId": clusterId, "clusterId": clusterId,
"updatedAt": updatedAt, "updatedAt": updatedAt,
@@ -73,6 +74,7 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64
"isOk": 0, "isOk": 0,
"error": "", "error": "",
"isNotified": 0, "isNotified": 0,
"version": version,
}) })
return err return err
} }
@@ -97,12 +99,14 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId in
"isOk": 0, "isOk": 0,
"isNotified": 0, "isNotified": 0,
"error": "", "error": "",
"version": time.Now().UnixNano(),
}, maps.Map{ }, maps.Map{
"updatedAt": updatedAt, "updatedAt": updatedAt,
"isDone": 0, "isDone": 0,
"isOk": 0, "isOk": 0,
"isNotified": 0, "isNotified": 0,
"error": "", "error": "",
"version": time.Now().UnixNano(),
}) })
return err return err
} }
@@ -125,8 +129,9 @@ func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, tas
return err return err
} }
var version = time.Now().UnixNano()
for _, nodeId := range nodeIds { for _, nodeId := range nodeIds {
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType) err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType, version)
if err != nil { if err != nil {
return err return err
} }
@@ -156,15 +161,16 @@ func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskT
Attr("role", nodeconfigs.NodeRoleDNS). Attr("role", nodeconfigs.NodeRoleDNS).
Attr("clusterId", clusterId). Attr("clusterId", clusterId).
Param("clusterIdString", types.String(clusterId)). Param("clusterIdString", types.String(clusterId)).
Where("nodeId> 0"). Where("nodeId > 0").
Attr("type", taskType). Attr("type", taskType).
Delete() Delete()
if err != nil { if err != nil {
return err return err
} }
var version = time.Now().UnixNano()
for _, nodeId := range nodeIds { for _, nodeId := range nodeIds {
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType) err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType, version)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -11,7 +11,7 @@ func TestNodeTaskDAO_CreateNodeTask(t *testing.T) {
dbs.NotifyReady() dbs.NotifyReady()
var tx *dbs.Tx var tx *dbs.Tx
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged) err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -13,6 +13,7 @@ type NodeTask struct {
IsOk uint8 `field:"isOk"` // 是否已完成 IsOk uint8 `field:"isOk"` // 是否已完成
Error string `field:"error"` // 错误信息 Error string `field:"error"` // 错误信息
IsNotified uint8 `field:"isNotified"` // 是否已通知更新 IsNotified uint8 `field:"isNotified"` // 是否已通知更新
Version uint64 `field:"version"` // 版本
} }
type NodeTaskOperator struct { type NodeTaskOperator struct {
@@ -27,6 +28,7 @@ type NodeTaskOperator struct {
IsOk interface{} // 是否已完成 IsOk interface{} // 是否已完成
Error interface{} // 错误信息 Error interface{} // 错误信息
IsNotified interface{} // 是否已通知更新 IsNotified interface{} // 是否已通知更新
Version interface{} // 版本
} }
func NewNodeTaskOperator() *NodeTaskOperator { func NewNodeTaskOperator() *NodeTaskOperator {

View File

@@ -3,13 +3,13 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -220,12 +220,12 @@ func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, add
} }
// ComposeOriginConfig 将源站信息转换为配置 // ComposeOriginConfig 将源站信息转换为配置
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap maps.Map) (*serverconfigs.OriginConfig, error) { func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap *utils.CacheMap) (*serverconfigs.OriginConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(originId) var cacheKey = this.Table + ":config:" + types.String(originId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.OriginConfig), nil return cache.(*serverconfigs.OriginConfig), nil
} }
@@ -358,7 +358,9 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64, cacheMap
// TODO // TODO
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,11 +3,11 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
) )
@@ -223,11 +223,11 @@ func (this *PlanDAO) SortPlans(tx *dbs.Tx, planIds []int64) error {
} }
// FindEnabledPlanTrafficLimit 获取套餐的流量限制 // FindEnabledPlanTrafficLimit 获取套餐的流量限制
func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cacheMap maps.Map) (*serverconfigs.TrafficLimitConfig, error) { func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
var cacheKey = this.Table + ":FindEnabledPlanTrafficLimit:" + types.String(planId) var cacheKey = this.Table + ":FindEnabledPlanTrafficLimit:" + types.String(planId)
if cacheMap != nil { if cacheMap != nil {
cache, ok := cacheMap[cacheKey] cache, _ := cacheMap.Get(cacheKey)
if ok { if cache != nil {
return cache.(*serverconfigs.TrafficLimitConfig), nil return cache.(*serverconfigs.TrafficLimitConfig), nil
} }
} }
@@ -250,7 +250,7 @@ func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cache
} }
if cacheMap != nil { if cacheMap != nil {
cacheMap[cacheKey] = config cacheMap.Put(cacheKey, config)
} }
return config, nil return config, nil

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@@ -80,12 +81,12 @@ func (this *ReverseProxyDAO) FindEnabledReverseProxy(tx *dbs.Tx, id int64) (*Rev
} }
// ComposeReverseProxyConfig 根据ID组合配置 // ComposeReverseProxyConfig 根据ID组合配置
func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyId int64, cacheMap maps.Map) (*serverconfigs.ReverseProxyConfig, error) { func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyId int64, cacheMap *utils.CacheMap) (*serverconfigs.ReverseProxyConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(reverseProxyId) var cacheKey = this.Table + ":config:" + types.String(reverseProxyId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*serverconfigs.ReverseProxyConfig), nil return cache.(*serverconfigs.ReverseProxyConfig), nil
} }
@@ -200,7 +201,9 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(tx *dbs.Tx, reverseProxyI
config.ProxyProtocol = proxyProtocolConfig config.ProxyProtocol = proxyProtocolConfig
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -54,7 +54,7 @@ func init() {
// SaveStats 提交数据 // SaveStats 提交数据
func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailyStat) error { func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailyStat) error {
var serverUserMap = map[int64]int64{} // serverId => userId var serverUserMap = map[int64]int64{} // serverId => userId
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
for _, stat := range stats { for _, stat := range stats {
day := timeutil.FormatTime("Ymd", stat.CreatedAt) day := timeutil.FormatTime("Ymd", stat.CreatedAt)
hour := timeutil.FormatTime("YmdH", stat.CreatedAt) hour := timeutil.FormatTime("YmdH", stat.CreatedAt)

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns" "github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils" "github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -839,13 +840,18 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
// ComposeServerConfig 构造服务的Config // ComposeServerConfig 构造服务的Config
// forNode 是否是节点请求 // forNode 是否是节点请求
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap maps.Map, forNode bool) (*serverconfigs.ServerConfig, error) { func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap *utils.CacheMap, forNode bool) (*serverconfigs.ServerConfig, error) {
if server == nil { if server == nil {
return nil, ErrNotFound return nil, ErrNotFound
} }
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
}
var cacheKey = this.Table + ":ComposeServerConfig:" + types.String(server.Id)
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*serverconfigs.ServerConfig), nil
} }
config := &serverconfigs.ServerConfig{} config := &serverconfigs.ServerConfig{}
@@ -1090,6 +1096,10 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
} }
} }
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }
@@ -1812,12 +1822,12 @@ func (this *ServerDAO) NotifyServerPortsUpdate(tx *dbs.Tx, serverId int64) error
} }
// FindServerTrafficLimitConfig 查找服务的流量限制 // FindServerTrafficLimitConfig 查找服务的流量限制
func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap maps.Map) (*serverconfigs.TrafficLimitConfig, error) { func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId) var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId)
result, ok := cacheMap[cacheKey] result, ok := cacheMap.Get(cacheKey)
if ok { if ok {
return result.(*serverconfigs.TrafficLimitConfig), nil return result.(*serverconfigs.TrafficLimitConfig), nil
} }
@@ -1842,19 +1852,21 @@ func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64,
return nil, err return nil, err
} }
cacheMap[cacheKey] = limit if cacheMap != nil {
cacheMap.Put(cacheKey, limit)
}
return limit, nil return limit, nil
} }
// CalculateServerTrafficLimitConfig 计算服务的流量限制 // CalculateServerTrafficLimitConfig 计算服务的流量限制
// TODO 优化性能 // TODO 优化性能
func (this *ServerDAO) CalculateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap maps.Map) (*serverconfigs.TrafficLimitConfig, error) { func (this *ServerDAO) CalculateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap *utils.CacheMap) (*serverconfigs.TrafficLimitConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId) var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId)
result, ok := cacheMap[cacheKey] result, ok := cacheMap.Get(cacheKey)
if ok { if ok {
return result.(*serverconfigs.TrafficLimitConfig), nil return result.(*serverconfigs.TrafficLimitConfig), nil
} }
@@ -1917,7 +1929,9 @@ func (this *ServerDAO) CalculateServerTrafficLimitConfig(tx *dbs.Tx, serverId in
} }
} }
cacheMap[cacheKey] = limitConfig if cacheMap != nil {
cacheMap.Put(cacheKey, limitConfig)
}
return limitConfig, nil return limitConfig, nil
} }

View File

@@ -3,12 +3,12 @@ package models
import ( import (
"crypto/md5" "crypto/md5"
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"testing" "testing"
"time" "time"
) )
@@ -140,6 +140,7 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) { func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) {
dbs.NotifyReady() dbs.NotifyReady()
var before = time.Now()
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48) servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -147,6 +148,34 @@ func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) {
for _, server := range servers { for _, server := range servers {
t.Log("serverId:", server.Id, "clusterId:", server.ClusterId) t.Log("serverId:", server.Id, "clusterId:", server.ClusterId)
} }
t.Log(time.Since(before).Seconds()*1000, "ms")
}
func TestServerDAO_FindAllEnabledServersWithNode_Cache(t *testing.T) {
dbs.NotifyReady()
var cacheMap = utils.NewCacheMap()
{
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil {
t.Fatal(err)
}
for _, server := range servers {
_, _ = SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
}
}
var before = time.Now()
{
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
if err != nil {
t.Fatal(err)
}
for _, server := range servers {
_, _ = SharedServerDAO.ComposeServerConfig(nil, server, cacheMap, true)
}
}
t.Log(time.Since(before).Seconds()*1000, "ms")
} }
func TestServerDAO_FindAllEnabledServersWithDomain(t *testing.T) { func TestServerDAO_FindAllEnabledServersWithDomain(t *testing.T) {
@@ -195,7 +224,7 @@ func TestServerDAO_CalculateServerTrafficLimitConfig(t *testing.T) {
t.Log(time.Since(before).Seconds()*1000, "ms") t.Log(time.Since(before).Seconds()*1000, "ms")
}() }()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap) config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -212,7 +241,7 @@ func TestServerDAO_CalculateServerTrafficLimitConfig_Cache(t *testing.T) {
t.Log(time.Since(before).Seconds()*1000, "ms") t.Log(time.Since(before).Seconds()*1000, "ms")
}() }()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap) config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
if err != nil { if err != nil {

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -267,13 +268,13 @@ func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, erro
} }
// ComposeGroupConfig 组合配置 // ComposeGroupConfig 组合配置
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap maps.Map) (*serverconfigs.ServerGroupConfig, error) { func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap *utils.CacheMap) (*serverconfigs.ServerGroupConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(groupId) var cacheKey = this.Table + ":config:" + types.String(groupId)
var cacheConfig = cacheMap.Get(cacheKey) var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil { if cacheConfig != nil {
// 克隆防止分解后的Server配置相互受到影响 // 克隆防止分解后的Server配置相互受到影响
configJSON, err := json.Marshal(cacheConfig) configJSON, err := json.Marshal(cacheConfig)
@@ -365,7 +366,9 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
} }
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,11 +3,11 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"time" "time"
@@ -164,12 +164,12 @@ func (this *SSLCertDAO) UpdateCert(tx *dbs.Tx, certId int64, isOn bool, name str
} }
// ComposeCertConfig 组合配置 // ComposeCertConfig 组合配置
func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap maps.Map) (*sslconfigs.SSLCertConfig, error) { func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap *utils.CacheMap) (*sslconfigs.SSLCertConfig, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(certId) var cacheKey = this.Table + ":config:" + types.String(certId)
var cache = cacheMap.Get(cacheKey) var cache, _ = cacheMap.Get(cacheKey)
if cache != nil { if cache != nil {
return cache.(*sslconfigs.SSLCertConfig), nil return cache.(*sslconfigs.SSLCertConfig), nil
} }
@@ -213,7 +213,9 @@ func (this *SSLCertDAO) ComposeCertConfig(tx *dbs.Tx, certId int64, cacheMap map
config.CommonNames = commonNames config.CommonNames = commonNames
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -3,6 +3,7 @@ package models
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -76,13 +77,13 @@ func (this *SSLPolicyDAO) FindEnabledSSLPolicy(tx *dbs.Tx, id int64) (*SSLPolicy
} }
// ComposePolicyConfig 组合配置 // ComposePolicyConfig 组合配置
func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap maps.Map) (*sslconfigs.SSLPolicy, error) { func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheMap *utils.CacheMap) (*sslconfigs.SSLPolicy, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":config:" + types.String(policyId) var cacheKey = this.Table + ":config:" + types.String(policyId)
var cacheConfig = cacheMap.Get(cacheKey) var cacheConfig, _ = cacheMap.Get(cacheKey)
if cacheConfig != nil { if cacheConfig != nil {
return cacheConfig.(*sslconfigs.SSLPolicy), nil return cacheConfig.(*sslconfigs.SSLPolicy), nil
} }
@@ -166,7 +167,9 @@ func (this *SSLPolicyDAO) ComposePolicyConfig(tx *dbs.Tx, policyId int64, cacheM
config.HSTS = hstsConfig config.HSTS = hstsConfig
} }
cacheMap[cacheKey] = config if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil return config, nil
} }

View File

@@ -8,7 +8,6 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
@@ -57,12 +56,12 @@ func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err
} }
// FindEnabledUser 查找启用的用户 // FindEnabledUser 查找启用的用户
func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, userId int64, cacheMap maps.Map) (*User, error) { func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, userId int64, cacheMap *utils.CacheMap) (*User, error) {
if cacheMap == nil { if cacheMap == nil {
cacheMap = maps.Map{} cacheMap = utils.NewCacheMap()
} }
var cacheKey = this.Table + ":FindEnabledUser:" + types.String(userId) var cacheKey = this.Table + ":FindEnabledUser:" + types.String(userId)
cache, ok := cacheMap[cacheKey] cache, ok := cacheMap.Get(cacheKey)
if ok { if ok {
return cache.(*User), nil return cache.(*User), nil
} }
@@ -75,7 +74,9 @@ func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, userId int64, cacheMap maps.Map
return nil, err return nil, err
} }
cacheMap[cacheKey] = result if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*User), err return result.(*User), err
} }

View File

@@ -2,8 +2,8 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
) )
// DecodeHTTP 解析HTTP配置 // DecodeHTTP 解析HTTP配置
@@ -26,7 +26,7 @@ func (this *UserNode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
} }
// DecodeHTTPS 解析HTTPS配置 // DecodeHTTPS 解析HTTPS配置
func (this *UserNode) DecodeHTTPS(cacheMap maps.Map) (*serverconfigs.HTTPSProtocolConfig, error) { func (this *UserNode) DecodeHTTPS(cacheMap *utils.CacheMap) (*serverconfigs.HTTPSProtocolConfig, error) {
if !IsNotNull(this.Https) { if !IsNotNull(this.Https) {
return nil, nil return nil, nil
} }

View File

@@ -2,10 +2,10 @@ package models
import ( import (
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"time" "time"
@@ -61,10 +61,10 @@ func (this *UserPlanDAO) DisableUserPlan(tx *dbs.Tx, id int64) error {
} }
// FindEnabledUserPlan 查找启用中的条目 // FindEnabledUserPlan 查找启用中的条目
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cacheMap maps.Map) (*UserPlan, error) { func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cacheMap *utils.CacheMap) (*UserPlan, error) {
var cacheKey = this.Table + ":FindEnabledUserPlan:" + types.String(userPlanId) var cacheKey = this.Table + ":FindEnabledUserPlan:" + types.String(userPlanId)
if cacheMap != nil { if cacheMap != nil {
cache, ok := cacheMap[cacheKey] cache, ok := cacheMap.Get(cacheKey)
if ok { if ok {
return cache.(*UserPlan), nil return cache.(*UserPlan), nil
} }
@@ -79,7 +79,7 @@ func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cache
} }
if cacheMap != nil { if cacheMap != nil {
cacheMap[cacheKey] = result cacheMap.Put(cacheKey, result)
} }
return result.(*UserPlan), err return result.(*UserPlan), err

View File

@@ -8,8 +8,8 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services" "github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
) )
// NSDomainService 域名相关服务 // NSDomainService 域名相关服务
@@ -149,7 +149,7 @@ func (this *NSDomainService) ListEnabledNSDomains(ctx context.Context, req *pb.L
return nil, err return nil, err
} }
pbDomains := []*pb.NSDomain{} pbDomains := []*pb.NSDomain{}
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
for _, domain := range domains { for _, domain := range domains {
// 集群 // 集群
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId)) cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId))

View File

@@ -755,7 +755,7 @@ func (this *AdminService) UpdateAdminTheme(ctx context.Context, req *pb.UpdateAd
// 查找集群、节点和服务的指标数据 // 查找集群、节点和服务的指标数据
func (this *AdminService) findMetricDataCharts(tx *dbs.Tx) (result []*pb.MetricDataChart, err error) { func (this *AdminService) findMetricDataCharts(tx *dbs.Tx) (result []*pb.MetricDataChart, err error) {
// 集群指标 // 集群指标
items, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP) items, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
@@ -147,7 +148,7 @@ func (this *MessageMediaInstanceService) FindEnabledMessageMediaInstance(ctx con
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
instance, err := models.SharedMessageMediaInstanceDAO.FindEnabledMessageMediaInstance(tx, req.MessageMediaInstanceId, cacheMap) instance, err := models.SharedMessageMediaInstanceDAO.FindEnabledMessageMediaInstance(tx, req.MessageMediaInstanceId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
@@ -69,7 +70,7 @@ func (this *MessageReceiverService) FindAllEnabledMessageReceivers(ctx context.C
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
receivers, err := models.SharedMessageReceiverDAO.FindAllEnabledReceivers(tx, req.Role, req.NodeClusterId, req.NodeId, req.ServerId, "") receivers, err := models.SharedMessageReceiverDAO.FindAllEnabledReceivers(tx, req.Role, req.NodeClusterId, req.NodeId, req.ServerId, "")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -3,8 +3,8 @@ package services
import ( import (
"context" "context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
) )
// MessageRecipientService 消息接收人服务 // MessageRecipientService 消息接收人服务
@@ -83,7 +83,7 @@ func (this *MessageRecipientService) ListEnabledMessageRecipients(ctx context.Co
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
recipients, err := models.SharedMessageRecipientDAO.ListAllEnabledRecipients(tx, req.AdminId, req.MessageRecipientGroupId, req.MediaType, req.Keyword, req.Offset, req.Size) recipients, err := models.SharedMessageRecipientDAO.ListAllEnabledRecipients(tx, req.AdminId, req.MessageRecipientGroupId, req.MediaType, req.Keyword, req.Offset, req.Size)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -163,7 +163,7 @@ func (this *MessageRecipientService) FindEnabledMessageRecipient(ctx context.Con
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
recipient, err := models.SharedMessageRecipientDAO.FindEnabledMessageRecipient(tx, req.MessageRecipientId, cacheMap) recipient, err := models.SharedMessageRecipientDAO.FindEnabledMessageRecipient(tx, req.MessageRecipientId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
@@ -37,7 +38,7 @@ func (this *MessageTaskService) FindSendingMessageTasks(ctx context.Context, req
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
tasks, err := models.SharedMessageTaskDAO.FindSendingMessageTasks(tx, req.Size) tasks, err := models.SharedMessageTaskDAO.FindSendingMessageTasks(tx, req.Size)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -186,7 +187,7 @@ func (this *MessageTaskService) FindEnabledMessageTask(ctx context.Context, req
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
task, err := models.SharedMessageTaskDAO.FindEnabledMessageTask(tx, req.MessageTaskId) task, err := models.SharedMessageTaskDAO.FindEnabledMessageTask(tx, req.MessageTaskId)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -305,7 +306,7 @@ func (this *MessageTaskService) ListMessageTasksWithStatus(ctx context.Context,
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
tasks, err := models.SharedMessageTaskDAO.ListMessageTasksWithStatus(tx, types.Int(req.Status), req.Offset, req.Size) tasks, err := models.SharedMessageTaskDAO.ListMessageTasksWithStatus(tx, types.Int(req.Status), req.Offset, req.Size)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -3,8 +3,8 @@ package services
import ( import (
"context" "context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
) )
// MessageTaskLogService 消息发送日志相关服务 // MessageTaskLogService 消息发送日志相关服务
@@ -33,7 +33,7 @@ func (this *MessageTaskLogService) ListMessageTaskLogs(ctx context.Context, req
return nil, err return nil, err
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
logs, err := models.SharedMessageTaskLogDAO.ListLogs(tx, req.Offset, req.Size) logs, err := models.SharedMessageTaskLogDAO.ListLogs(tx, req.Offset, req.Size)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -10,6 +10,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/installers" "github.com/TeaOSLab/EdgeAPI/internal/installers"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils" "github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -23,8 +24,18 @@ import (
"net" "net"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time"
) )
// NodeVersionCache 节点版本缓存
type NodeVersionCache struct {
CacheMap map[int64]*utils.CacheMap // version => map
}
var nodeVersionCacheMap = map[int64]*NodeVersionCache{} // [cluster_id]_[version] => cache
var nodeVersionCacheLocker = &sync.Mutex{}
// NodeService 边缘节点相关服务 // NodeService 边缘节点相关服务
type NodeService struct { type NodeService struct {
BaseService BaseService
@@ -604,8 +615,6 @@ func (this *NodeService) FindEnabledBasicNode(ctx context.Context, req *pb.FindE
// FindCurrentNodeConfig 组合节点配置 // FindCurrentNodeConfig 组合节点配置
func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.FindCurrentNodeConfigRequest) (*pb.FindCurrentNodeConfigResponse, error) { func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.FindCurrentNodeConfigRequest) (*pb.FindCurrentNodeConfigResponse, error) {
_ = req
// 校验节点 // 校验节点
_, _, nodeId, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode) _, _, nodeId, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
if err != nil { if err != nil {
@@ -623,7 +632,12 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find
return &pb.FindCurrentNodeConfigResponse{IsChanged: false}, nil return &pb.FindCurrentNodeConfigResponse{IsChanged: false}, nil
} }
nodeConfig, err := models.SharedNodeDAO.ComposeNodeConfig(tx, nodeId, nil) clusterId, err := models.SharedNodeDAO.FindNodeClusterId(tx, nodeId)
if err != nil {
return nil, err
}
var cacheMap = this.findClusterCacheMap(clusterId, req.NodeTaskVersion)
nodeConfig, err := models.SharedNodeDAO.ComposeNodeConfig(tx, nodeId, cacheMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -633,6 +647,7 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find
return nil, err return nil, err
} }
// 压缩
var isCompressed = false var isCompressed = false
if req.Compress { if req.Compress {
var buf = &bytes.Buffer{} var buf = &bytes.Buffer{}
@@ -1561,3 +1576,37 @@ func (this *NodeService) UpdateNodeCache(ctx context.Context, req *pb.UpdateNode
return this.Success() return this.Success()
} }
// 获取缓存CacheMap
func (this *NodeService) findClusterCacheMap(clusterId int64, version int64) *utils.CacheMap {
nodeVersionCacheLocker.Lock()
defer nodeVersionCacheLocker.Unlock()
cache, ok := nodeVersionCacheMap[clusterId]
if ok {
cacheMap, ok := cache.CacheMap[version]
if ok {
return cacheMap
}
// 清除以前版本
for v := range cache.CacheMap {
if version-v > 60*time.Second.Nanoseconds() {
delete(cache.CacheMap, v)
}
}
// 添加
cacheMap = utils.NewCacheMap()
cache.CacheMap[version] = cacheMap
return cacheMap
} else {
var cacheMap = utils.NewCacheMap()
cache = &NodeVersionCache{
CacheMap: map[int64]*utils.CacheMap{
version: cacheMap,
}}
nodeVersionCacheMap[clusterId] = cache
return cacheMap
}
}

View File

@@ -665,7 +665,7 @@ func (this *NodeClusterService) FindEnabledNodeClusterTOA(ctx context.Context, r
tx := this.NullTx() tx := this.NullTx()
config, err := models.SharedNodeClusterDAO.FindClusterTOAConfig(tx, req.NodeClusterId) config, err := models.SharedNodeClusterDAO.FindClusterTOAConfig(tx, req.NodeClusterId, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -8,12 +8,12 @@ import (
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
) )
// 防火墙动作服务 // NodeClusterFirewallActionService 防火墙动作服务
type NodeClusterFirewallActionService struct { type NodeClusterFirewallActionService struct {
BaseService BaseService
} }
// 创建动作 // CreateNodeClusterFirewallAction 创建动作
func (this *NodeClusterFirewallActionService) CreateNodeClusterFirewallAction(ctx context.Context, req *pb.CreateNodeClusterFirewallActionRequest) (*pb.NodeClusterFirewallActionResponse, error) { func (this *NodeClusterFirewallActionService) CreateNodeClusterFirewallAction(ctx context.Context, req *pb.CreateNodeClusterFirewallActionRequest) (*pb.NodeClusterFirewallActionResponse, error) {
adminId, err := this.ValidateAdmin(ctx, 0) adminId, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {
@@ -36,7 +36,7 @@ func (this *NodeClusterFirewallActionService) CreateNodeClusterFirewallAction(ct
return &pb.NodeClusterFirewallActionResponse{NodeClusterFirewallActionId: actionId}, nil return &pb.NodeClusterFirewallActionResponse{NodeClusterFirewallActionId: actionId}, nil
} }
// 修改动作 // UpdateNodeClusterFirewallAction 修改动作
func (this *NodeClusterFirewallActionService) UpdateNodeClusterFirewallAction(ctx context.Context, req *pb.UpdateNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) { func (this *NodeClusterFirewallActionService) UpdateNodeClusterFirewallAction(ctx context.Context, req *pb.UpdateNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0) _, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {
@@ -59,7 +59,7 @@ func (this *NodeClusterFirewallActionService) UpdateNodeClusterFirewallAction(ct
return this.Success() return this.Success()
} }
// 删除动作 // DeleteNodeClusterFirewallAction 删除动作
func (this *NodeClusterFirewallActionService) DeleteNodeClusterFirewallAction(ctx context.Context, req *pb.DeleteNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) { func (this *NodeClusterFirewallActionService) DeleteNodeClusterFirewallAction(ctx context.Context, req *pb.DeleteNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0) _, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {
@@ -74,7 +74,7 @@ func (this *NodeClusterFirewallActionService) DeleteNodeClusterFirewallAction(ct
return this.Success() return this.Success()
} }
// 查询集群的所有动作 // FindAllEnabledNodeClusterFirewallActions 查询集群的所有动作
func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.FindAllEnabledNodeClusterFirewallActionsRequest) (*pb.FindAllEnabledNodeClusterFirewallActionsResponse, error) { func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.FindAllEnabledNodeClusterFirewallActionsRequest) (*pb.FindAllEnabledNodeClusterFirewallActionsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0) _, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {
@@ -82,7 +82,7 @@ func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallA
} }
var tx = this.NullTx() var tx = this.NullTx()
actions, err := models.SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, req.NodeClusterId) actions, err := models.SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, req.NodeClusterId, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -100,7 +100,7 @@ func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallA
return &pb.FindAllEnabledNodeClusterFirewallActionsResponse{NodeClusterFirewallActions: pbActions}, nil return &pb.FindAllEnabledNodeClusterFirewallActionsResponse{NodeClusterFirewallActions: pbActions}, nil
} }
// 查询单个动作 // FindEnabledNodeClusterFirewallAction 查询单个动作
func (this *NodeClusterFirewallActionService) FindEnabledNodeClusterFirewallAction(ctx context.Context, req *pb.FindEnabledNodeClusterFirewallActionRequest) (*pb.FindEnabledNodeClusterFirewallActionResponse, error) { func (this *NodeClusterFirewallActionService) FindEnabledNodeClusterFirewallAction(ctx context.Context, req *pb.FindEnabledNodeClusterFirewallActionRequest) (*pb.FindEnabledNodeClusterFirewallActionResponse, error) {
_, err := this.ValidateAdmin(ctx, 0) _, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {
@@ -125,7 +125,7 @@ func (this *NodeClusterFirewallActionService) FindEnabledNodeClusterFirewallActi
}}, nil }}, nil
} }
// 计算动作数量 // CountAllEnabledNodeClusterFirewallActions 计算动作数量
func (this *NodeClusterFirewallActionService) CountAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.CountAllEnabledNodeClusterFirewallActionsRequest) (*pb.RPCCountResponse, error) { func (this *NodeClusterFirewallActionService) CountAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.CountAllEnabledNodeClusterFirewallActionsRequest) (*pb.RPCCountResponse, error) {
_, err := this.ValidateAdmin(ctx, 0) _, err := this.ValidateAdmin(ctx, 0)
if err != nil { if err != nil {

View File

@@ -18,7 +18,9 @@ import (
"time" "time"
) )
// 命令请求相关 var primaryNodeId int64 = 0
// CommandRequest 命令请求相关
type CommandRequest struct { type CommandRequest struct {
Id int64 Id int64
Code string Code string
@@ -74,6 +76,26 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
return err return err
} }
// 选择一个作为主节点
if primaryNodeId == 0 {
primaryNodeId = nodeId
}
defer func() {
if primaryNodeId == nodeId {
primaryNodeId = 0
nodeLocker.Lock()
if len(nodeRequestChanMap) > 0 {
for anotherNodeId := range nodeRequestChanMap {
primaryNodeId = anotherNodeId
break
}
}
nodeLocker.Unlock()
}
}()
// 返回连接成功 // 返回连接成功
{ {
apiConfig, err := configs.SharedAPIConfig() apiConfig, err := configs.SharedAPIConfig()

View File

@@ -37,6 +37,8 @@ func (this *NodeTaskService) FindNodeTasks(ctx context.Context, req *pb.FindNode
pbTasks = append(pbTasks, &pb.NodeTask{ pbTasks = append(pbTasks, &pb.NodeTask{
Id: int64(task.Id), Id: int64(task.Id),
Type: task.Type, Type: task.Type,
Version: int64(task.Version),
IsPrimary: primaryNodeId == nodeId,
}) })
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns" "github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions" "github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -1639,7 +1640,7 @@ func (this *ServerService) PurgeServerCache(ctx context.Context, req *pb.PurgeSe
} }
var tx = this.NullTx() var tx = this.NullTx()
var cacheMap = maps.Map{} var cacheMap = utils.NewCacheMap()
var purgeResponse = &pb.PurgeServerCacheResponse{} var purgeResponse = &pb.PurgeServerCacheResponse{}
for _, domain := range req.Domains { for _, domain := range req.Domains {

View File

@@ -566,7 +566,7 @@ func (this *ServerStatBoardService) findNodeClusterMetricDataCharts(tx *dbs.Tx,
metricItemIds = append(metricItemIds, itemId) metricItemIds = append(metricItemIds, itemId)
} }
publicMetricItems, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, category) publicMetricItems, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, category, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package utils
import (
"github.com/iwind/TeaGo/maps"
"sync"
)
type CacheMap struct {
locker sync.Mutex
m maps.Map
}
func NewCacheMap() *CacheMap {
return &CacheMap{m: maps.Map{}}
}
func (this *CacheMap) Get(key string) (value interface{}, ok bool) {
this.locker.Lock()
value, ok = this.m[key]
this.locker.Unlock()
return
}
func (this *CacheMap) Put(key string, value interface{}) {
if value == nil {
return
}
this.locker.Lock()
this.m[key] = value
this.locker.Unlock()
}
func (this *CacheMap) Len() int {
this.locker.Lock()
var l = len(this.m)
this.locker.Unlock()
return l
}

View File

@@ -0,0 +1,26 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package utils
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestNewCacheMap(t *testing.T) {
var a = assert.NewAssertion(t)
m := NewCacheMap()
{
m.Put("Hello", "World")
v, ok := m.Get("Hello")
a.IsTrue(ok)
a.IsTrue(v == "World")
}
{
v, ok := m.Get("Hello1")
a.IsFalse(ok)
a.IsTrue(v == nil)
}
}