mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	集群增加自动同步时钟选项
This commit is contained in:
		@@ -187,7 +187,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateCluster 修改集群
 | 
			
		||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool) error {
 | 
			
		||||
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig) error {
 | 
			
		||||
	if clusterId <= 0 {
 | 
			
		||||
		return errors.New("invalid clusterId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -204,6 +204,14 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
 | 
			
		||||
	op.NodeMaxThreads = nodeMaxThreads
 | 
			
		||||
	op.AutoOpenPorts = autoOpenPorts
 | 
			
		||||
 | 
			
		||||
	if clockConfig != nil {
 | 
			
		||||
		clockJSON, err := json.Marshal(clockConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		op.Clock = clockJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := this.Save(tx, op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -926,7 +934,7 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
 | 
			
		||||
	cluster, err := this.Query(tx).
 | 
			
		||||
		Pk(clusterId).
 | 
			
		||||
		State(NodeClusterStateEnabled).
 | 
			
		||||
		Result("id", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection").
 | 
			
		||||
		Result("id", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock").
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil || cluster == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
@@ -30,45 +30,47 @@ type NodeCluster struct {
 | 
			
		||||
	SystemServices       dbs.JSON `field:"systemServices"`       // 系统服务设置
 | 
			
		||||
	TimeZone             string   `field:"timeZone"`             // 时区
 | 
			
		||||
	NodeMaxThreads       uint32   `field:"nodeMaxThreads"`       // 节点最大线程数
 | 
			
		||||
	DdosProtection       dbs.JSON `field:"ddosProtection"`       // DDOS端口
 | 
			
		||||
	DdosProtection       dbs.JSON `field:"ddosProtection"`       // DDoS防护设置
 | 
			
		||||
	AutoOpenPorts        uint8    `field:"autoOpenPorts"`        // 是否自动尝试开放端口
 | 
			
		||||
	IsPinned             bool     `field:"isPinned"`             // 是否置顶
 | 
			
		||||
	Webp                 dbs.JSON `field:"webp"`                 // WebP设置
 | 
			
		||||
	Uam                  dbs.JSON `field:"uam"`                  // UAM设置
 | 
			
		||||
	Clock                dbs.JSON `field:"clock"`                // 时钟配置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type NodeClusterOperator struct {
 | 
			
		||||
	Id                   interface{} // ID
 | 
			
		||||
	AdminId              interface{} // 管理员ID
 | 
			
		||||
	UserId               interface{} // 用户ID
 | 
			
		||||
	IsOn                 interface{} // 是否启用
 | 
			
		||||
	Name                 interface{} // 名称
 | 
			
		||||
	UseAllAPINodes       interface{} // 是否使用所有API节点
 | 
			
		||||
	ApiNodes             interface{} // 使用的API节点
 | 
			
		||||
	InstallDir           interface{} // 安装目录
 | 
			
		||||
	Order                interface{} // 排序
 | 
			
		||||
	CreatedAt            interface{} // 创建时间
 | 
			
		||||
	GrantId              interface{} // 默认认证方式
 | 
			
		||||
	State                interface{} // 状态
 | 
			
		||||
	AutoRegister         interface{} // 是否开启自动注册
 | 
			
		||||
	UniqueId             interface{} // 唯一ID
 | 
			
		||||
	Secret               interface{} // 密钥
 | 
			
		||||
	HealthCheck          interface{} // 健康检查
 | 
			
		||||
	DnsName              interface{} // DNS名称
 | 
			
		||||
	DnsDomainId          interface{} // 域名ID
 | 
			
		||||
	Dns                  interface{} // DNS配置
 | 
			
		||||
	Toa                  interface{} // TOA配置
 | 
			
		||||
	CachePolicyId        interface{} // 缓存策略ID
 | 
			
		||||
	HttpFirewallPolicyId interface{} // WAF策略ID
 | 
			
		||||
	AccessLog            interface{} // 访问日志设置
 | 
			
		||||
	SystemServices       interface{} // 系统服务设置
 | 
			
		||||
	TimeZone             interface{} // 时区
 | 
			
		||||
	NodeMaxThreads       interface{} // 节点最大线程数
 | 
			
		||||
	DdosProtection       interface{} // DDOS端口
 | 
			
		||||
	AutoOpenPorts        interface{} // 是否自动尝试开放端口
 | 
			
		||||
	IsPinned             interface{} // 是否置顶
 | 
			
		||||
	Webp                 interface{} // WebP设置
 | 
			
		||||
	Uam                  interface{} // UAM设置
 | 
			
		||||
	Id                   any // ID
 | 
			
		||||
	AdminId              any // 管理员ID
 | 
			
		||||
	UserId               any // 用户ID
 | 
			
		||||
	IsOn                 any // 是否启用
 | 
			
		||||
	Name                 any // 名称
 | 
			
		||||
	UseAllAPINodes       any // 是否使用所有API节点
 | 
			
		||||
	ApiNodes             any // 使用的API节点
 | 
			
		||||
	InstallDir           any // 安装目录
 | 
			
		||||
	Order                any // 排序
 | 
			
		||||
	CreatedAt            any // 创建时间
 | 
			
		||||
	GrantId              any // 默认认证方式
 | 
			
		||||
	State                any // 状态
 | 
			
		||||
	AutoRegister         any // 是否开启自动注册
 | 
			
		||||
	UniqueId             any // 唯一ID
 | 
			
		||||
	Secret               any // 密钥
 | 
			
		||||
	HealthCheck          any // 健康检查
 | 
			
		||||
	DnsName              any // DNS名称
 | 
			
		||||
	DnsDomainId          any // 域名ID
 | 
			
		||||
	Dns                  any // DNS配置
 | 
			
		||||
	Toa                  any // TOA配置
 | 
			
		||||
	CachePolicyId        any // 缓存策略ID
 | 
			
		||||
	HttpFirewallPolicyId any // WAF策略ID
 | 
			
		||||
	AccessLog            any // 访问日志设置
 | 
			
		||||
	SystemServices       any // 系统服务设置
 | 
			
		||||
	TimeZone             any // 时区
 | 
			
		||||
	NodeMaxThreads       any // 节点最大线程数
 | 
			
		||||
	DdosProtection       any // DDoS防护设置
 | 
			
		||||
	AutoOpenPorts        any // 是否自动尝试开放端口
 | 
			
		||||
	IsPinned             any // 是否置顶
 | 
			
		||||
	Webp                 any // WebP设置
 | 
			
		||||
	Uam                  any // UAM设置
 | 
			
		||||
	Clock                any // 时钟配置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewNodeClusterOperator() *NodeClusterOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,9 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -50,3 +52,15 @@ func (this *NodeCluster) HasDDoSProtection() bool {
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DecodeClock 解析时钟配置
 | 
			
		||||
func (this *NodeCluster) DecodeClock() *nodeconfigs.ClockConfig {
 | 
			
		||||
	var clock = nodeconfigs.DefaultClockConfig()
 | 
			
		||||
	if IsNotNull(this.Clock) {
 | 
			
		||||
		err := json.Unmarshal(this.Clock, clock)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			remotelogs.Error("NodeCluster.DecodeClock()", err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return clock
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1085,6 +1085,11 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 时钟
 | 
			
		||||
		if config.Clock == nil {
 | 
			
		||||
			config.Clock = nodeCluster.DecodeClock()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 最大线程数、TCP连接数
 | 
			
		||||
		if clusterIndex == 0 {
 | 
			
		||||
			config.MaxThreads = int(nodeCluster.NodeMaxThreads)
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,21 @@ func (this *NodeClusterService) UpdateNodeCluster(ctx context.Context, req *pb.U
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.AutoOpenPorts)
 | 
			
		||||
	// validate clock
 | 
			
		||||
	var clockConfig = nodeconfigs.DefaultClockConfig()
 | 
			
		||||
	if len(req.ClockJSON) > 0 {
 | 
			
		||||
		err = json.Unmarshal(req.ClockJSON, clockConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.New("decode clock failed: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = clockConfig.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, errors.New("validate clock failed: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.AutoOpenPorts, clockConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -169,6 +183,7 @@ func (this *NodeClusterService) FindEnabledNodeCluster(ctx context.Context, req
 | 
			
		||||
		TimeZone:             cluster.TimeZone,
 | 
			
		||||
		NodeMaxThreads:       int32(cluster.NodeMaxThreads),
 | 
			
		||||
		AutoOpenPorts:        cluster.AutoOpenPorts == 1,
 | 
			
		||||
		ClockJSON:            cluster.Clock,
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user