goProject/trunk/game/common/clientMgr/expire.go

42 lines
1.1 KiB
Go
Raw Normal View History

2025-01-15 17:36:12 +08:00
package clientMgr
import (
"time"
"framework/goroutineMgr"
"goutil/logUtil"
)
func init() {
// 清理过期的客户端
go clearExpiredClient()
}
// 清理过期的客户端
func clearExpiredClient() {
// 处理goroutine数量
goroutineName := "clientMgr.clearExpiredClient"
goroutineMgr.Monitor(goroutineName)
defer goroutineMgr.ReleaseMonitor(goroutineName)
for {
// 休眠指定的时间(单位:秒)(放在此处是因为程序刚启动时并没有过期的客户端所以先不用占用资源并且此时LogPath尚未设置如果直接执行后面的代码会出现panic异常)
time.Sleep(5 * time.Minute)
// 获取过期的客户端列表
expiredClientList := getExpiredClientList()
expiredClientCount := len(expiredClientList)
beforeClientCount := getClientCount()
// 客户端断开
if expiredClientCount > 0 {
for _, item := range expiredClientList {
Disconnect(item)
}
}
// 记录日志
logUtil.DebugLog("清理前的客户端数量为:%d本次清理不活跃的客户端数量为%d", beforeClientCount, expiredClientCount)
}
}