diff --git a/domain/model/group_m/group_im.go b/domain/model/group_m/group_im.go new file mode 100644 index 0000000000000000000000000000000000000000..8b9ecdc86d2f179795a99e23576950e8a36119ab --- /dev/null +++ b/domain/model/group_m/group_im.go @@ -0,0 +1,68 @@ +package group_m + +import ( + "git.hilo.cn/hilo-common/domain" + "hilo-group/_const/enum/group_e" + "hilo-group/myerr" + "math/rand" + "time" +) + +type GroupIm struct { + Id uint64 + Provider group_e.GroupProvider + Prob int + StartIdx int `gorm:"-"'` + EndIdx int `gorm:"-"` +} + +type GroupImLog struct { + ImGroupId string + Provider group_e.GroupProvider +} + +func GetGroupImConf(model *domain.Model) ([]*GroupIm, error) { + rows := make([]*GroupIm, 0) + if err := model.Db.Table("group_im").Find(&rows).Error; err != nil { + return nil, myerr.WrapErr(err) + } + return rows, nil +} + +func GetRandomImProvider(model *domain.Model) group_e.GroupProvider { + var res group_e.GroupProvider + confList, err := GetGroupImConf(model) + if err != nil { + model.Log.Errorf("GetRandomImProvider err:%v", err) + return res + } + conf := GetRandomGroupImConf(confList) + return conf.Provider +} + +func GetRandomGroupImConf(confList []*GroupIm) *GroupIm { + var res *GroupIm + var max int + for i, v := range confList { + if v.Prob > 0 { + confList[i].StartIdx = max + max += v.Prob + confList[i].EndIdx = max + } + } + rand.Seed(time.Now().UnixNano()) + randNum := rand.Intn(max) + for _, v := range confList { + if v.StartIdx <= randNum && randNum < v.EndIdx { + res = v + } + } + return res +} + +func CreateGroupImLog(model *domain.Model, imGroupId string, provider group_e.GroupProvider) error { + if err := model.Db.Model(GroupImLog{}).Create(GroupImLog{ImGroupId: imGroupId, Provider: provider}).Error; err != nil { + return myerr.WrapErr(err) + } + return nil +} diff --git a/mysql/trtc.sql b/mysql/trtc.sql index fdc3fe807e5a9d78cfb9258bf597ce0f9b204cda..17757155749f733c6d68ec12d4f4b42df0b8a84b 100644 --- a/mysql/trtc.sql +++ b/mysql/trtc.sql @@ -6,4 +6,29 @@ PRIMARY KEY (`id`), UNIQUE KEY `im_group_id` (`im_group_id`), KEY `created_time` (`created_time`) USING BTREE - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='使用trtc的群组'; \ No newline at end of file + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='使用trtc的群组'; + +CREATE TABLE `group_im` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + `provider` int NOT NULL COMMENT '厂商类型0.声网1.腾讯trtc', + `prob` bigint unsigned NOT NULL COMMENT '概率', + `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `created_time` (`created_time`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='房间im厂商选用概率配置'; + +INSERT INTO hilo.group_im (`provider`, `prob`) +VALUES (0, 1000), + (1, 1000); + +CREATE TABLE `group_im_log` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + `im_group_id` char(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `provider` int NOT NULL COMMENT '厂商类型0.声网1.腾讯trtc', + `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `im_group_id_idx` (`im_group_id`) USING BTREE, + KEY `created_time_idx` (`created_time`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='房间im提供商切换log'; \ No newline at end of file diff --git a/route/group_r/group_op.go b/route/group_r/group_op.go index c601f465ce0dcfe8366c503eedfe50a26d7470d6..285adcacd9bc9e652b634657ae08cef2c0baefd8 100644 --- a/route/group_r/group_op.go +++ b/route/group_r/group_op.go @@ -2,7 +2,6 @@ package group_r import ( "encoding/json" - "git.hilo.cn/hilo-common/_const/rediskey" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/mylogrus" @@ -1775,26 +1774,50 @@ func GroupIn(c *gin.Context) (*mycontext.MyContext, error) { // } // } //} - } else if time.Now().Unix() > 1684810309 && time.Now().Unix() < 1684857600 { + } else { // 增加trtc房间 - dayMaxCovertNum := int64(600) - numKey := rediskey.GetConvertToTRTCNum() - covertNum, err := redisCli.GetCacheInt64(numKey) - if err == nil && covertNum <= dayMaxCovertNum { - //if false && time.Now().Unix() > 1682499704 { - roomUser, err := group_m.GetRoomOnlineUser(myContext, groupId) - if err == nil && roomUser.Total == 0 { - newNum, err := redisCli.IncrNumExpire(numKey, 1, time.Hour*24) - if err == nil && newNum <= dayMaxCovertNum { - if err := group_m.InitTRTC(model, groupId); err != nil { - return myContext, err - } - provider = group_e.GroupProvider_TRTC - model.Log.Infof("auto shift trtc room groupId:%v,%+v-%v", groupId, roomUser, err) + //dayMaxCovertNum := int64(600) + //numKey := rediskey.GetConvertToTRTCNum() + //covertNum, err := redisCli.GetCacheInt64(numKey) + //if time.Now().Unix() > 1684810309 && time.Now().Unix() < 1684857600 && err == nil && covertNum <= dayMaxCovertNum { + // roomUser, err := group_m.GetRoomOnlineUser(myContext, groupId) + // if err == nil && roomUser.Total == 0 { + // newNum, err := redisCli.IncrNumExpire(numKey, 1, time.Hour*24) + // if err == nil && newNum <= dayMaxCovertNum { + // if err := group_m.InitTRTC(model, groupId); err != nil { + // return myContext, err + // } + // provider = group_e.GroupProvider_TRTC + // model.Log.Infof("auto shift trtc room groupId:%v,%+v-%v", groupId, roomUser, err) + // } + // } + //} + } + // 进房时判断要用哪个im + targetProvider := group_m.GetRandomImProvider(model) + if provider != targetProvider { + roomUser, err := group_m.GetRoomOnlineUser(myContext, groupId) + if err == nil && roomUser.Total == 0 { // 房间没人才做切换 + switch provider { + case group_e.GroupProvider_SW: // 旧的是声网,切换成trtc + if err := group_m.InitTRTC(model, groupId); err != nil { + return myContext, err + } + case group_e.GroupProvider_TRTC: // 旧的是trtc,切换成声网 + if err := group_m.DeleteTRTC(model, groupId); err != nil { + return myContext, err } } + // 切换 + provider = targetProvider + // log + err = group_m.CreateGroupImLog(model, groupId, provider) + if err != nil { + model.Log.Errorf("CreateGroupImLog err:%v, imGroupId:%s, provider:%v", err, groupId, provider) + } } } + if provider == group_e.GroupProvider_TRTC { // 版本升级提示,旧版本(3.6.0以下),提示升级 _, major, minor, _, err := req.GetAppVersion(c) diff --git a/test/group_power_test.go b/test/group_power_test.go index 54c80d0df956ebe0180fa5d4daf5f59a4f2d0cbb..70b19f2cabc0c47fe06539c8b743a7fcf967bf75 100644 --- a/test/group_power_test.go +++ b/test/group_power_test.go @@ -49,3 +49,23 @@ func TestDoCalcGroupPowerMonthRankActbufa(t *testing.T) { beginDate, endDate := _now.BeginningOfMonth().Format("2006-01-02"), _now.EndOfMonth().Format("2006-01-02") _ = group_power_s.BufaDoCalcGroupPowerMonthRankAct(domain.CreateModelNil(), calcMonth, beginDate, endDate, starDate) } + +func TestGetRandomImProvider(t *testing.T) { + var times0, times1 int + model := domain.CreateModelNil() + confList, err := group_m.GetGroupImConf(model) + if err != nil { + model.Log.Errorf("GetRandomImProvider err:%v", err) + return + } + for i := 0; i < 1000000; i++ { + conf := group_m.GetRandomGroupImConf(confList) + if conf.Provider == 0 { + times0 += 1 + } else if conf.Provider == 1 { + times1 += 1 + } + } + fmt.Printf("true--------------times0:%v\n", times0) + fmt.Printf("true--------------times1:%v\n", times1) +}