From 4db4fa6adfd9d70edcf792d2f61876a00c7412bc Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Mon, 23 Dec 2024 00:14:30 +0800 Subject: [PATCH] fix: monitor --- service/aiproxy/controller/relay.go | 37 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/service/aiproxy/controller/relay.go b/service/aiproxy/controller/relay.go index 41e5a5199db..bcab8b90df1 100644 --- a/service/aiproxy/controller/relay.go +++ b/service/aiproxy/controller/relay.go @@ -41,12 +41,31 @@ func relayHelper(meta *meta.Meta, c *gin.Context) *model.ErrorWithStatusCode { } } -func RelayHelper(meta *meta.Meta, c *gin.Context) *model.ErrorWithStatusCode { +func RelayHelper(meta *meta.Meta, c *gin.Context) (*model.ErrorWithStatusCode, bool) { err := relayHelper(meta, c) - if err := monitor.AddRequest(c.Request.Context(), meta.OriginModelName, int64(meta.Channel.ID), err != nil); err != nil { - log.Errorf("add request failed: %+v", err) + if err == nil { + if err := monitor.AddRequest( + c.Request.Context(), + meta.OriginModelName, + int64(meta.Channel.ID), + false, + ); err != nil { + log.Errorf("add request failed: %+v", err) + } + return nil, false + } + if shouldRetry(c, err.StatusCode) { + if err := monitor.AddRequest( + c.Request.Context(), + meta.OriginModelName, + int64(meta.Channel.ID), + true, + ); err != nil { + log.Errorf("add request failed: %+v", err) + } + return err, true } - return err + return nil, false } func getChannelWithFallback(model string, failedChannelIDs ...int) (*dbmodel.Channel, error) { @@ -88,14 +107,14 @@ func Relay(c *gin.Context) { } meta := middleware.NewMetaByContext(c, channel) - bizErr := RelayHelper(meta, c) + bizErr, retry := RelayHelper(meta, c) if bizErr == nil { return } failedChannelIDs = append(failedChannelIDs, channel.ID) requestID := c.GetString(ctxkey.RequestID) var retryTimes int64 - if shouldRetry(c, bizErr.StatusCode) { + if retry { retryTimes = config.GetRetryTimes() } for i := retryTimes; i > 0; i-- { @@ -117,10 +136,13 @@ func Relay(c *gin.Context) { } c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) meta.Reset(newChannel) - bizErr = RelayHelper(meta, c) + bizErr, retry = RelayHelper(meta, c) if bizErr == nil { return } + if !retry { + break + } failedChannelIDs = append(failedChannelIDs, newChannel.ID) } if bizErr != nil { @@ -139,6 +161,7 @@ func Relay(c *gin.Context) { } } +// 仅当是channel错误时,才需要重试,用户请求参数错误时,不需要重试 func shouldRetry(_ *gin.Context, statusCode int) bool { if statusCode == http.StatusTooManyRequests || statusCode == http.StatusGatewayTimeout ||