```
refactor(socket): 使用fmt.Println替换Logger.Error处理panic 修复socket连接关闭和流量处理中的panic恢复机制, 使用fmt.Println直接打印错误信息替代原有的Logger组件。 --- refactor(timer): 移除ants协程池依赖并优化错误处理 移除timer模块中对ants协程池的依赖,改用原生goroutine, 添加panic恢复机制确保任务执行的稳定性。 --- feat(player): 移除CountPlayer函数 删除玩家服务中统计在线玩家数量的功能函数, 该功能
This commit is contained in:
@@ -2,6 +2,7 @@ package socket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
@@ -44,7 +45,7 @@ func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
||||
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||||
// 1. 打印错误信息
|
||||
|
||||
cool.Logger.Error(context.TODO(), "panic 错误:", err)
|
||||
fmt.Println(context.TODO(), "panic 错误:", err)
|
||||
|
||||
}
|
||||
}()
|
||||
@@ -111,7 +112,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||||
// 1. 打印错误信息
|
||||
|
||||
cool.Logger.Error(context.TODO(), "panic 错误:", err)
|
||||
fmt.Println(context.TODO(), "panic 错误:", err)
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -6,11 +6,10 @@ package timer
|
||||
import (
|
||||
"container/heap"
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
var _ Timer = (*minHeap)(nil)
|
||||
@@ -113,8 +112,6 @@ func (m *minHeap) getNewSleepTime() time.Duration {
|
||||
return timeout
|
||||
}
|
||||
|
||||
var pool, _ = ants.NewPool(-1)
|
||||
|
||||
func (m *minHeap) process() {
|
||||
for {
|
||||
m.mu.Lock()
|
||||
@@ -153,11 +150,19 @@ func (m *minHeap) process() {
|
||||
|
||||
// 正在运行的任务数加1
|
||||
atomic.AddInt32(&m.runCount, 1)
|
||||
pool.Submit(func() {
|
||||
go func() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||||
// 1. 打印错误信息
|
||||
|
||||
fmt.Println(context.TODO(), "panic 错误:", err)
|
||||
|
||||
}
|
||||
}()
|
||||
callback()
|
||||
// 对正在运行的任务数减1
|
||||
atomic.AddInt32(&m.runCount, -1)
|
||||
})
|
||||
}()
|
||||
|
||||
// 如果堆中没有元素,就等待
|
||||
if m.minHeaps.Len() == 0 {
|
||||
|
||||
Reference in New Issue
Block a user