mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-04 21:50:28 +08:00
自动更新API节点配置
This commit is contained in:
@@ -342,6 +342,12 @@ func (this *RPCClient) APIContext(apiNodeId int64) context.Context {
|
|||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 修改配置
|
||||||
|
func (this *RPCClient) UpdateConfig(config *configs.APIConfig) error {
|
||||||
|
this.apiConfig = config
|
||||||
|
return this.init()
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
func (this *RPCClient) init() error {
|
func (this *RPCClient) init() error {
|
||||||
// 重新连接
|
// 重新连接
|
||||||
@@ -369,7 +375,9 @@ 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")
|
||||||
}
|
}
|
||||||
|
this.locker.Lock()
|
||||||
this.conns = conns
|
this.conns = conns
|
||||||
|
this.locker.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
92
internal/tasks/task_sync_api_nodes.go
Normal file
92
internal/tasks/task_sync_api_nodes.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package tasks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/events"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"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)
|
||||||
|
}
|
||||||
|
for range ticker.C {
|
||||||
|
err := this.Loop()
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("[TASK][SYNC_API_NODES]" + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SyncAPINodesTask) Loop() error {
|
||||||
|
// 获取所有可用的节点
|
||||||
|
rpcClient, err := rpc.SharedRPC()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resp, err := rpcClient.APINodeRPC().FindAllEnabledAPINodes(rpcClient.Context(0), &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, "&")
|
||||||
|
}
|
||||||
15
internal/tasks/task_sync_api_nodes_test.go
Normal file
15
internal/tasks/task_sync_api_nodes_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package tasks
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSyncAPINodesTask_Loop(t *testing.T) {
|
||||||
|
task := NewSyncAPINodesTask()
|
||||||
|
err := task.Loop()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user