From 272d36add5ce91ef674d1050e5a4133ddf37d40c Mon Sep 17 00:00:00 2001 From: tbphp Date: Tue, 10 Jun 2025 11:09:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/proxy/server.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/proxy/server.go b/internal/proxy/server.go index abdcc9c..baccb54 100644 --- a/internal/proxy/server.go +++ b/internal/proxy/server.go @@ -4,6 +4,7 @@ package proxy import ( "bytes" "context" + "encoding/json" "fmt" "io" "net/http" @@ -148,7 +149,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti // Check retry limit if retryCount >= keysConfig.MaxRetries { - logrus.Errorf("Max retries exceeded (%d)", retryCount) + logrus.Debugf("Max retries exceeded (%d)", retryCount) // Return detailed error information errorResponse := gin.H{ @@ -288,8 +289,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti responseTime := time.Since(startTime) // Check if HTTP status code requires retry - // 429 (Too Many Requests) and 5xx server errors need retry - if resp.StatusCode == 429 || resp.StatusCode >= 500 { + if resp.StatusCode >= 400 { // Log failure if retryCount > 0 { logrus.Debugf("Retry request returned error %d (attempt %d) (response time: %v)", resp.StatusCode, retryCount+1, responseTime) @@ -305,6 +305,18 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti errorMessage = fmt.Sprintf("HTTP %d", resp.StatusCode) } + var jsonError struct { + Error struct { + Message string `json:"message"` + } `json:"error"` + } + + if err := json.Unmarshal([]byte(errorMessage), &jsonError); err == nil && jsonError.Error.Message != "" { + logrus.Errorf("Error message: %s", jsonError.Error.Message) + } else { + logrus.Errorf("Error message: %s", errorMessage) + } + // Record failure asynchronously go ps.keyManager.RecordFailure(keyInfo.Key, fmt.Errorf("HTTP %d", resp.StatusCode)) @@ -329,7 +341,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti // Log final success result if retryCount > 0 { - logrus.Infof("Request succeeded after %d retries (response time: %v)", retryCount, responseTime) + logrus.Debugf("Request succeeded after %d retries (response time: %v)", retryCount, responseTime) } else { logrus.Debugf("Request succeeded on first attempt (response time: %v)", responseTime) }