Refactor API handlers to implement retry mechanism with configurable limits and improved error handling
docker-image / docker (push) Has been cancelled
goreleaser / goreleaser (push) Has been cancelled

- Introduced retry counter with a configurable ` RequestRetry ` limit in all handlers.
- Enhanced error handling with specific HTTP status codes for switching clients.
- Standardized response forwarding for non-retriable errors.
- Improved logging for quota and client switch scenarios.
This commit is contained in:
Luis Pater
2025-08-25 23:27:30 +08:00
parent 8c555c4e69
commit 9102ff031d
11 changed files with 148 additions and 43 deletions
+13 -3
View File
@@ -37,6 +37,9 @@ type Server struct {
// cfg holds the current server configuration.
cfg *config.Config
// requestLogger is the request logger instance for dynamic configuration updates.
requestLogger *logging.FileRequestLogger
}
// NewServer creates and initializes a new API server instance.
@@ -69,9 +72,10 @@ func NewServer(cfg *config.Config, cliClients []interfaces.Client) *Server {
// Create server instance
s := &Server{
engine: engine,
handlers: handlers.NewBaseAPIHandlers(cliClients, cfg),
cfg: cfg,
engine: engine,
handlers: handlers.NewBaseAPIHandlers(cliClients, cfg),
cfg: cfg,
requestLogger: requestLogger,
}
// Setup routes
@@ -189,6 +193,12 @@ func corsMiddleware() gin.HandlerFunc {
// - clients: The new slice of AI service clients
// - cfg: The new application configuration
func (s *Server) UpdateClients(clients []interfaces.Client, cfg *config.Config) {
// Update request logger enabled state if it has changed
if s.requestLogger != nil && s.cfg.RequestLog != cfg.RequestLog {
s.requestLogger.SetEnabled(cfg.RequestLog)
log.Debugf("request logging updated from %t to %t", s.cfg.RequestLog, cfg.RequestLog)
}
s.cfg = cfg
s.handlers.UpdateClients(clients, cfg)
log.Infof("server clients and configuration updated: %d clients", len(clients))