mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-12-04 15:26:16 +08:00
代码规范过滤,移除冗余代码,替换掉不推荐的包
This commit is contained in:
@@ -96,14 +96,12 @@ func NewClient(config *ClientConfig) (client *Client, err error) {
|
||||
|
||||
if config.ConnectInterval <= 0 {
|
||||
client.connectInterval = 5 * time.Second
|
||||
//client.Logger.Debugf(client.Ctx, "invalid connectInterval, reset to %v", client.connectInterval)
|
||||
} else {
|
||||
client.connectInterval = config.ConnectInterval
|
||||
}
|
||||
|
||||
if config.Timeout <= 0 {
|
||||
client.timeout = 10 * time.Second
|
||||
//client.Logger.Debugf(client.Ctx, "invalid timeout, reset to %v", client.timeout)
|
||||
} else {
|
||||
client.timeout = config.Timeout
|
||||
}
|
||||
@@ -204,7 +202,7 @@ reconnect:
|
||||
client.Lock()
|
||||
if client.closeFlag {
|
||||
client.Unlock()
|
||||
conn.Close()
|
||||
_ = conn.Close()
|
||||
client.Logger.Debugf(client.Ctx, "client connect but closeFlag is true")
|
||||
return
|
||||
}
|
||||
@@ -226,7 +224,7 @@ func (client *Client) read() {
|
||||
client.Close()
|
||||
client.Logger.Debugf(client.Ctx, "client are about to be reconnected..")
|
||||
time.Sleep(client.connectInterval)
|
||||
client.Start()
|
||||
_ = client.Start()
|
||||
}()
|
||||
|
||||
for {
|
||||
@@ -383,6 +381,6 @@ func (client *Client) RpcRequest(ctx context.Context, data interface{}) (res int
|
||||
}
|
||||
|
||||
return client.rpc.Request(key, func() {
|
||||
client.Write(data)
|
||||
_ = client.Write(data)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func (client *Client) stopCron() {
|
||||
func (client *Client) startCron() {
|
||||
// 心跳超时检查
|
||||
if gcron.Search(client.getCronKey(consts.TCPCronHeartbeatVerify)) == nil {
|
||||
gcron.AddSingleton(client.Ctx, "@every 600s", func(ctx context.Context) {
|
||||
_, _ = gcron.AddSingleton(client.Ctx, "@every 600s", func(ctx context.Context) {
|
||||
if client.heartbeat < gtime.Timestamp()-600 {
|
||||
client.Logger.Debugf(client.Ctx, "client heartbeat timeout, about to reconnect..")
|
||||
client.Destroy()
|
||||
@@ -36,7 +36,7 @@ func (client *Client) startCron() {
|
||||
|
||||
// 心跳
|
||||
if gcron.Search(client.getCronKey(consts.TCPCronHeartbeat)) == nil {
|
||||
gcron.AddSingleton(client.Ctx, "@every 120s", func(ctx context.Context) {
|
||||
_, _ = gcron.AddSingleton(client.Ctx, "@every 120s", func(ctx context.Context) {
|
||||
client.serverHeartbeat()
|
||||
}, client.getCronKey(consts.TCPCronHeartbeat))
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ package tcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/net/gtcp"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"hotgo/internal/consts"
|
||||
)
|
||||
@@ -24,11 +23,6 @@ func initCtx(ctx context.Context, model *Context) (newCtx context.Context, cance
|
||||
return
|
||||
}
|
||||
|
||||
// SetCtx 设置上下文变量
|
||||
func SetCtx(ctx context.Context, model *Context) {
|
||||
context.WithValue(ctx, consts.ContextTCPKey, model)
|
||||
}
|
||||
|
||||
// GetCtx 获得上下文变量,如果没有设置,那么返回nil
|
||||
func GetCtx(ctx context.Context) *Context {
|
||||
value := ctx.Value(consts.ContextTCPKey)
|
||||
@@ -40,23 +34,3 @@ func GetCtx(ctx context.Context) *Context {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCtxConn .
|
||||
func GetCtxConn(ctx context.Context) *gtcp.Conn {
|
||||
c := GetCtx(ctx)
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return c.Conn
|
||||
}
|
||||
|
||||
// GetCtxAuth 认证元数据
|
||||
func GetCtxAuth(ctx context.Context) *AuthMeta {
|
||||
c := GetCtx(ctx)
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return c.Auth
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func MsgPkg(data interface{}, auth *AuthMeta, traceID string) string {
|
||||
|
||||
// doHandleRouterMsg 处理路由消息
|
||||
func doHandleRouterMsg(fun RouterHandler, ctx context.Context, cancel context.CancelFunc, args ...interface{}) {
|
||||
GoPool.Add(ctx, func(ctx context.Context) {
|
||||
_ = GoPool.Add(ctx, func(ctx context.Context) {
|
||||
fun(ctx, args...)
|
||||
cancel()
|
||||
})
|
||||
|
||||
@@ -23,7 +23,6 @@ type ClientConn struct {
|
||||
Conn *gtcp.Conn
|
||||
Auth *AuthMeta
|
||||
heartbeat int64
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -88,11 +87,9 @@ func NewServer(config *ServerConfig) (server *Server, err error) {
|
||||
func (server *Server) accept(conn *gtcp.Conn) {
|
||||
defer func() {
|
||||
server.mutexConns.Lock()
|
||||
conn.Close()
|
||||
_ = conn.Close()
|
||||
// 从登录列表中移除
|
||||
if _, ok := server.clients[conn.RemoteAddr().String()]; ok {
|
||||
delete(server.clients, conn.RemoteAddr().String())
|
||||
}
|
||||
delete(server.clients, conn.RemoteAddr().String())
|
||||
server.mutexConns.Unlock()
|
||||
}()
|
||||
|
||||
@@ -282,14 +279,14 @@ func (server *Server) Close() {
|
||||
|
||||
server.mutexConns.Lock()
|
||||
for _, client := range server.clients {
|
||||
client.Conn.Close()
|
||||
_ = client.Conn.Close()
|
||||
}
|
||||
server.clients = nil
|
||||
server.mutexConns.Unlock()
|
||||
server.wgConns.Wait()
|
||||
|
||||
if server.ln != nil {
|
||||
server.ln.Close()
|
||||
_ = server.ln.Close()
|
||||
}
|
||||
server.wgLn.Wait()
|
||||
}
|
||||
@@ -345,6 +342,6 @@ func (server *Server) RpcRequest(ctx context.Context, client *ClientConn, data i
|
||||
}
|
||||
|
||||
return server.rpc.Request(key, func() {
|
||||
server.Write(client.Conn, data)
|
||||
_ = server.Write(client.Conn, data)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ func (server *Server) stopCron() {
|
||||
func (server *Server) startCron() {
|
||||
// 心跳超时检查
|
||||
if gcron.Search(server.getCronKey(consts.TCPCronHeartbeatVerify)) == nil {
|
||||
gcron.AddSingleton(server.Ctx, "@every 300s", func(ctx context.Context) {
|
||||
_, _ = gcron.AddSingleton(server.Ctx, "@every 300s", func(ctx context.Context) {
|
||||
if server.clients == nil {
|
||||
return
|
||||
}
|
||||
for _, client := range server.clients {
|
||||
if client.heartbeat < gtime.Timestamp()-300 {
|
||||
client.Conn.Close()
|
||||
_ = client.Conn.Close()
|
||||
server.Logger.Debugf(server.Ctx, "client heartbeat timeout, close conn. auth:%+v", client.Auth)
|
||||
}
|
||||
}
|
||||
@@ -41,13 +41,13 @@ func (server *Server) startCron() {
|
||||
|
||||
// 认证检查
|
||||
if gcron.Search(server.getCronKey(consts.TCPCronAuthVerify)) == nil {
|
||||
gcron.AddSingleton(server.Ctx, "@every 300s", func(ctx context.Context) {
|
||||
_, _ = 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()
|
||||
_ = client.Conn.Close()
|
||||
server.Logger.Debugf(server.Ctx, "client auth expired, close conn. auth:%+v", client.Auth)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,14 +37,14 @@ func (server *Server) onServerLogin(ctx context.Context, args ...interface{}) {
|
||||
if err != nil {
|
||||
res.Code = 1
|
||||
res.Message = err.Error()
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models == nil {
|
||||
res.Code = 2
|
||||
res.Message = "授权信息不存在"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -52,28 +52,28 @@ func (server *Server) onServerLogin(ctx context.Context, args ...interface{}) {
|
||||
if _, err = VerifySign(in, models.Appid, models.SecretKey); err != nil {
|
||||
res.Code = 3
|
||||
res.Message = "签名错误,请联系管理员"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.Status != consts.StatusEnabled {
|
||||
res.Code = 4
|
||||
res.Message = "授权已禁用,请联系管理员"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.Group != in.Group {
|
||||
res.Code = 5
|
||||
res.Message = "你登录的授权分组未得到授权,请联系管理员"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.EndAt.Before(gtime.Now()) {
|
||||
res.Code = 6
|
||||
res.Message = "授权已过期,请联系管理员"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ func (server *Server) onServerLogin(ctx context.Context, args ...interface{}) {
|
||||
if _, ok2 := allowedIps[ip]; !ok2 {
|
||||
res.Code = 7
|
||||
res.Message = "IP(" + ip + ")未授权,请联系管理员"
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -92,18 +92,17 @@ func (server *Server) onServerLogin(ctx context.Context, args ...interface{}) {
|
||||
clients := server.getAppIdClients(models.Appid)
|
||||
online := len(clients) + 1
|
||||
if online > models.OnlineLimit {
|
||||
online = 0
|
||||
res2 := new(msgin.ResponseServerLogin)
|
||||
res2.Code = 8
|
||||
res2.Message = "授权登录端超出上限已进行记录。请立即终止操作。如有疑问请联系管理员"
|
||||
for _, client := range clients {
|
||||
server.Write(client.Conn, res2)
|
||||
client.Conn.Close()
|
||||
_ = server.Write(client.Conn, res2)
|
||||
_ = client.Conn.Close()
|
||||
}
|
||||
|
||||
// 当前连接也踢掉
|
||||
server.Write(user.Conn, res2)
|
||||
user.Conn.Close()
|
||||
_ = server.Write(user.Conn, res2)
|
||||
_ = user.Conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -135,7 +134,7 @@ func (server *Server) onServerLogin(ctx context.Context, args ...interface{}) {
|
||||
|
||||
res.AppId = in.AppId
|
||||
res.Code = consts.TCPMsgCodeSuccess
|
||||
server.Write(user.Conn, res)
|
||||
_ = server.Write(user.Conn, res)
|
||||
}
|
||||
|
||||
func (server *Server) onServerHeartbeat(ctx context.Context, args ...interface{}) {
|
||||
@@ -161,6 +160,5 @@ func (server *Server) onServerHeartbeat(ctx context.Context, args ...interface{}
|
||||
}
|
||||
|
||||
res.Code = consts.TCPMsgCodeSuccess
|
||||
server.Write(client.Conn, res)
|
||||
|
||||
_ = server.Write(client.Conn, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user