fix: 修复多实例下connId共享的问题,修复消息为空时连接并未主动关闭的问题。

main v0.5.4
NoahLan 2 years ago
parent 2cc3e06721
commit 1a30d41e21

@ -8,7 +8,6 @@ import (
"git.noahlan.cn/noahlan/nnet/scheduler" "git.noahlan.cn/noahlan/nnet/scheduler"
"git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntools-go/core/nlog"
"git.noahlan.cn/noahlan/ntools-go/core/pool" "git.noahlan.cn/noahlan/ntools-go/core/pool"
"io"
"net" "net"
"sync/atomic" "sync/atomic"
) )
@ -71,7 +70,7 @@ func newConnection(server *engine, conn net.Conn) *connection {
_, r.isWS = conn.(*WSConn) _, r.isWS = conn.(*WSConn)
// binding session // binding session
r.session = newSession(r) r.session = newSession(r, server.sessIdMgr.SessionID())
return r return r
} }
@ -204,12 +203,18 @@ func (r *connection) read() {
for { for {
n, err := r.conn.Read(buf) n, err := r.conn.Read(buf)
//nlog.Debugf("receive data %v", buf[:n]) //nlog.Debugf("receive data %v", buf[:n])
if err != nil && err != io.EOF { if err != nil {
nlog.Errorf("%s [readLoop] Read message error: %s, session will be closed immediately", nlog.Errorf("%s [readLoop] Read message error: %s, session will be closed immediately",
r.ngin.logPrefix(), err.Error()) r.ngin.logPrefix(), err.Error())
return return
} }
if n == 0 {
nlog.Errorf("%s [readLoop] Read empty message, session will be closed immediately",
r.ngin.logPrefix())
return
}
if r.packer == nil { if r.packer == nil {
nlog.Errorf("%s [readLoop] unexpected error: packer is nil", r.ngin.logPrefix()) nlog.Errorf("%s [readLoop] unexpected error: packer is nil", r.ngin.logPrefix())
return return

@ -50,6 +50,7 @@ type (
wsOpt wsOptions // websocket wsOpt wsOptions // websocket
connManager *conn2.Manager connManager *conn2.Manager
sessIdMgr *sessionIDMgr
} }
wsOptions struct { wsOptions struct {
@ -70,6 +71,7 @@ func newEngine(conf config.EngineConf) *engine {
routes: make([]Route, 0), routes: make([]Route, 0),
taskTimerPrecision: conf.TaskTimerPrecision, taskTimerPrecision: conf.TaskTimerPrecision,
connManager: conn2.NewManager(), connManager: conn2.NewManager(),
sessIdMgr: newSessionIDMgr(),
} }
pool.InitPool(conf.Pool) pool.InitPool(conf.Pool)

@ -17,9 +17,9 @@ type session struct {
data map[string]interface{} // session数据存储内存 data map[string]interface{} // session数据存储内存
} }
func newSession(entity entity.NetworkEntity) *session { func newSession(entity entity.NetworkEntity, id int64) *session {
return &session{ return &session{
id: sessionIDMgrInstance.SessionID(), id: id,
entity: entity, entity: entity,
uid: "", uid: "",
data: make(map[string]interface{}), data: make(map[string]interface{}),
@ -110,8 +110,6 @@ func (s *session) Close() {
s.Invalidate() s.Invalidate()
} }
var sessionIDMgrInstance = newSessionIDMgr()
type sessionIDMgr struct { type sessionIDMgr struct {
count int64 count int64
sid int64 sid int64

Loading…
Cancel
Save