节点可以单独设置所使用的API节点地址

This commit is contained in:
刘祥超
2022-11-21 19:55:01 +08:00
parent 991e08fa71
commit d6f311e057
6 changed files with 144 additions and 0 deletions

View File

@@ -961,6 +961,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
Level: types.Int32(node.Level),
GroupId: int64(node.GroupId),
EnableIPLists: node.EnableIPLists,
APINodeAddrs: node.DecodeAPINodeAddrs(),
}
// API节点IP
@@ -2038,6 +2039,46 @@ func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProt
return nil
}
// FindNodeAPIConfig 查找API相关配置信息
func (this *NodeDAO) FindNodeAPIConfig(tx *dbs.Tx, nodeId int64) (*Node, error) {
if nodeId <= 0 {
return nil, nil
}
one, err := this.Query(tx).
Pk(nodeId).
Result("apiNodeAddrs").
Find()
if err != nil || one == nil {
return nil, err
}
return one.(*Node), nil
}
// UpdateNodeAPIConfig 修改API相关配置信息
func (this *NodeDAO) UpdateNodeAPIConfig(tx *dbs.Tx, nodeId int64, apiNodeAddrs []*serverconfigs.NetworkAddressConfig) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
if apiNodeAddrs == nil {
apiNodeAddrs = []*serverconfigs.NetworkAddressConfig{}
}
apiNodeAddrsJSON, err := json.Marshal(apiNodeAddrs)
if err != nil {
return err
}
var op = NewNodeOperator()
op.Id = nodeId
op.ApiNodeAddrs = apiNodeAddrsJSON
err = this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, nodeId)
}
// NotifyUpdate 通知节点相关更新
func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
// 这里只需要通知单个集群即可,因为节点是公用的,更新一个就相当于更新了所有

View File

@@ -42,6 +42,7 @@ type Node struct {
CacheDiskSubDirs dbs.JSON `field:"cacheDiskSubDirs"` // 其他缓存目录
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
EnableIPLists bool `field:"enableIPLists"` // 启用IP名单
ApiNodeAddrs dbs.JSON `field:"apiNodeAddrs"` // API节点地址
}
type NodeOperator struct {
@@ -83,6 +84,7 @@ type NodeOperator struct {
CacheDiskSubDirs any // 其他缓存目录
DnsResolver any // DNS解析器
EnableIPLists any // 启用IP名单
ApiNodeAddrs any // API节点地址
}
func NewNodeOperator() *NodeOperator {

View File

@@ -137,6 +137,7 @@ func (this *Node) HasDDoSProtection() bool {
return false
}
// DecodeMaxCacheDiskCapacity 解析硬盘容量
func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
if this.MaxCacheDiskCapacity.IsNull() {
return nil
@@ -147,6 +148,7 @@ func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
return capacity
}
// DecodeMaxCacheMemoryCapacity 解析内存容量
func (this *Node) DecodeMaxCacheMemoryCapacity() *shared.SizeCapacity {
if this.MaxCacheMemoryCapacity.IsNull() {
return nil
@@ -171,6 +173,7 @@ func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
return resolverConfig
}
// DecodeLnAddrs 解析Ln地址
func (this *Node) DecodeLnAddrs() []string {
if IsNull(this.LnAddrs) {
return nil
@@ -184,6 +187,7 @@ func (this *Node) DecodeLnAddrs() []string {
return result
}
// DecodeCacheDiskSubDirs 解析缓存目录
func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
if IsNull(this.CacheDiskSubDirs) {
return nil
@@ -196,3 +200,17 @@ func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
}
return result
}
// DecodeAPINodeAddrs 解析API节点地址
func (this *Node) DecodeAPINodeAddrs() []*serverconfigs.NetworkAddressConfig {
var result = []*serverconfigs.NetworkAddressConfig{}
if IsNull(this.ApiNodeAddrs) {
return result
}
err := json.Unmarshal(this.ApiNodeAddrs, &result)
if err != nil {
remotelogs.Error("Node.DecodeAPINodeAddrs", err.Error())
}
return result
}