perf: pooled connection`s instantiation

- pooled connection: reduce the pressure on the GC to process heap memory objects with high concurrent "connection"
This commit is contained in:
Tianyi Zheng
2022-11-25 11:06:17 +08:00
committed by finley
parent f255fac7fb
commit a7a3da6e49
3 changed files with 24 additions and 3 deletions

View File

@@ -383,6 +383,7 @@ func (mdb *MultiDB) receiveAOF(ctx context.Context, configVersion int32) error {
n, mdb.replication.replOffset, strconv.Quote(string(cmdLine.ToBytes())))) n, mdb.replication.replOffset, strconv.Quote(string(cmdLine.ToBytes()))))
mdb.replication.mutex.Unlock() mdb.replication.mutex.Unlock()
case <-ctx.Done(): case <-ctx.Done():
conn.GetConnPool().Put(conn)
return nil return nil
} }
} }
@@ -420,7 +421,7 @@ func (repl *slaveStatus) sendAck2Master() error {
strconv.FormatInt(repl.replOffset, 10)) strconv.FormatInt(repl.replOffset, 10))
psyncReq := protocol.MakeMultiBulkReply(psyncCmdLine) psyncReq := protocol.MakeMultiBulkReply(psyncCmdLine)
_, err := repl.masterConn.Write(psyncReq.ToBytes()) _, err := repl.masterConn.Write(psyncReq.ToBytes())
//logger.Info("send ack to master") // logger.Info("send ack to master")
return err return err
} }

View File

@@ -2,6 +2,7 @@ package connection
import ( import (
"bytes" "bytes"
"github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/lib/sync/wait" "github.com/hdt3213/godis/lib/sync/wait"
"net" "net"
"sync" "sync"
@@ -42,6 +43,17 @@ type Connection struct {
role int32 role int32
} }
var connPool = sync.Pool{
New: func() interface{} {
return &Connection{}
},
}
// GetConnPool returns the connection pool pointer for putting and getting connection
func (c *Connection) GetConnPool() *sync.Pool {
return &connPool
}
// RemoteAddr returns the remote network address // RemoteAddr returns the remote network address
func (c *Connection) RemoteAddr() net.Addr { func (c *Connection) RemoteAddr() net.Addr {
return c.conn.RemoteAddr() return c.conn.RemoteAddr()
@@ -56,9 +68,15 @@ func (c *Connection) Close() error {
// NewConn creates Connection instance // NewConn creates Connection instance
func NewConn(conn net.Conn) *Connection { func NewConn(conn net.Conn) *Connection {
return &Connection{ c, ok := connPool.Get().(*Connection)
conn: conn, if !ok {
logger.Error("connection pool make wrong type")
return &Connection{
conn: conn,
}
} }
c.conn = conn
return c
} }
// Write sends response to client over tcp connection // Write sends response to client over tcp connection

View File

@@ -50,6 +50,8 @@ func (h *Handler) closeClient(client *connection.Connection) {
_ = client.Close() _ = client.Close()
h.db.AfterClientClose(client) h.db.AfterClientClose(client)
h.activeConn.Delete(client) h.activeConn.Delete(client)
client.GetConnPool().Put(client)
} }
// Handle receives and executes redis commands // Handle receives and executes redis commands