forked from mirror/redis
Fix MinIdleConns and MaxConnAge
This commit is contained in:
parent
9ecae37814
commit
b706478fdc
|
@ -17,14 +17,16 @@ type Conn struct {
|
||||||
rdLocked bool
|
rdLocked bool
|
||||||
wr *proto.Writer
|
wr *proto.Writer
|
||||||
|
|
||||||
InitedAt time.Time
|
Inited bool
|
||||||
pooled bool
|
pooled bool
|
||||||
usedAt atomic.Value
|
createdAt time.Time
|
||||||
|
usedAt atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn(netConn net.Conn) *Conn {
|
func NewConn(netConn net.Conn) *Conn {
|
||||||
cn := &Conn{
|
cn := &Conn{
|
||||||
netConn: netConn,
|
netConn: netConn,
|
||||||
|
createdAt: time.Now(),
|
||||||
}
|
}
|
||||||
cn.rd = proto.NewReader(netConn)
|
cn.rd = proto.NewReader(netConn)
|
||||||
cn.wr = proto.NewWriter(netConn)
|
cn.wr = proto.NewWriter(netConn)
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package pool
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func (cn *Conn) SetCreatedAt(tm time.Time) {
|
||||||
|
cn.createdAt = tm
|
||||||
|
}
|
|
@ -468,7 +468,7 @@ func (p *ConnPool) isStaleConn(cn *Conn) bool {
|
||||||
if p.opt.IdleTimeout > 0 && now.Sub(cn.UsedAt()) >= p.opt.IdleTimeout {
|
if p.opt.IdleTimeout > 0 && now.Sub(cn.UsedAt()) >= p.opt.IdleTimeout {
|
||||||
return true
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -280,7 +280,7 @@ var _ = Describe("conns reaper", func() {
|
||||||
case "idle":
|
case "idle":
|
||||||
cn.SetUsedAt(time.Now().Add(-2 * idleTimeout))
|
cn.SetUsedAt(time.Now().Add(-2 * idleTimeout))
|
||||||
case "aged":
|
case "aged":
|
||||||
cn.InitedAt = time.Now().Add(-2 * maxAge)
|
cn.SetCreatedAt(time.Now().Add(-2 * maxAge))
|
||||||
}
|
}
|
||||||
conns = append(conns, cn)
|
conns = append(conns, cn)
|
||||||
staleConns = append(staleConns, cn)
|
staleConns = append(staleConns, cn)
|
||||||
|
@ -294,9 +294,6 @@ var _ = Describe("conns reaper", func() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cn := range conns {
|
for _, cn := range conns {
|
||||||
if cn.InitedAt.IsZero() {
|
|
||||||
cn.InitedAt = time.Now()
|
|
||||||
}
|
|
||||||
connPool.Put(cn)
|
connPool.Put(cn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
redis.go
24
redis.go
|
@ -51,11 +51,10 @@ func (c *baseClient) newConn() (*pool.Conn, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cn.InitedAt.IsZero() {
|
err = c.initConn(cn)
|
||||||
if err := c.initConn(cn); err != nil {
|
if err != nil {
|
||||||
_ = c.connPool.CloseConn(cn)
|
_ = c.connPool.CloseConn(cn)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cn, nil
|
return cn, nil
|
||||||
|
@ -85,12 +84,10 @@ func (c *baseClient) _getConn() (*pool.Conn, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cn.InitedAt.IsZero() {
|
err = c.initConn(cn)
|
||||||
err := c.initConn(cn)
|
if err != nil {
|
||||||
if err != nil {
|
c.connPool.Remove(cn)
|
||||||
c.connPool.Remove(cn)
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cn, nil
|
return cn, nil
|
||||||
|
@ -121,7 +118,10 @@ func (c *baseClient) releaseConnStrict(cn *pool.Conn, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *baseClient) initConn(cn *pool.Conn) 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 == "" &&
|
if c.opt.Password == "" &&
|
||||||
c.opt.DB == 0 &&
|
c.opt.DB == 0 &&
|
||||||
|
|
Loading…
Reference in New Issue