mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 18:50:27 +08:00
自动同步API节点配置
This commit is contained in:
97
internal/nodes/task_sync_api_nodes.go
Normal file
97
internal/nodes/task_sync_api_nodes.go
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package nodes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/configs"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
events.On(events.EventStart, func() {
|
||||||
|
task := NewSyncAPINodesTask()
|
||||||
|
go task.Start()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// API节点同步任务
|
||||||
|
type SyncAPINodesTask struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSyncAPINodesTask() *SyncAPINodesTask {
|
||||||
|
return &SyncAPINodesTask{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SyncAPINodesTask) Start() {
|
||||||
|
ticker := time.NewTicker(5 * time.Minute)
|
||||||
|
if Tea.IsTesting() {
|
||||||
|
// 快速测试
|
||||||
|
ticker = time.NewTicker(1 * time.Minute)
|
||||||
|
}
|
||||||
|
events.On(events.EventQuit, func() {
|
||||||
|
remotelogs.Println("SYNC_API_NODES_TASK", "quit task")
|
||||||
|
ticker.Stop()
|
||||||
|
})
|
||||||
|
for range ticker.C {
|
||||||
|
err := this.Loop()
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("[TASK][SYNC_API_NODES_TASK]" + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SyncAPINodesTask) Loop() error {
|
||||||
|
// 获取所有可用的节点
|
||||||
|
rpcClient, err := rpc.SharedRPC()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resp, err := rpcClient.APINodeRPC().FindAllEnabledAPINodes(rpcClient.Context(), &pb.FindAllEnabledAPINodesRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newEndpoints := []string{}
|
||||||
|
for _, node := range resp.Nodes {
|
||||||
|
if !node.IsOn {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newEndpoints = append(newEndpoints, node.AccessAddrs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 和现有的对比
|
||||||
|
config, err := configs.LoadAPIConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if this.isSame(newEndpoints, config.RPC.Endpoints) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改RPC对象配置
|
||||||
|
config.RPC.Endpoints = newEndpoints
|
||||||
|
err = rpcClient.UpdateConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存到文件
|
||||||
|
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SyncAPINodesTask) isSame(endpoints1 []string, endpoints2 []string) bool {
|
||||||
|
sort.Strings(endpoints1)
|
||||||
|
sort.Strings(endpoints2)
|
||||||
|
return strings.Join(endpoints1, "&") == strings.Join(endpoints2, "&")
|
||||||
|
}
|
||||||
@@ -158,6 +158,12 @@ func (this *RPCClient) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 修改配置
|
||||||
|
func (this *RPCClient) UpdateConfig(config *configs.APIConfig) error {
|
||||||
|
this.apiConfig = config
|
||||||
|
return this.init()
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
func (this *RPCClient) init() error {
|
func (this *RPCClient) init() error {
|
||||||
// 重新连接
|
// 重新连接
|
||||||
@@ -185,6 +191,8 @@ func (this *RPCClient) init() error {
|
|||||||
if len(conns) == 0 {
|
if len(conns) == 0 {
|
||||||
return errors.New("[RPC]no available endpoints")
|
return errors.New("[RPC]no available endpoints")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 这里不需要加锁,防止和pickConn()冲突
|
||||||
this.conns = conns
|
this.conns = conns
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user