Commit 3df2d5f0 authored by chenweijian's avatar chenweijian Committed by hujiebin

trtc导量

parent 6e504653
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
}
......@@ -7,3 +7,28 @@
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的群组';
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
......@@ -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 {
//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 {
newNum, err := redisCli.IncrNumExpire(numKey, 1, time.Hour*24)
if err == nil && newNum <= dayMaxCovertNum {
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
}
provider = group_e.GroupProvider_TRTC
model.Log.Infof("auto shift trtc room groupId:%v,%+v-%v", groupId, roomUser, 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)
......
......@@ -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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment