v1.5.1 增强程序稳定性

This commit is contained in:
robin
2026-03-22 17:37:40 +08:00
parent afbaaa869c
commit 17e182b413
652 changed files with 22949 additions and 34397 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/iwind/TeaGo/types"
"strings"
"sync"
"sync/atomic"
"time"
)
@@ -22,11 +23,13 @@ type DomainManager struct {
namesMap map[string]map[int64]*models.NSDomain // domain name => { domainId => domain }
clusterId int64
db *dbs.DB
version int64
locker *sync.RWMutex
db *dbs.DB
version int64
locker *sync.RWMutex
dbWriteFailures atomic.Int64 // DB 写入累计失败次数
notifier chan bool
readyCh chan struct{} // 初始加载完成后关闭
}
// NewDomainManager 获取域名管理器对象
@@ -38,6 +41,7 @@ func NewDomainManager(db *dbs.DB, clusterId int64) *DomainManager {
clusterId: clusterId,
notifier: make(chan bool, 8),
locker: &sync.RWMutex{},
readyCh: make(chan struct{}),
}
}
@@ -65,6 +69,9 @@ func (this *DomainManager) Start() {
}
}
// 通知初始加载完成
close(this.readyCh)
// 更新
var ticker = time.NewTicker(20 * time.Second)
for {
@@ -244,7 +251,8 @@ func (this *DomainManager) processDomain(domain *pb.NSDomain) {
if this.db != nil {
err := this.db.DeleteDomain(domain.Id)
if err != nil {
remotelogs.Error("DOMAIN_MANAGER", "delete domain from db failed: "+err.Error())
count := this.dbWriteFailures.Add(1)
remotelogs.Error("DOMAIN_MANAGER", "delete domain from db failed (total failures: "+types.String(count)+"): "+err.Error())
}
}
@@ -255,17 +263,20 @@ func (this *DomainManager) processDomain(domain *pb.NSDomain) {
if this.db != nil {
exists, err := this.db.ExistsDomain(domain.Id)
if err != nil {
remotelogs.Error("DOMAIN_MANAGER", "query failed: "+err.Error())
count := this.dbWriteFailures.Add(1)
remotelogs.Error("DOMAIN_MANAGER", "query failed (total failures: "+types.String(count)+"): "+err.Error())
} else {
if exists {
err = this.db.UpdateDomain(domain.Id, domain.NsCluster.Id, domain.UserId, domain.Name, domain.TsigJSON, domain.Version)
if err != nil {
remotelogs.Error("DOMAIN_MANAGER", "update failed: "+err.Error())
count := this.dbWriteFailures.Add(1)
remotelogs.Error("DOMAIN_MANAGER", "update failed (total failures: "+types.String(count)+"): "+err.Error())
}
} else {
err = this.db.InsertDomain(domain.Id, domain.NsCluster.Id, domain.UserId, domain.Name, domain.TsigJSON, domain.Version)
if err != nil {
remotelogs.Error("DOMAIN_MANAGER", "insert failed: "+err.Error())
count := this.dbWriteFailures.Add(1)
remotelogs.Error("DOMAIN_MANAGER", "insert failed (total failures: "+types.String(count)+"): "+err.Error())
}
}
}