Skip to content

Commit

Permalink
fix: getChannelWithFallback
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Dec 20, 2024
1 parent 674a6ca commit 6fd6677
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions service/aiproxy/controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ func RelayHelper(meta *meta.Meta, c *gin.Context) *model.ErrorWithStatusCode {
return err
}

func getChannelWithFallback(model string, failedChannelIDs ...int) (*dbmodel.Channel, error) {
channel, err := dbmodel.CacheGetRandomSatisfiedChannel(model, failedChannelIDs...)
if err == nil {
return channel, nil
}
if !errors.Is(err, dbmodel.ErrChannelsExhausted) {
return nil, err
}
return dbmodel.CacheGetRandomSatisfiedChannel(model)
}

func Relay(c *gin.Context) {
log := middleware.GetLogger(c)

Expand All @@ -64,7 +75,7 @@ func Relay(c *gin.Context) {
failedChannelIDs = append(failedChannelIDs, int(id))
}

channel, err := dbmodel.CacheGetRandomSatisfiedChannel(requestModel, failedChannelIDs...)
channel, err := getChannelWithFallback(requestModel, failedChannelIDs...)
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": &model.Error{
Expand Down Expand Up @@ -94,7 +105,6 @@ func Relay(c *gin.Context) {
break
}
if !errors.Is(err, dbmodel.ErrChannelsExhausted) {
log.Errorf("get random satisfied channel failed: %+v", err)
break
}
newChannel = channel
Expand Down

0 comments on commit 6fd6677

Please sign in to comment.