feat: 优化代码
This commit is contained in:
@@ -124,7 +124,7 @@ func (a *App) Start() error {
|
|||||||
if err := a.keyPoolProvider.LoadKeysFromDB(); err != nil {
|
if err := a.keyPoolProvider.LoadKeysFromDB(); err != nil {
|
||||||
return fmt.Errorf("failed to load keys into key pool: %w", err)
|
return fmt.Errorf("failed to load keys into key pool: %w", err)
|
||||||
}
|
}
|
||||||
logrus.Info("API keys loaded into Redis cache by leader.")
|
logrus.Debug("API keys loaded into Redis cache by leader.")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Info("Follower Mode. Waiting for leader to complete initialization.")
|
logrus.Info("Follower Mode. Waiting for leader to complete initialization.")
|
||||||
@@ -134,11 +134,11 @@ func (a *App) Start() error {
|
|||||||
a.settingsManager.Initialize(a.storage, a.groupManager, a.leaderService)
|
a.settingsManager.Initialize(a.storage, a.groupManager, a.leaderService)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.groupManager.Initialize()
|
|
||||||
|
|
||||||
// 显示配置并启动所有后台服务
|
// 显示配置并启动所有后台服务
|
||||||
a.configManager.DisplayConfig()
|
a.configManager.DisplayConfig()
|
||||||
|
|
||||||
|
a.groupManager.Initialize()
|
||||||
|
|
||||||
a.startRequestLogger()
|
a.startRequestLogger()
|
||||||
a.logCleanupService.Start()
|
a.logCleanupService.Start()
|
||||||
a.keyValidationPool.Start()
|
a.keyValidationPool.Start()
|
||||||
@@ -159,8 +159,6 @@ func (a *App) Start() error {
|
|||||||
go func() {
|
go func() {
|
||||||
logrus.Info("GPT-Load proxy server started successfully")
|
logrus.Info("GPT-Load proxy server started successfully")
|
||||||
logrus.Infof("Server address: http://%s:%d", serverConfig.Host, serverConfig.Port)
|
logrus.Infof("Server address: http://%s:%d", serverConfig.Host, serverConfig.Port)
|
||||||
logrus.Infof("Statistics: http://%s:%d/stats", serverConfig.Host, serverConfig.Port)
|
|
||||||
logrus.Infof("Health check: http://%s:%d/health", serverConfig.Host, serverConfig.Port)
|
|
||||||
logrus.Info("")
|
logrus.Info("")
|
||||||
if err := a.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := a.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
logrus.Fatalf("Server startup failed: %v", err)
|
logrus.Fatalf("Server startup failed: %v", err)
|
||||||
@@ -184,10 +182,10 @@ func (a *App) Stop(ctx context.Context) {
|
|||||||
a.keyValidationPool.Stop()
|
a.keyValidationPool.Stop()
|
||||||
a.leaderService.Stop()
|
a.leaderService.Stop()
|
||||||
a.logCleanupService.Stop()
|
a.logCleanupService.Stop()
|
||||||
|
a.groupManager.Stop()
|
||||||
a.settingsManager.Stop()
|
a.settingsManager.Stop()
|
||||||
|
|
||||||
// Close resources
|
// Close resources
|
||||||
a.proxyServer.Close()
|
|
||||||
a.storage.Close()
|
a.storage.Close()
|
||||||
|
|
||||||
// Wait for the logger to finish writing all logs
|
// Wait for the logger to finish writing all logs
|
||||||
|
@@ -46,7 +46,6 @@ type Manager struct {
|
|||||||
// Config represents the application configuration
|
// Config represents the application configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server types.ServerConfig `json:"server"`
|
Server types.ServerConfig `json:"server"`
|
||||||
OpenAI types.OpenAIConfig `json:"openai"`
|
|
||||||
Auth types.AuthConfig `json:"auth"`
|
Auth types.AuthConfig `json:"auth"`
|
||||||
CORS types.CORSConfig `json:"cors"`
|
CORS types.CORSConfig `json:"cors"`
|
||||||
Performance types.PerformanceConfig `json:"performance"`
|
Performance types.PerformanceConfig `json:"performance"`
|
||||||
@@ -87,14 +86,6 @@ func (m *Manager) ReloadConfig() error {
|
|||||||
IdleTimeout: defaultSettings.ServerIdleTimeout,
|
IdleTimeout: defaultSettings.ServerIdleTimeout,
|
||||||
GracefulShutdownTimeout: defaultSettings.ServerGracefulShutdownTimeout,
|
GracefulShutdownTimeout: defaultSettings.ServerGracefulShutdownTimeout,
|
||||||
},
|
},
|
||||||
OpenAI: types.OpenAIConfig{
|
|
||||||
// BaseURLs will be configured per group
|
|
||||||
BaseURLs: []string{},
|
|
||||||
// Timeout configs now come from system settings
|
|
||||||
RequestTimeout: defaultSettings.RequestTimeout,
|
|
||||||
ResponseTimeout: defaultSettings.ResponseTimeout,
|
|
||||||
IdleConnTimeout: defaultSettings.IdleConnTimeout,
|
|
||||||
},
|
|
||||||
Auth: types.AuthConfig{
|
Auth: types.AuthConfig{
|
||||||
Key: os.Getenv("AUTH_KEY"),
|
Key: os.Getenv("AUTH_KEY"),
|
||||||
Enabled: os.Getenv("AUTH_KEY") != "",
|
Enabled: os.Getenv("AUTH_KEY") != "",
|
||||||
@@ -130,16 +121,9 @@ func (m *Manager) ReloadConfig() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Info("Environment configuration reloaded successfully")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfig returns the raw config struct
|
|
||||||
func (m *Manager) GetConfig() *Config {
|
|
||||||
return m.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAuthConfig returns authentication configuration
|
// GetAuthConfig returns authentication configuration
|
||||||
func (m *Manager) GetAuthConfig() types.AuthConfig {
|
func (m *Manager) GetAuthConfig() types.AuthConfig {
|
||||||
return m.config.Auth
|
return m.config.Auth
|
||||||
@@ -217,7 +201,7 @@ func (m *Manager) DisplayConfig() {
|
|||||||
perfConfig := m.GetPerformanceConfig()
|
perfConfig := m.GetPerformanceConfig()
|
||||||
logConfig := m.GetLogConfig()
|
logConfig := m.GetLogConfig()
|
||||||
|
|
||||||
logrus.Info("Current Configuration:")
|
logrus.Info("Current Server Configuration:")
|
||||||
logrus.Infof(" Server: %s:%d", serverConfig.Host, serverConfig.Port)
|
logrus.Infof(" Server: %s:%d", serverConfig.Host, serverConfig.Port)
|
||||||
|
|
||||||
authStatus := "disabled"
|
authStatus := "disabled"
|
||||||
|
@@ -51,6 +51,5 @@ func NewDB(configManager types.ConfigManager) (*gorm.DB, error) {
|
|||||||
sqlDB.SetMaxOpenConns(100)
|
sqlDB.SetMaxOpenConns(100)
|
||||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||||
|
|
||||||
fmt.Println("Database connection initialized.")
|
|
||||||
return DB, nil
|
return DB, nil
|
||||||
}
|
}
|
||||||
|
@@ -39,7 +39,7 @@ func NewKeyCronService(
|
|||||||
|
|
||||||
// Start begins the cron job execution.
|
// Start begins the cron job execution.
|
||||||
func (s *KeyCronService) Start() {
|
func (s *KeyCronService) Start() {
|
||||||
logrus.Info("Starting KeyCronService...")
|
logrus.Debug("Starting KeyCronService...")
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go s.runLoop()
|
go s.runLoop()
|
||||||
}
|
}
|
||||||
|
@@ -170,7 +170,7 @@ func (s *LeaderService) maintainLeadershipLoop() {
|
|||||||
ticker := time.NewTicker(leaderRenewalInterval)
|
ticker := time.NewTicker(leaderRenewalInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
logrus.Info("Leadership maintenance loop started.")
|
logrus.Debug("Leadership maintenance loop started.")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
@@ -200,7 +200,7 @@ func (s *LeaderService) tryToBeLeader() error {
|
|||||||
return fmt.Errorf("failed to acquire lock: %w", err)
|
return fmt.Errorf("failed to acquire lock: %w", err)
|
||||||
}
|
}
|
||||||
if acquired {
|
if acquired {
|
||||||
logrus.WithField("nodeID", s.nodeID).Info("Successfully acquired leadership.")
|
logrus.WithField("nodeID", s.nodeID).Debug("Successfully acquired leadership.")
|
||||||
s.isLeader.Store(true)
|
s.isLeader.Store(true)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@@ -30,7 +30,7 @@ func NewLogCleanupService(db *gorm.DB, settingsManager *config.SystemSettingsMan
|
|||||||
// Start 启动日志清理服务
|
// Start 启动日志清理服务
|
||||||
func (s *LogCleanupService) Start() {
|
func (s *LogCleanupService) Start() {
|
||||||
go s.run()
|
go s.run()
|
||||||
logrus.Info("Log cleanup service started")
|
logrus.Debug("Log cleanup service started")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop 停止日志清理服务
|
// Stop 停止日志清理服务
|
||||||
|
@@ -23,7 +23,7 @@ func NewStore(cfg types.ConfigManager) (Store, error) {
|
|||||||
return nil, fmt.Errorf("failed to connect to redis: %w", err)
|
return nil, fmt.Errorf("failed to connect to redis: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Info("Successfully connected to Redis.")
|
logrus.Debug("Successfully connected to Redis.")
|
||||||
return NewRedisStore(client), nil
|
return NewRedisStore(client), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user