From 1a95ce8c4874e8b05826b18a8ea1bfcbf30de235 Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Mon, 26 Jan 2026 14:12:12 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E7=A7=BB=E9=99=A4=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96=E5=92=8C=E5=86=97=E4=BD=99=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/cool/cool.go | 23 +++++++++--------- .../concurrent_swiss_map.go | 5 ++-- logic/main.go | 3 +-- logic/server.go | 3 +++ logic/service/fight/action.go | 24 +++++++++++-------- logic/service/fight/loop.go | 19 +++++++++++++++ 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/common/cool/cool.go b/common/cool/cool.go index c5d9502c3..d5e1d8904 100644 --- a/common/cool/cool.go +++ b/common/cool/cool.go @@ -6,7 +6,6 @@ import ( "github.com/gogf/gf/v2/database/gredis" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/i18n/gi18n" - "github.com/gogf/gf/v2/os/gbuild" "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gtime" @@ -19,9 +18,9 @@ var ( CacheEPS = gcache.New() // 定义全局缓存对象 供EPS使用 CacheManager = gcache.New() // 定义全局缓存对象 供其他业务使用 ProcessFlag = guid.S() // 定义全局进程标识 - RunMode = "dev" // 定义全局运行模式 - IsRedisMode = false // 定义全局是否为redis模式 - I18n = gi18n.New() // 定义全局国际化对象 + // RunMode = "dev" // 定义全局运行模式 + IsRedisMode = false // 定义全局是否为redis模式 + I18n = gi18n.New() // 定义全局国际化对象 ) func init() { @@ -34,13 +33,13 @@ func init() { redisConfig = &gredis.Config{} ) Logger.Debug(ctx, "module cool init start ...", gtime.Now()) - buildData := gbuild.Data() - if _, ok := buildData["mode"]; ok { - RunMode = buildData["mode"].(string) - } - if RunMode == "cool-tools" { - return - } + // buildData := gbuild.Data() + // if _, ok := buildData["mode"]; ok { + // RunMode = buildData["mode"].(string) + // } + // if RunMode == "cool-tools" { + // return + // } redisVar, err := g.Cfg().Get(ctx, "redis.cool") if err != nil { Logger.Error(ctx, "初始化缓存失败,请检查配置文件") @@ -54,7 +53,7 @@ func init() { } CacheManager.SetAdapter(gcache.NewAdapterRedis(redis)) IsRedisMode = true - g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(redis)) //设置数据库 + // g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(redis)) //设置数据库 } //Logerebug(ctx, "当前运行模式", RunMode) Logger.Debug(ctx, "当前实例ID:", ProcessFlag) diff --git a/common/utils/concurrent-swiss-map/concurrent_swiss_map.go b/common/utils/concurrent-swiss-map/concurrent_swiss_map.go index d8e4c6627..f16767894 100644 --- a/common/utils/concurrent-swiss-map/concurrent_swiss_map.go +++ b/common/utils/concurrent-swiss-map/concurrent_swiss_map.go @@ -7,7 +7,6 @@ import ( "sync" "github.com/mhmtszr/concurrent-swiss-map/maphash" - "github.com/panjf2000/ants/v2" "github.com/mhmtszr/concurrent-swiss-map/swiss" ) @@ -17,7 +16,7 @@ type CsMap[K comparable, V any] struct { shards []shard[K, V] shardCount uint64 size uint64 - pool *ants.Pool + //pool *ants.Pool } type HashShardPair[K comparable, V any] struct { @@ -48,7 +47,7 @@ func New[K comparable, V any](options ...OptFunc[K, V]) *CsMap[K, V] { for i := 0; i < int(m.shardCount); i++ { m.shards[i] = shard[K, V]{items: swiss.NewMap[K, V](uint32((m.size / m.shardCount) + 1)), RWMutex: &sync.RWMutex{}} } - m.pool, _ = ants.NewPool(-1) + //m.pool, _ = ants.NewPool(-1) return &m } diff --git a/logic/main.go b/logic/main.go index 73382257a..65c7d0f92 100644 --- a/logic/main.go +++ b/logic/main.go @@ -12,7 +12,6 @@ import ( "github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gproc" - "blazing/logic/service/fight" "blazing/logic/service/player" "blazing/cool" @@ -44,7 +43,7 @@ func cleanup() { return false }) - fight.Fightpool.ReleaseTimeout(0) + //fight.Fightpool.ReleaseTimeout(0) log.Println("资源清理完成,程序即将退出") } diff --git a/logic/server.go b/logic/server.go index 210940176..392971598 100644 --- a/logic/server.go +++ b/logic/server.go @@ -57,6 +57,9 @@ func isPortAvailable(port uint32) bool { func Start() { serverID := cool.Config.GameOnlineID cool.Config.ServerInfo = config.NewServerService().GetServerID(serverID).ServerList + // if cool.Config.ServerInfo.IsVip != 0 { + // g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(redis)) //设置数据库 + // } if cool.Config.ServerInfo.IsDebug == 1 { g.DB().SetDebug(true) diff --git a/logic/service/fight/action.go b/logic/service/fight/action.go index 548c757ad..c96cfc512 100644 --- a/logic/service/fight/action.go +++ b/logic/service/fight/action.go @@ -8,10 +8,8 @@ import ( "blazing/logic/service/fight/info" "blazing/logic/service/fight/input" "context" - "log" "github.com/jinzhu/copier" - "github.com/panjf2000/ants/v2" ) // Compare 比较两个1v1战斗动作的执行优先级(核心逻辑) @@ -157,9 +155,10 @@ func (f *FightC) startBattle(startInfo info.FightStartOutboundInfo) { f.startl.Do(func() { // 提交战斗循环到战斗池(处理战斗池容量问题) - if err := Fightpool.Submit(f.battleLoop); err != nil { - log.Panic(context.Background(), "战斗循环提交失败", "error", err) - } + // if err := Fightpool.Invoke(f); err != nil { + // log.Panic(context.Background(), "战斗循环提交失败", "error", err) + // } + go f.battleLoop() f.Broadcast(func(ff *input.Input) { @@ -171,9 +170,14 @@ func (f *FightC) startBattle(startInfo info.FightStartOutboundInfo) { } -var Fightpool *ants.Pool +// var Fightpool *ants.PoolWithFuncGeneric[*FightC] -func init() { - Fightpool, _ = ants.NewPool(-1) - //defer p.Release() -} +// func init() { +// Fightpool, _ = ants.NewPoolWithFuncGeneric(-1, func(f *FightC) { +// f.battleLoop() +// }, ants.WithPanicHandler(func(a any) { +// cool.Logger.Debug(context.Background(), "战斗池发生panic", "error", a) +// }), +// ) +// //defer p.Release() +// } diff --git a/logic/service/fight/loop.go b/logic/service/fight/loop.go index b7518f13c..a11c6ce01 100644 --- a/logic/service/fight/loop.go +++ b/logic/service/fight/loop.go @@ -2,6 +2,7 @@ package fight import ( "blazing/common/data/xmlres" + "blazing/common/socket/errorcode" "blazing/common/utils" "blazing/cool" "context" @@ -20,7 +21,25 @@ import ( ) func (f *FightC) battleLoop() { + defer func() { + if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值 + // 1. 打印错误信息 + cool.Logger.Error(context.TODO(), "panic 错误:", err) + f.Broadcast(func(ff *input.Input) { + if p, ok := ff.Player.(*player.Player); ok { + head := common.NewTomeeHeader(1001, p.Info.UserID) + + head.Result = uint32(errorcode.ErrorCodes.ErrSystemBusyTryLater) + + //实际上这里有个问题,会造成重复保存问题 + + p.SendPack(head.Pack(nil)) + } + }) + + } + }() f.actionChan = make(chan action.BattleActionI, 10) //fmt.Println("战斗开始精灵", f.Our.Player.GetInfo().PetList[0].CatchTime)