更新配置

This commit is contained in:
Hua
2025-02-19 08:46:26 +08:00
parent e0b133ea1e
commit 7ec5abe67f
3 changed files with 21 additions and 5 deletions

View File

@ -18,6 +18,7 @@ var (
mu sync.RWMutex mu sync.RWMutex
aiBalancer *AIBalancer aiBalancer *AIBalancer
balancerMu sync.RWMutex
) )
type Config struct { type Config struct {
@ -332,7 +333,16 @@ func (b *AIBalancer) Next() (*AIConfig, error) {
return &highPriorityAIs[0], nil return &highPriorityAIs[0], nil
} }
// 添加获取 AIBalancer 的方法 // ResetAIBalancer 重置 AI 负载均衡器
func ResetAIBalancer(ais []AIConfig) {
balancerMu.Lock()
defer balancerMu.Unlock()
aiBalancer = NewAIBalancer(ais)
}
// GetAIBalancer 获取当前的 AI 负载均衡器
func GetAIBalancer() *AIBalancer { func GetAIBalancer() *AIBalancer {
balancerMu.RLock()
defer balancerMu.RUnlock()
return aiBalancer return aiBalancer
} }

View File

@ -135,8 +135,13 @@ func (h *ConfigHandler) UpdateConfig(c *gin.Context) {
return return
} }
log.Printf("配置更新成功") // 更新运行时配置
h.cfg = &newConfig h.cfg = &newConfig
// 重新初始化 AI 负载均衡器
config.ResetAIBalancer(newConfig.AIs)
log.Printf("配置更新成功")
c.JSON(http.StatusOK, gin.H{"message": "配置更新成功"}) c.JSON(http.StatusOK, gin.H{"message": "配置更新成功"})
} }

View File

@ -6,8 +6,9 @@ import (
"code-review/services" "code-review/services"
"code-review/services/ai" "code-review/services/ai"
"fmt" "fmt"
"github.com/gin-gonic/gin"
"log" "log"
"github.com/gin-gonic/gin"
) )
func main() { func main() {
@ -22,10 +23,10 @@ func main() {
} }
// 初始化 AI 负载均衡器 // 初始化 AI 负载均衡器
aiBalancer := config.NewAIBalancer(cfg.AIs) config.ResetAIBalancer(cfg.AIs)
// 获取一个 AI 配置 // 获取一个 AI 配置
aiConfig, err := aiBalancer.Next() aiConfig, err := config.GetAIBalancer().Next()
if err != nil { if err != nil {
log.Fatalf("获取 AI 配置失败: %v", err) log.Fatalf("获取 AI 配置失败: %v", err)
} }