Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fb0513703 | ||
|
|
7b96919d80 | ||
|
|
d6c3f542f3 | ||
|
|
1efac70cdb | ||
|
|
01c97c37f7 | ||
|
|
4fd262beae | ||
|
|
239115ead8 | ||
|
|
02b3e275d0 | ||
|
|
9810e55a15 | ||
|
|
03e4ad5db5 |
@@ -26,6 +26,7 @@ var (
|
|||||||
Name = "game_act"
|
Name = "game_act"
|
||||||
ActList = gset.New(true)
|
ActList = gset.New(true)
|
||||||
RunTimeMax *gtime.Time
|
RunTimeMax *gtime.Time
|
||||||
|
TaskMax int64 = 100
|
||||||
)
|
)
|
||||||
|
|
||||||
type sGameAct struct {
|
type sGameAct struct {
|
||||||
@@ -77,7 +78,7 @@ func (s *sGameAct) Info(uid int64, actId int) (data *g.Var, err error) {
|
|||||||
_, err = g.Redis().Set(ctx, keyCache, data)
|
_, err = g.Redis().Set(ctx, keyCache, data)
|
||||||
|
|
||||||
var CacheKey = fmt.Sprintf("act:update:%d", uid)
|
var CacheKey = fmt.Sprintf("act:update:%d", uid)
|
||||||
pkg.Cache("redis").Set(ctx, CacheKey, uid, time.Hour*24*3)
|
pkg.Cache("redis").Set(ctx, CacheKey, uid, time.Hour*24*3+time.Hour*12)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -236,7 +237,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//删除缓存
|
//删除缓存
|
||||||
go s.DelCacheKey(ctx, v.ActId, v.Uid)
|
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||||
|
|
||||||
updateCount++
|
updateCount++
|
||||||
update = make([]*entity.GameAct, 0)
|
update = make([]*entity.GameAct, 0)
|
||||||
@@ -264,10 +265,10 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v2 := range add {
|
//for _, v2 := range add {
|
||||||
//删除缓存
|
// //删除缓存
|
||||||
go s.DelCacheKey(ctx, v2.ActId, v2.Uid)
|
// s.DelCacheKey(ctx, v2.ActId, v2.Uid)
|
||||||
}
|
//}
|
||||||
|
|
||||||
//g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count)
|
//g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count)
|
||||||
add = make([]*entity.GameAct, 0)
|
add = make([]*entity.GameAct, 0)
|
||||||
@@ -311,7 +312,7 @@ func (s *sGameAct) SavesV2() (err error) {
|
|||||||
go func() {
|
go func() {
|
||||||
scanErr := tools.Redis.RedisScanV2("act:*", func(keys []string) error {
|
scanErr := tools.Redis.RedisScanV2("act:*", func(keys []string) error {
|
||||||
if gtime.Now().After(RunTimeMax) {
|
if gtime.Now().After(RunTimeMax) {
|
||||||
return errors.New("Redis扫描超时")
|
return errors.New("redis扫描超时")
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if keyErr := s.SaveV2(ctx, key, addChan, updateChan); keyErr != nil {
|
if keyErr := s.SaveV2(ctx, key, addChan, updateChan); keyErr != nil {
|
||||||
@@ -421,12 +422,18 @@ func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, addChan, updateC
|
|||||||
// @param update []*entity.GameAct: 更新数据
|
// @param update []*entity.GameAct: 更新数据
|
||||||
// @return err error: 返回错误信息
|
// @return err error: 返回错误信息
|
||||||
func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) {
|
func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) {
|
||||||
|
tx, err := g.DB().Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Error(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//批量写入数据库
|
//批量写入数据库
|
||||||
updateCount := 0
|
var updateCount int64
|
||||||
if len(update) > 0 {
|
if len(update) > 0 {
|
||||||
for _, v := range update {
|
for _, v := range update {
|
||||||
v.UpdatedAt = gtime.Now()
|
v.UpdatedAt = gtime.Now()
|
||||||
updateRes, err2 := g.Model(Name).Where(do.GameAct{
|
updateRes, err2 := tx.Model(Name).Where(do.GameAct{
|
||||||
Uid: v.Uid,
|
Uid: v.Uid,
|
||||||
ActId: v.ActId,
|
ActId: v.ActId,
|
||||||
}).Data(v).Update()
|
}).Data(v).Update()
|
||||||
@@ -438,18 +445,30 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
|
|||||||
g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateCount++
|
||||||
|
if updateCount > TaskMax {
|
||||||
|
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Debugf(ctx, "act当前更新数据库失败:%v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
updateCount = 0
|
||||||
|
tx, err = g.DB().Begin(ctx)
|
||||||
|
}
|
||||||
//删除缓存
|
//删除缓存
|
||||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||||
updateCount++
|
|
||||||
}
|
}
|
||||||
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
//循环结束了,最后写入一波
|
||||||
|
g.Log().Debugf(ctx, "Cache2Sql运行结束,act当前更新数据库: %v 条", updateCount)
|
||||||
update = (update)[:0]
|
update = (update)[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
var addCount int64
|
var addCount int64
|
||||||
if len(add) > 0 {
|
if len(add) > 0 {
|
||||||
for _, v := range add {
|
for _, v := range add {
|
||||||
addRes, err2 := g.Model(Name).Data(v).Insert()
|
addRes, err2 := tx.Model(Name).Data(v).Insert()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
g.Log().Error(ctx, err2)
|
g.Log().Error(ctx, err2)
|
||||||
continue
|
continue
|
||||||
@@ -459,12 +478,26 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addCount++
|
addCount++
|
||||||
|
if addCount > TaskMax {
|
||||||
|
g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount)
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
addCount = 0
|
||||||
|
tx, err = g.DB().Begin(ctx)
|
||||||
|
}
|
||||||
//删除缓存
|
//删除缓存
|
||||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||||
}
|
}
|
||||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
|
||||||
|
//循环结束了,最后写入一波
|
||||||
|
g.Log().Debugf(ctx, "Cache2Sql运行结束,act当前写入数据库: %v 条", addCount)
|
||||||
add = (add)[:0]
|
add = (add)[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,9 +510,17 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
//通道关闭标志
|
//通道关闭标志
|
||||||
addClosed := false
|
addClosed := false
|
||||||
updateClosed := false
|
updateClosed := false
|
||||||
|
//写入总数
|
||||||
|
var addAllCount, updateAllCount int64
|
||||||
|
|
||||||
|
tx, err := g.DB().Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Error(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 使用链式安全模式
|
// 使用链式安全模式
|
||||||
var db = g.Model(Name).Safe()
|
//var db = tx.Model(Name).Safe()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
//检查是否两个通道都已关闭且为空
|
//检查是否两个通道都已关闭且为空
|
||||||
@@ -493,7 +534,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
addClosed = true // 仅标记关闭,不立即日志
|
addClosed = true // 仅标记关闭,不立即日志
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addRes, err2 := db.Data(v).Insert()
|
addRes, err2 := tx.Model(Name).Data(v).Insert()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
g.Log().Error(ctx, err2)
|
g.Log().Error(ctx, err2)
|
||||||
continue
|
continue
|
||||||
@@ -504,6 +545,20 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
}
|
}
|
||||||
row, _ := addRes.RowsAffected()
|
row, _ := addRes.RowsAffected()
|
||||||
addCount += row
|
addCount += row
|
||||||
|
|
||||||
|
if addCount > TaskMax {
|
||||||
|
// g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount)
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//清空数量前累加一下
|
||||||
|
addAllCount += addCount
|
||||||
|
addCount = 0
|
||||||
|
tx, err = g.DB().Begin(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
//删除缓存
|
//删除缓存
|
||||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||||
|
|
||||||
@@ -513,7 +568,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
v.UpdatedAt = gtime.Now()
|
v.UpdatedAt = gtime.Now()
|
||||||
updateRes, err2 := db.Where(do.GameAct{
|
updateRes, err2 := tx.Model(Name).Where(do.GameAct{
|
||||||
Uid: v.Uid,
|
Uid: v.Uid,
|
||||||
ActId: v.ActId,
|
ActId: v.ActId,
|
||||||
}).Data(v).Update()
|
}).Data(v).Update()
|
||||||
@@ -525,9 +580,23 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
//g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
//g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
updateCount++
|
||||||
|
|
||||||
|
if updateCount > TaskMax {
|
||||||
|
// g.Log().Debugf(ctx, "超过%v条,act当前更新数据库: %v 条", TaskMax, updateCount)
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Debugf(ctx, "act当前更新数据库失败:%v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//清空数量前累加一下
|
||||||
|
updateAllCount += updateCount
|
||||||
|
updateCount = 0
|
||||||
|
tx, err = g.DB().Begin(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
//删除缓存
|
//删除缓存
|
||||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||||
updateCount++
|
|
||||||
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
g.Log().Debug(ctx, "act协程被上下文取消")
|
g.Log().Debug(ctx, "act协程被上下文取消")
|
||||||
@@ -535,25 +604,30 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
addAllCount += addCount
|
||||||
|
updateAllCount += updateCount
|
||||||
// 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志)
|
// 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志)
|
||||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
g.Log().Debugf(ctx, "运行结束act当前写入数据库: %v 条", addAllCount)
|
||||||
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
g.Log().Debugf(ctx, "运行结束act当前更新数据库: %v 条", updateAllCount)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除缓存key
|
// 删除缓存key
|
||||||
func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) {
|
func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) {
|
||||||
//如果有活跃,跳过删除
|
go func() {
|
||||||
if getBool, _ := pkg.Cache("redis").
|
//如果有活跃,跳过删除
|
||||||
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
|
if getBool, _ := pkg.Cache("redis").
|
||||||
return
|
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
|
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
|
||||||
_, err := g.Redis().Del(ctx, cacheKey)
|
_, err := g.Redis().Del(ctx, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
g.Log().Error(ctx, err)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清空GetRedDot缓存
|
// 清空GetRedDot缓存
|
||||||
|
|||||||
@@ -100,9 +100,6 @@ func safeProperty(property map[string]any) {
|
|||||||
if _, ok := v.(string); ok {
|
if _, ok := v.(string); ok {
|
||||||
property[k] = onlyWordRE.ReplaceAllString(gconv.String(v), "*")
|
property[k] = onlyWordRE.ReplaceAllString(gconv.String(v), "*")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
property[k] = safePropertyRE.ReplaceAllString(gconv.String(v), "*")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, delkey := range delkeys {
|
for _, delkey := range delkeys {
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ func TestGamelog(t *testing.T) {
|
|||||||
// 必填
|
// 必填
|
||||||
Pid: "test5", // 项目ID
|
Pid: "test5", // 项目ID
|
||||||
// BaseUrl: "http://47.76.178.47:10101", // 香港测试服上报地址
|
// BaseUrl: "http://47.76.178.47:10101", // 香港测试服上报地址
|
||||||
BaseUrl: "http://101.37.28.111:10101", // 香港测试服上报地址
|
// BaseUrl: "http://101.37.28.111:10101", // 香港测试服上报地址
|
||||||
|
BaseUrl: "http://47.77.200.131:10101", // 美国BIDB服务器
|
||||||
// BaseUrl: "http://127.0.0.1:10101", // 本次测试上报地址
|
// BaseUrl: "http://127.0.0.1:10101", // 本次测试上报地址
|
||||||
ReportSk: "sngame2025", // xor混淆key
|
ReportSk: "sngame2025", // xor混淆key
|
||||||
FlushInterval: 5, // 上报间隔
|
FlushInterval: 5, // 上报间隔
|
||||||
@@ -52,3 +53,53 @@ func TestGamelog(t *testing.T) {
|
|||||||
glsdk.Shutdown()
|
glsdk.Shutdown()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPressMQ(t *testing.T) {
|
||||||
|
glsdk, err := gamelog.INIT(&gamelog.SDKConfig{
|
||||||
|
// 必填
|
||||||
|
Pid: "yotest", // 项目ID
|
||||||
|
BaseUrl: "http://47.77.200.131:10101", // 美国BIDB服务器
|
||||||
|
ReportSk: "sngame2025", // xor混淆key
|
||||||
|
FlushInterval: 6, // 上报间隔
|
||||||
|
DiskBakPath: "gamelog", // 本地磁盘备份, 用于意外情况下临时保存日志, 请确保该目录持久化(容器内要挂载). 每次启动时或每N次上报时加载到失败队列
|
||||||
|
// 可填
|
||||||
|
RetryN: 2, // 默认每10次, 上传检查一次磁盘的失败数据
|
||||||
|
ChanSize: 500, // 默认1000, 信道size
|
||||||
|
SendSaveType: 2, // 发送存储类型, 默认不设置为0代表文件存储, 2代表走kafka可实同步日志
|
||||||
|
})
|
||||||
|
|
||||||
|
// 随机测试事件和属性
|
||||||
|
events := []string{"e1", "e2", "e3", "e4"}
|
||||||
|
pms := []map[string]any{
|
||||||
|
{"a": "1"},
|
||||||
|
{"a": "2"},
|
||||||
|
{"a": "3"},
|
||||||
|
{"a": "4"},
|
||||||
|
}
|
||||||
|
uuids := []string{}
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
uuidval, _ := uuid.NewUUID()
|
||||||
|
randUid := strings.ReplaceAll(uuidval.String(), "-", "")
|
||||||
|
uuids = append(uuids, randUid)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
n := 0
|
||||||
|
const limit = 30000
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
glsdk.LogLtz(uuids[grand.Intn(len(uuids))], events[grand.Intn(len(events))], pms[grand.Intn(len(pms))])
|
||||||
|
// 并发控制
|
||||||
|
n++
|
||||||
|
if n%limit == 0 {
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
time.Sleep(time.Second * 120)
|
||||||
|
// 模拟等待信号后优雅关闭
|
||||||
|
glsdk.Shutdown()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user