From b5c6462dac00cfde088a85a846465d2a6bb8c124 Mon Sep 17 00:00:00 2001 From: zhengchong <730427512@qq.com> Date: Sat, 29 May 2021 11:36:41 +0800 Subject: [PATCH] perf: atomic replace RWMutex --- znet/connection.go | 4 ++-- znet/connmanager.go | 56 +++++++++++++++++++-------------------------- znet/server.go | 2 +- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/znet/connection.go b/znet/connection.go index a168942..f41c568 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -39,8 +39,8 @@ type Connection struct { isClosed bool } -//NewConntion 创建连接的方法 -func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection { +//NewConnection 创建连接的方法 +func NewConnection(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection { //初始化Conn属性 c := &Connection{ TCPServer: server, diff --git a/znet/connmanager.go b/znet/connmanager.go index d3fcb1b..306760c 100644 --- a/znet/connmanager.go +++ b/znet/connmanager.go @@ -3,55 +3,49 @@ package znet import ( "errors" "fmt" - "sync" + "sync/atomic" "github.com/aceld/zinx/ziface" ) //ConnManager 连接管理模块 type ConnManager struct { - connections map[uint32]ziface.IConnection //管理的连接信息 - connLock sync.RWMutex //读写连接的读写锁 + connections atomic.Value } //NewConnManager 创建一个链接管理 func NewConnManager() *ConnManager { - return &ConnManager{ - connections: make(map[uint32]ziface.IConnection), - } + var cm = &ConnManager{} + connections := make(map[uint32]ziface.IConnection) + cm.connections.Store(connections) + return cm } //Add 添加链接 func (connMgr *ConnManager) Add(conn ziface.IConnection) { - //保护共享资源Map 加写锁 - connMgr.connLock.Lock() - defer connMgr.connLock.Unlock() + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) //将conn连接添加到ConnMananger中 - connMgr.connections[conn.GetConnID()] = conn + connections[conn.GetConnID()] = conn + connMgr.connections.Store(connections) fmt.Println("connection add to ConnManager successfully: conn num = ", connMgr.Len()) } //Remove 删除连接 func (connMgr *ConnManager) Remove(conn ziface.IConnection) { - //保护共享资源Map 加写锁 - connMgr.connLock.Lock() - defer connMgr.connLock.Unlock() - + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) //删除连接信息 - delete(connMgr.connections, conn.GetConnID()) - + delete(connections, conn.GetConnID()) + connMgr.connections.Store(connections) fmt.Println("connection Remove ConnID=", conn.GetConnID(), " successfully: conn num = ", connMgr.Len()) } //Get 利用ConnID获取链接 func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error) { - //保护共享资源Map 加读锁 - connMgr.connLock.RLock() - defer connMgr.connLock.RUnlock() + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) - if conn, ok := connMgr.connections[connID]; ok { + if conn, ok := connections[connID]; ok { return conn, nil } @@ -61,37 +55,35 @@ func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error) { //Len 获取当前连接 func (connMgr *ConnManager) Len() int { - return len(connMgr.connections) + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) + return len(connections) } //ClearConn 清除并停止所有连接 func (connMgr *ConnManager) ClearConn() { - //保护共享资源Map 加写锁 - connMgr.connLock.Lock() - defer connMgr.connLock.Unlock() + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) //停止并删除全部的连接信息 - for connID, conn := range connMgr.connections { + for connID, conn := range connections { //停止 conn.Stop() //删除 - delete(connMgr.connections, connID) + delete(connections, connID) } - + connMgr.connections.Store(connections) fmt.Println("Clear All Connections successfully: conn num = ", connMgr.Len()) } //ClearOneConn 利用ConnID获取一个链接 并且删除 func (connMgr *ConnManager) ClearOneConn(connID uint32) { - //保护共享资源Map 加写锁 - connMgr.connLock.Lock() - defer connMgr.connLock.Unlock() + connections:=connMgr.connections.Load().(map[uint32]ziface.IConnection) - if conn, ok := connMgr.connections[connID]; !ok { + if conn, ok := connections[connID]; !ok { //停止 conn.Stop() //删除 - delete(connMgr.connections, connID) + delete(connections, connID) + connMgr.connections.Store(connections) fmt.Println("Clear Connections ID: ", connID, "succeed") return } diff --git a/znet/server.go b/znet/server.go index ac9ccf5..915df2d 100644 --- a/znet/server.go +++ b/znet/server.go @@ -105,7 +105,7 @@ func (s *Server) Start() { } //3.3 处理该新连接请求的 业务 方法, 此时应该有 handler 和 conn是绑定的 - dealConn := NewConntion(s, conn, cID, s.msgHandler) + dealConn := NewConnection(s, conn, cID, s.msgHandler) cID++ //3.4 启动当前链接的处理业务