From b706478fdcade5b07a2a810c8898deb59a8f7dcf Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 25 Mar 2019 13:02:31 +0200 Subject: [PATCH] Fix MinIdleConns and MaxConnAge --- internal/pool/conn.go | 10 ++++++---- internal/pool/export_test.go | 7 +++++++ internal/pool/pool.go | 2 +- internal/pool/pool_test.go | 5 +---- redis.go | 24 ++++++++++++------------ 5 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 internal/pool/export_test.go diff --git a/internal/pool/conn.go b/internal/pool/conn.go index 1095bfe5..ac48113b 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -17,14 +17,16 @@ type Conn struct { rdLocked bool wr *proto.Writer - InitedAt time.Time - pooled bool - usedAt atomic.Value + Inited bool + pooled bool + createdAt time.Time + usedAt atomic.Value } func NewConn(netConn net.Conn) *Conn { cn := &Conn{ - netConn: netConn, + netConn: netConn, + createdAt: time.Now(), } cn.rd = proto.NewReader(netConn) cn.wr = proto.NewWriter(netConn) diff --git a/internal/pool/export_test.go b/internal/pool/export_test.go new file mode 100644 index 00000000..de7a644e --- /dev/null +++ b/internal/pool/export_test.go @@ -0,0 +1,7 @@ +package pool + +import "time" + +func (cn *Conn) SetCreatedAt(tm time.Time) { + cn.createdAt = tm +} diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 9cecee8a..88b059ca 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -468,7 +468,7 @@ func (p *ConnPool) isStaleConn(cn *Conn) bool { if p.opt.IdleTimeout > 0 && now.Sub(cn.UsedAt()) >= p.opt.IdleTimeout { return true } - if p.opt.MaxConnAge > 0 && now.Sub(cn.InitedAt) >= p.opt.MaxConnAge { + if p.opt.MaxConnAge > 0 && now.Sub(cn.createdAt) >= p.opt.MaxConnAge { return true } diff --git a/internal/pool/pool_test.go b/internal/pool/pool_test.go index 0250f9d6..07fb48ac 100644 --- a/internal/pool/pool_test.go +++ b/internal/pool/pool_test.go @@ -280,7 +280,7 @@ var _ = Describe("conns reaper", func() { case "idle": cn.SetUsedAt(time.Now().Add(-2 * idleTimeout)) case "aged": - cn.InitedAt = time.Now().Add(-2 * maxAge) + cn.SetCreatedAt(time.Now().Add(-2 * maxAge)) } conns = append(conns, cn) staleConns = append(staleConns, cn) @@ -294,9 +294,6 @@ var _ = Describe("conns reaper", func() { } for _, cn := range conns { - if cn.InitedAt.IsZero() { - cn.InitedAt = time.Now() - } connPool.Put(cn) } diff --git a/redis.go b/redis.go index aca30648..644c8da8 100644 --- a/redis.go +++ b/redis.go @@ -51,11 +51,10 @@ func (c *baseClient) newConn() (*pool.Conn, error) { return nil, err } - if cn.InitedAt.IsZero() { - if err := c.initConn(cn); err != nil { - _ = c.connPool.CloseConn(cn) - return nil, err - } + err = c.initConn(cn) + if err != nil { + _ = c.connPool.CloseConn(cn) + return nil, err } return cn, nil @@ -85,12 +84,10 @@ func (c *baseClient) _getConn() (*pool.Conn, error) { return nil, err } - if cn.InitedAt.IsZero() { - err := c.initConn(cn) - if err != nil { - c.connPool.Remove(cn) - return nil, err - } + err = c.initConn(cn) + if err != nil { + c.connPool.Remove(cn) + return nil, err } return cn, nil @@ -121,7 +118,10 @@ func (c *baseClient) releaseConnStrict(cn *pool.Conn, err error) { } func (c *baseClient) initConn(cn *pool.Conn) error { - cn.InitedAt = time.Now() + if cn.Inited { + return nil + } + cn.Inited = true if c.opt.Password == "" && c.opt.DB == 0 &&