fix: 修复playground优先级失效
This commit is contained in:
@@ -58,19 +58,6 @@ func Playground(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
//c.Set("token_name", "playground-"+group)
|
|
||||||
tempToken := &model.Token{
|
|
||||||
UserId: userId,
|
|
||||||
Name: fmt.Sprintf("playground-%s", group),
|
|
||||||
Group: group,
|
|
||||||
}
|
|
||||||
_ = middleware.SetupContextForToken(c, tempToken)
|
|
||||||
_, newAPIError = getChannel(c, group, playgroundRequest.Model, 1)
|
|
||||||
if newAPIError != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//middleware.SetupContextForSelectedChannel(c, channel, playgroundRequest.Model)
|
|
||||||
common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
|
|
||||||
|
|
||||||
// Write user context to ensure acceptUnsetRatio is available
|
// Write user context to ensure acceptUnsetRatio is available
|
||||||
userCache, err := model.GetUserCache(userId)
|
userCache, err := model.GetUserCache(userId)
|
||||||
@@ -79,5 +66,19 @@ func Playground(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
userCache.WriteContext(c)
|
userCache.WriteContext(c)
|
||||||
|
|
||||||
|
tempToken := &model.Token{
|
||||||
|
UserId: userId,
|
||||||
|
Name: fmt.Sprintf("playground-%s", group),
|
||||||
|
Group: group,
|
||||||
|
}
|
||||||
|
_ = middleware.SetupContextForToken(c, tempToken)
|
||||||
|
_, newAPIError = getChannel(c, group, playgroundRequest.Model, 0)
|
||||||
|
if newAPIError != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//middleware.SetupContextForSelectedChannel(c, channel, playgroundRequest.Model)
|
||||||
|
common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
|
||||||
|
|
||||||
Relay(c)
|
Relay(c)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,16 +124,16 @@ func authHelper(c *gin.Context, minRole int) {
|
|||||||
c.Set("group", session.Get("group"))
|
c.Set("group", session.Get("group"))
|
||||||
c.Set("use_access_token", useAccessToken)
|
c.Set("use_access_token", useAccessToken)
|
||||||
|
|
||||||
userCache, err := model.GetUserCache(id.(int))
|
//userCache, err := model.GetUserCache(id.(int))
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
// c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
// "success": false,
|
||||||
"message": err.Error(),
|
// "message": err.Error(),
|
||||||
})
|
// })
|
||||||
c.Abort()
|
// c.Abort()
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
userCache.WriteContext(c)
|
//userCache.WriteContext(c)
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,26 +87,29 @@ func getPriority(group string, model string, retry int) (int, error) {
|
|||||||
return priorityToUse, nil
|
return priorityToUse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getChannelQuery(group string, model string, retry int) *gorm.DB {
|
func getChannelQuery(group string, model string, retry int) (*gorm.DB, error) {
|
||||||
maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true)
|
maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true)
|
||||||
channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = (?)", group, model, true, maxPrioritySubQuery)
|
channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = (?)", group, model, true, maxPrioritySubQuery)
|
||||||
if retry != 0 {
|
if retry != 0 {
|
||||||
priority, err := getPriority(group, model, retry)
|
priority, err := getPriority(group, model, retry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error()))
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
channelQuery = DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = ?", group, model, true, priority)
|
channelQuery = DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = ?", group, model, true, priority)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return channelQuery
|
return channelQuery, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, error) {
|
func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, error) {
|
||||||
var abilities []Ability
|
var abilities []Ability
|
||||||
|
|
||||||
var err error = nil
|
var err error = nil
|
||||||
channelQuery := getChannelQuery(group, model, retry)
|
channelQuery, err := getChannelQuery(group, model, retry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if common.UsingSQLite || common.UsingPostgreSQL {
|
if common.UsingSQLite || common.UsingPostgreSQL {
|
||||||
err = channelQuery.Order("weight DESC").Find(&abilities).Error
|
err = channelQuery.Order("weight DESC").Find(&abilities).Error
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user