mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-10 11:13:44 +08:00
发布v2.8.4版本,更新内容请查看:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN/addon-version-upgrade.md
This commit is contained in:
@@ -10,12 +10,11 @@ import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gcron"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"hotgo/internal/consts"
|
||||
)
|
||||
|
||||
// getCronKey 生成客户端定时任务名称
|
||||
func (client *Client) getCronKey(s string) string {
|
||||
return fmt.Sprintf("tcp.client_%s_%s:%s", s, client.auth.Group, client.auth.Name)
|
||||
return fmt.Sprintf("tcp.client_%s:%s", s, client.conn.LocalAddr().String())
|
||||
}
|
||||
|
||||
// stopCron 停止定时任务
|
||||
@@ -28,19 +27,22 @@ func (client *Client) stopCron() {
|
||||
// startCron 启动定时任务
|
||||
func (client *Client) startCron() {
|
||||
// 心跳超时检查
|
||||
if gcron.Search(client.getCronKey(consts.TCPCronHeartbeatVerify)) == nil {
|
||||
_, _ = gcron.AddSingleton(client.Ctx, "@every 600s", func(ctx context.Context) {
|
||||
if client.heartbeat < gtime.Timestamp()-consts.TCPHeartbeatTimeout {
|
||||
client.Logger.Debugf(client.Ctx, "client heartbeat timeout, about to reconnect..")
|
||||
if gcron.Search(client.getCronKey(CronHeartbeatVerify)) == nil {
|
||||
_, _ = gcron.AddSingleton(client.ctx, "@every 600s", func(ctx context.Context) {
|
||||
if client == nil || client.conn == nil {
|
||||
return
|
||||
}
|
||||
if client.conn.Heartbeat < gtime.Timestamp()-HeartbeatTimeout {
|
||||
client.logger.Debugf(client.ctx, "client heartbeat timeout, about to reconnect..")
|
||||
client.Destroy()
|
||||
}
|
||||
}, client.getCronKey(consts.TCPCronHeartbeatVerify))
|
||||
}, client.getCronKey(CronHeartbeatVerify))
|
||||
}
|
||||
|
||||
// 心跳
|
||||
if gcron.Search(client.getCronKey(consts.TCPCronHeartbeat)) == nil {
|
||||
_, _ = gcron.AddSingleton(client.Ctx, "@every 120s", func(ctx context.Context) {
|
||||
if gcron.Search(client.getCronKey(CronHeartbeat)) == nil {
|
||||
_, _ = gcron.AddSingleton(client.ctx, "@every 300s", func(ctx context.Context) {
|
||||
client.serverHeartbeat()
|
||||
}, client.getCronKey(consts.TCPCronHeartbeat))
|
||||
}, client.getCronKey(CronHeartbeat))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user