Merge pull request #363 from dalefengs/main

fix: http code is not properly disabled channel
This commit is contained in:
Calcium-Ion 2024-07-14 15:35:27 +08:00 committed by GitHub
commit 5e936b3923
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 17 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func testChannel(channel *model.Channel, testModel string) (err error, openaiErr *dto.OpenAIError) { func testChannel(channel *model.Channel, testModel string) (err error, openAIErrorWithStatusCode *dto.OpenAIErrorWithStatusCode) {
tik := time.Now() tik := time.Now()
if channel.Type == common.ChannelTypeMidjourney { if channel.Type == common.ChannelTypeMidjourney {
return errors.New("midjourney channel test is not supported"), nil return errors.New("midjourney channel test is not supported"), nil
@ -58,8 +58,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openaiErr
modelMap := make(map[string]string) modelMap := make(map[string]string)
err := json.Unmarshal([]byte(modelMapping), &modelMap) err := json.Unmarshal([]byte(modelMapping), &modelMap)
if err != nil { if err != nil {
openaiErr := service.OpenAIErrorWrapperLocal(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError).Error return err, service.OpenAIErrorWrapperLocal(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
return err, &openaiErr
} }
if modelMap[testModel] != "" { if modelMap[testModel] != "" {
testModel = modelMap[testModel] testModel = modelMap[testModel]
@ -104,11 +103,11 @@ func testChannel(channel *model.Channel, testModel string) (err error, openaiErr
} }
if resp != nil && resp.StatusCode != http.StatusOK { if resp != nil && resp.StatusCode != http.StatusOK {
err := relaycommon.RelayErrorHandler(resp) err := relaycommon.RelayErrorHandler(resp)
return fmt.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), &err.Error return fmt.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), err
} }
usage, respErr := adaptor.DoResponse(c, resp, meta) usage, respErr := adaptor.DoResponse(c, resp, meta)
if respErr != nil { if respErr != nil {
return fmt.Errorf("%s", respErr.Error.Message), &respErr.Error return fmt.Errorf("%s", respErr.Error.Message), respErr
} }
if usage == nil { if usage == nil {
return errors.New("usage is nil"), nil return errors.New("usage is nil"), nil
@ -222,7 +221,7 @@ func testAllChannels(notify bool) error {
for _, channel := range channels { for _, channel := range channels {
isChannelEnabled := channel.Status == common.ChannelStatusEnabled isChannelEnabled := channel.Status == common.ChannelStatusEnabled
tik := time.Now() tik := time.Now()
err, openaiErr := testChannel(channel, "") err, openaiWithStatusErr := testChannel(channel, "")
tok := time.Now() tok := time.Now()
milliseconds := tok.Sub(tik).Milliseconds() milliseconds := tok.Sub(tik).Milliseconds()
@ -233,14 +232,10 @@ func testAllChannels(notify bool) error {
} }
// request error disables the channel // request error disables the channel
if openaiErr != nil { if openaiWithStatusErr != nil {
err = errors.New(fmt.Sprintf("type %s, code %v, message %s", openaiErr.Type, openaiErr.Code, openaiErr.Message)) oaiErr := openaiWithStatusErr.Error
openAiErrWithStatus := dto.OpenAIErrorWithStatusCode{ err = errors.New(fmt.Sprintf("type %s, httpCode %d, code %v, message %s", oaiErr.Type, openaiWithStatusErr.StatusCode, oaiErr.Code, oaiErr.Message))
StatusCode: -1, ban = service.ShouldDisableChannel(channel.Type, openaiWithStatusErr)
Error: *openaiErr,
LocalError: false,
}
ban = service.ShouldDisableChannel(channel.Type, &openAiErrWithStatus)
} }
// parse *int to bool // parse *int to bool
@ -254,7 +249,7 @@ func testAllChannels(notify bool) error {
} }
// enable channel // enable channel
if !isChannelEnabled && service.ShouldEnableChannel(err, openaiErr, channel.Status) { if !isChannelEnabled && service.ShouldEnableChannel(err, openaiWithStatusErr, channel.Status) {
service.EnableChannel(channel.Id, channel.Name) service.EnableChannel(channel.Id, channel.Name)
} }

View File

@ -74,14 +74,14 @@ func ShouldDisableChannel(channelType int, err *relaymodel.OpenAIErrorWithStatus
return false return false
} }
func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError, status int) bool { func ShouldEnableChannel(err error, openaiWithStatusErr *relaymodel.OpenAIErrorWithStatusCode, status int) bool {
if !common.AutomaticEnableChannelEnabled { if !common.AutomaticEnableChannelEnabled {
return false return false
} }
if err != nil { if err != nil {
return false return false
} }
if openAIErr != nil { if openaiWithStatusErr != nil {
return false return false
} }
if status != common.ChannelStatusAutoDisabled { if status != common.ChannelStatusAutoDisabled {