mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-11 19:50:13 +08:00
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:
@@ -383,6 +383,7 @@ func (mdb *MultiDB) receiveAOF(ctx context.Context, configVersion int32) error {
|
||||
n, mdb.replication.replOffset, strconv.Quote(string(cmdLine.ToBytes()))))
|
||||
mdb.replication.mutex.Unlock()
|
||||
case <-ctx.Done():
|
||||
conn.GetConnPool().Put(conn)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -420,7 +421,7 @@ func (repl *slaveStatus) sendAck2Master() error {
|
||||
strconv.FormatInt(repl.replOffset, 10))
|
||||
psyncReq := protocol.MakeMultiBulkReply(psyncCmdLine)
|
||||
_, err := repl.masterConn.Write(psyncReq.ToBytes())
|
||||
//logger.Info("send ack to master")
|
||||
// logger.Info("send ack to master")
|
||||
return err
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@ package connection
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/hdt3213/godis/lib/logger"
|
||||
"github.com/hdt3213/godis/lib/sync/wait"
|
||||
"net"
|
||||
"sync"
|
||||
@@ -42,6 +43,17 @@ type Connection struct {
|
||||
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
|
||||
func (c *Connection) RemoteAddr() net.Addr {
|
||||
return c.conn.RemoteAddr()
|
||||
@@ -56,9 +68,15 @@ func (c *Connection) Close() error {
|
||||
|
||||
// NewConn creates Connection instance
|
||||
func NewConn(conn net.Conn) *Connection {
|
||||
return &Connection{
|
||||
conn: conn,
|
||||
c, ok := connPool.Get().(*Connection)
|
||||
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
|
||||
|
@@ -50,6 +50,8 @@ func (h *Handler) closeClient(client *connection.Connection) {
|
||||
_ = client.Close()
|
||||
h.db.AfterClientClose(client)
|
||||
h.activeConn.Delete(client)
|
||||
|
||||
client.GetConnPool().Put(client)
|
||||
}
|
||||
|
||||
// Handle receives and executes redis commands
|
||||
|
Reference in New Issue
Block a user