发布v2.4.4版本,本次为优化版本。更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md

This commit is contained in:
孟帅
2023-03-16 15:35:02 +08:00
parent 1acc6d17c4
commit 2c27be12fd
16 changed files with 309 additions and 282 deletions

View File

@@ -1,9 +1,12 @@
package tcp
import "github.com/gogf/gf/v2/os/gtime"
// 定时任务
const (
cronHeartbeatVerify = "tcpHeartbeatVerify"
cronHeartbeat = "tcpHeartbeat"
cronAuthVerify = "tcpAuthVerify"
)
// 认证分组
@@ -15,10 +18,11 @@ const (
// AuthMeta 认证元数据
type AuthMeta struct {
Group string `json:"group"`
Name string `json:"name"`
AppId string `json:"appId"`
SecretKey string `json:"secretKey"`
Group string `json:"group"`
Name string `json:"name"`
AppId string `json:"appId"`
SecretKey string `json:"secretKey"`
EndAt *gtime.Time `json:"-"`
}
// CallbackEvent 回调事件

View File

@@ -27,9 +27,24 @@ func (server *Server) startCron() {
for _, client := range server.clients {
if client.heartbeat < gtime.Timestamp()-300 {
client.Conn.Close()
server.Logger.Debugf(server.Ctx, "client heartbeat timeout, about to reconnect.. auth:%+v", client.Auth)
server.Logger.Debugf(server.Ctx, "client heartbeat timeout, close conn. auth:%+v", client.Auth)
}
}
}, server.getCronKey(cronHeartbeatVerify))
}
// 认证检查
if gcron.Search(server.getCronKey(cronAuthVerify)) == nil {
gcron.AddSingleton(server.Ctx, "@every 300s", func(ctx context.Context) {
if server.clients == nil {
return
}
for _, client := range server.clients {
if client.Auth.EndAt.Before(gtime.Now()) {
client.Conn.Close()
server.Logger.Debugf(server.Ctx, "client auth expired, close conn. auth:%+v", client.Auth)
}
}
}, server.getCronKey(cronAuthVerify))
}
}

View File

@@ -107,6 +107,7 @@ func (server *Server) onServerLogin(args ...interface{}) {
Name: in.Name,
AppId: in.AppId,
SecretKey: models.SecretKey,
EndAt: models.EndAt,
},
heartbeat: gtime.Timestamp(),
}