From d6769c0175afb9ef5d304a156e71c7d22262b432 Mon Sep 17 00:00:00 2001 From: Ghostz Date: Wed, 22 May 2024 12:27:03 +0800 Subject: [PATCH 1/3] add userid when relay error --- controller/relay.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/controller/relay.go b/controller/relay.go index aba4cd94..179ce6de 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -4,6 +4,9 @@ import ( "bytes" "context" "fmt" + "io" + "net/http" + "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/config" @@ -16,8 +19,6 @@ import ( "github.com/songquanpeng/one-api/relay/controller" "github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/relaymode" - "io" - "net/http" ) // https://platform.openai.com/docs/api-reference/chat @@ -47,6 +48,7 @@ func Relay(c *gin.Context) { logger.Debugf(ctx, "request body: %s", string(requestBody)) } channelId := c.GetInt(ctxkey.ChannelId) + userId := c.GetInt("id") bizErr := relayHelper(c, relayMode) if bizErr == nil { monitor.Emit(channelId, true) @@ -56,7 +58,7 @@ func Relay(c *gin.Context) { channelName := c.GetString(ctxkey.ChannelName) group := c.GetString(ctxkey.Group) originalModel := c.GetString(ctxkey.OriginalModel) - go processChannelRelayError(ctx, channelId, channelName, bizErr) + go processChannelRelayError(ctx, userId, channelId, channelName, bizErr) requestId := c.GetString(helper.RequestIdKey) retryTimes := config.RetryTimes if !shouldRetry(c, bizErr.StatusCode) { @@ -83,7 +85,7 @@ func Relay(c *gin.Context) { channelId := c.GetInt(ctxkey.ChannelId) lastFailedChannelId = channelId channelName := c.GetString(ctxkey.ChannelName) - go processChannelRelayError(ctx, channelId, channelName, bizErr) + go processChannelRelayError(ctx, userId, channelId, channelName, bizErr) } if bizErr != nil { if bizErr.StatusCode == http.StatusTooManyRequests { @@ -115,8 +117,8 @@ func shouldRetry(c *gin.Context, statusCode int) bool { return true } -func processChannelRelayError(ctx context.Context, channelId int, channelName string, err *model.ErrorWithStatusCode) { - logger.Errorf(ctx, "relay error (channel #%d): %s", channelId, err.Message) +func processChannelRelayError(ctx context.Context, userId int, channelId int, channelName string, err *model.ErrorWithStatusCode) { + logger.Errorf(ctx, "relay error (userId #%d channel #%d): %s", userId, channelId, err.Message) // https://platform.openai.com/docs/guides/error-codes/api-errors if monitor.ShouldDisableChannel(&err.Error, err.StatusCode) { monitor.DisableChannel(channelId, channelName, err.Message) From 391759dc16378d1b40379d74bc5bca31c9b68bda Mon Sep 17 00:00:00 2001 From: JustSong Date: Tue, 28 May 2024 01:29:57 +0800 Subject: [PATCH 2/3] chore: update log format --- controller/relay.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/relay.go b/controller/relay.go index 179ce6de..5d8ac690 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -118,7 +118,7 @@ func shouldRetry(c *gin.Context, statusCode int) bool { } func processChannelRelayError(ctx context.Context, userId int, channelId int, channelName string, err *model.ErrorWithStatusCode) { - logger.Errorf(ctx, "relay error (userId #%d channel #%d): %s", userId, channelId, err.Message) + logger.Errorf(ctx, "relay error (channel id %d, user id: %d): %s", channelId, userId, err.Message) // https://platform.openai.com/docs/guides/error-codes/api-errors if monitor.ShouldDisableChannel(&err.Error, err.StatusCode) { monitor.DisableChannel(channelId, channelName, err.Message) From 15737cd0cf1275a56f52dbf1e5f4ea2d65eb5c52 Mon Sep 17 00:00:00 2001 From: Ghostz Date: Fri, 21 Jun 2024 11:40:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?disabled=20channel=20when=20return=20?= =?UTF-8?q?=E6=97=A0=E5=8F=AF=E7=94=A8=E6=B8=A0=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/distributor.go | 7 ++++--- monitor/manage.go | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/middleware/distributor.go b/middleware/distributor.go index 0c4b04c3..32f3df5c 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -2,13 +2,14 @@ package middleware import ( "fmt" + "net/http" + "strconv" + "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/model" "github.com/songquanpeng/one-api/relay/channeltype" - "net/http" - "strconv" ) type ModelRequest struct { @@ -48,7 +49,7 @@ func Distribute() func(c *gin.Context) { logger.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id)) message = "数据库一致性已被破坏,请联系管理员" } - abortWithMessage(c, http.StatusServiceUnavailable, message) + abortWithMessage(c, http.StatusNotFound, message) return } } diff --git a/monitor/manage.go b/monitor/manage.go index 946e78af..d65fe3ce 100644 --- a/monitor/manage.go +++ b/monitor/manage.go @@ -1,10 +1,11 @@ package monitor import ( - "github.com/songquanpeng/one-api/common/config" - "github.com/songquanpeng/one-api/relay/model" "net/http" "strings" + + "github.com/songquanpeng/one-api/common/config" + "github.com/songquanpeng/one-api/relay/model" ) func ShouldDisableChannel(err *model.Error, statusCode int) bool { @@ -35,6 +36,8 @@ func ShouldDisableChannel(err *model.Error, statusCode int) bool { return true } else if strings.HasPrefix(err.Message, "This organization has been disabled.") { return true + } else if strings.HasPrefix(err.Message, "无可用渠道") { + return true } //if strings.Contains(err.Message, "quota") { // return true