diff --git a/middleware/distributor.go b/middleware/distributor.go index e2b8f2a..74754f4 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -101,10 +101,12 @@ func Distribute() func(c *gin.Context) { channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model) if err != nil { message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model) + // 如果错误,但是渠道不为空,说明是数据库一致性问题 if channel != nil { common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id)) message = "数据库一致性已被破坏,请联系管理员" } + // 如果错误,而且渠道为空,说明是没有可用渠道 abortWithMessage(c, http.StatusServiceUnavailable, message) return } diff --git a/model/ability.go b/model/ability.go index 7a2cc39..c7875da 100644 --- a/model/ability.go +++ b/model/ability.go @@ -159,5 +159,6 @@ func FixAbility() (int, error) { count++ } } + InitChannelCache() return count, nil } diff --git a/model/cache.go b/model/cache.go index a134ec0..5c9be39 100644 --- a/model/cache.go +++ b/model/cache.go @@ -258,8 +258,8 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error return channel, nil } } - // return the last channel if no channel is found - return channels[endIdx-1], nil + // return null if no channel is not found + return nil, errors.New("channel not found") } func CacheGetChannel(id int) (*Channel, error) {