Преглед изворни кода

fix: fix priority not updated & random choice not working

JustSong пре 2 година
родитељ
комит
a5647b1ea7
3 измењених фајлова са 19 додато и 6 уклоњено
  1. 1 1
      model/ability.go
  2. 10 4
      model/cache.go
  3. 8 1
      model/channel.go

+ 1 - 1
model/ability.go

@@ -10,7 +10,7 @@ type Ability struct {
 	Model     string `json:"model" gorm:"primaryKey;autoIncrement:false"`
 	ChannelId int    `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
 	Enabled   bool   `json:"enabled"`
-	Priority  int64  `json:"priority" gorm:"bigint;default:0"`
+	Priority  *int64 `json:"priority" gorm:"bigint;default:0"`
 }
 
 func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {

+ 10 - 4
model/cache.go

@@ -165,7 +165,7 @@ func InitChannelCache() {
 	for group, model2channels := range newGroup2model2channels {
 		for model, channels := range model2channels {
 			sort.Slice(channels, func(i, j int) bool {
-				return channels[i].Priority > channels[j].Priority
+				return channels[i].GetPriority() > channels[j].GetPriority()
 			})
 			newGroup2model2channels[group][model] = channels
 		}
@@ -195,11 +195,17 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
 	if len(channels) == 0 {
 		return nil, errors.New("channel not found")
 	}
+	endIdx := len(channels)
 	// choose by priority
 	firstChannel := channels[0]
-	if firstChannel.Priority > 0 {
-		return firstChannel, nil
+	if firstChannel.GetPriority() > 0 {
+		for i := range channels {
+			if channels[i].GetPriority() != firstChannel.GetPriority() {
+				endIdx = i
+				break
+			}
+		}
 	}
-	idx := rand.Intn(len(channels))
+	idx := rand.Intn(endIdx)
 	return channels[idx], nil
 }

+ 8 - 1
model/channel.go

@@ -23,7 +23,7 @@ type Channel struct {
 	Group              string  `json:"group" gorm:"type:varchar(32);default:'default'"`
 	UsedQuota          int64   `json:"used_quota" gorm:"bigint;default:0"`
 	ModelMapping       string  `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
-	Priority           int64   `json:"priority" gorm:"bigint;default:0"`
+	Priority           *int64  `json:"priority" gorm:"bigint;default:0"`
 }
 
 func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {
@@ -79,6 +79,13 @@ func BatchInsertChannels(channels []Channel) error {
 	return nil
 }
 
+func (channel *Channel) GetPriority() int64 {
+	if channel == nil {
+		return 0
+	}
+	return *channel.Priority
+}
+
 func (channel *Channel) Insert() error {
 	var err error
 	err = DB.Create(channel).Error