优化代码

This commit is contained in:
刘祥超
2022-09-23 09:28:19 +08:00
parent c568ad3e9a
commit 5655f89ba6
20 changed files with 90 additions and 58 deletions

View File

@@ -433,17 +433,6 @@ func (this *DNSDomainService) convertDomainToPB(tx *dbs.Tx, domain *dns.DNSDomai
}, nil
}
// 转换域名记录信息
func (this *DNSDomainService) convertRecordToPB(record *dnstypes.Record) *pb.DNSRecord {
return &pb.DNSRecord{
Id: record.Id,
Name: record.Name,
Value: record.Value,
Type: record.Type,
Route: record.Route,
}
}
// 检查集群节点变化
func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster, records []*dnstypes.Record, domainName string, defaultRoute string) (result []maps.Map, doneNodeRecords []*dnstypes.Record, doneServerRecords []*dnstypes.Record, countAllNodes int64, countAllServers int64, nodesChanged bool, serversChanged bool, err error) {
var clusterId = int64(cluster.Id)

View File

@@ -3,21 +3,21 @@
package services
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestHTTPCacheTaskService_CountHTTPCacheTasks(t *testing.T) {
func TestHTTPCacheTaskService_ParseDomain(t *testing.T) {
var a = assert.NewAssertion(t)
var service = &HTTPCacheTaskService{}
a.IsTrue(service.parseDomain("aaa") == "aaa")
a.IsTrue(service.parseDomain("AAA") == "aaa")
a.IsTrue(service.parseDomain("a.b-c.com") == "a.b-c.com")
a.IsTrue(service.parseDomain("a.b-c.com/hello/world") == "a.b-c.com")
a.IsTrue(service.parseDomain("https://a.b-c.com") == "a.b-c.com")
a.IsTrue(service.parseDomain("http://a.b-c.com/hello/world") == "a.b-c.com")
a.IsTrue(service.parseDomain("http://a.B-c.com/hello/world") == "a.b-c.com")
a.IsTrue(service.parseDomain("http:/aaaa.com") == "http")
a.IsTrue(service.parseDomain("北京") == "")
a.IsTrue(utils.ParseDomainFromKey("aaa") == "aaa")
a.IsTrue(utils.ParseDomainFromKey("AAA") == "aaa")
a.IsTrue(utils.ParseDomainFromKey("a.b-c.com") == "a.b-c.com")
a.IsTrue(utils.ParseDomainFromKey("a.b-c.com/hello/world") == "a.b-c.com")
a.IsTrue(utils.ParseDomainFromKey("https://a.b-c.com") == "a.b-c.com")
a.IsTrue(utils.ParseDomainFromKey("http://a.b-c.com/hello/world") == "a.b-c.com")
a.IsTrue(utils.ParseDomainFromKey("http://a.B-c.com/hello/world") == "a.b-c.com")
a.IsTrue(utils.ParseDomainFromKey("http:/aaaa.com") == "http")
a.IsTrue(utils.ParseDomainFromKey("北京") == "")
}

View File

@@ -25,6 +25,8 @@ func init() {
goman.New(func() {
// 将队列导入数据库
var countKeys = 0
var useTx = true
for key := range metricStatKeysQueue {
err := func(key string) error {
metricStatsLocker.Lock()
@@ -43,18 +45,31 @@ func init() {
var itemId = types.Int64(pieces[3])
// 删除旧的数据
tx, err := models.SharedMetricStatDAO.Instance.Begin()
if err != nil {
return err
}
var tx *dbs.Tx
var err error
if useTx {
var before = time.Now()
defer func() {
// 失败时不需要rollback
commitErr := tx.Commit()
if commitErr != nil {
remotelogs.Error("METRIC_STAT", "commit metric stats failed: "+commitErr.Error())
tx, err = models.SharedMetricStatDAO.Instance.Begin()
if err != nil {
return err
}
}()
defer func() {
// 失败时不需要rollback
if tx != nil {
commitErr := tx.Commit()
if commitErr != nil {
remotelogs.Error("METRIC_STAT", "commit metric stats failed: "+commitErr.Error())
}
}
// 如果运行时间过长,则不使用事务
if time.Since(before) > 1*time.Second {
useTx = false
}
}()
}
err = models.SharedMetricStatDAO.DeleteNodeItemStats(tx, nodeId, serverId, itemId, req.Time)
if err != nil {

View File

@@ -213,6 +213,7 @@ func (this *ServerService) UpdateServerGroupIds(ctx context.Context, req *pb.Upd
}
// 检查分组IDs
var serverGroupIds = []int64{}
for _, groupId := range req.ServerGroupIds {
if userId > 0 {
err = models.SharedServerGroupDAO.CheckUserGroup(tx, userId, groupId)
@@ -228,18 +229,19 @@ func (this *ServerService) UpdateServerGroupIds(ctx context.Context, req *pb.Upd
continue
}
}
serverGroupIds = append(serverGroupIds, groupId)
}
// 增加默认分组
if userId > 0 {
config, err := models.SharedSysSettingDAO.ReadUserServerConfig(tx)
if err == nil && config.GroupId > 0 && !lists.ContainsInt64(req.ServerGroupIds, config.GroupId) {
req.ServerGroupIds = append(req.ServerGroupIds, config.GroupId)
if err == nil && config.GroupId > 0 && !lists.ContainsInt64(serverGroupIds, config.GroupId) {
serverGroupIds = append(serverGroupIds, config.GroupId)
}
}
// 修改
err = models.SharedServerDAO.UpdateServerGroupIds(tx, req.ServerId, req.ServerGroupIds)
err = models.SharedServerDAO.UpdateServerGroupIds(tx, req.ServerId, serverGroupIds)
if err != nil {
return nil, err
}

View File

@@ -20,6 +20,7 @@ var serverBandwidthStatsLocker = &sync.Mutex{}
func init() {
var ticker = time.NewTicker(1 * time.Minute)
var useTx = true
dbs.OnReadyDone(func() {
goman.New(func() {
@@ -30,15 +31,32 @@ func init() {
serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{}
serverBandwidthStatsLocker.Unlock()
tx, err := models.SharedServerBandwidthStatDAO.Instance.Begin()
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "begin transaction failed: "+err.Error())
return
}
var tx *dbs.Tx
var err error
defer func() {
_ = tx.Commit()
}()
if useTx {
var before = time.Now()
tx, err = models.SharedServerBandwidthStatDAO.Instance.Begin()
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "begin transaction failed: "+err.Error())
return
}
defer func() {
if tx != nil {
commitErr := tx.Commit()
if commitErr != nil {
remotelogs.Error("METRIC_STAT", "commit bandwidth stats failed: "+commitErr.Error())
}
}
// 如果运行时间过长,则不使用事务
if time.Since(before) > 1*time.Second {
useTx = false
}
}()
}
for _, stat := range m {
// 更新服务的带宽峰值