From c41646eac8e9654d5387d53aa57a23519065997f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Tue, 28 Oct 2025 23:56:15 +0800 Subject: [PATCH] =?UTF-8?q?```=20fix(socket):=20=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=BF=9E=E6=8E=A5=E5=85=B3=E9=97=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将连接关闭时的数据保存和资源清理操作提交到工作池中异步执行, 避免阻塞主线程。但需要注意可能存在执行顺序问题,需要进一步修复。 ``` --- common/socket/ServerEvent.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index 6db74c939..744766adf 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -54,17 +54,20 @@ func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) { atomic.AddInt64(&s.connected, -1) //logging.Infof("conn[%v] disconnected", c.RemoteAddr().String()) v, _ := c.Context().(*player.ClientData) - v.Lf.Close() //关闭lockfree + s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复 + v.Lf.Close() //关闭lockfree + + if v.Player != nil { + //cool.Loger.Info(context.TODO(), "准备保存", v.Player.Info.UserID) + v.Player.Save() //保存玩家数据 + //cool.Loger.Info(context.TODO(), "保存完成", v.Player.Info.UserID) + if v.CloseChan != nil { + close(v.CloseChan) + } - if v.Player != nil { - //cool.Loger.Info(context.TODO(), "准备保存", v.Player.Info.UserID) - v.Player.Save() //保存玩家数据 - //cool.Loger.Info(context.TODO(), "保存完成", v.Player.Info.UserID) - if v.CloseChan != nil { - close(v.CloseChan) } - } + }) //} //关闭连接