Make free-connection stack a LIFO.

This commit is contained in:
Dimitrij Denissenko 2016-03-12 13:39:50 +02:00 committed by Vladimir Mihailenco
parent 7a4f8ede8f
commit 1bb55e3a9a
3 changed files with 103 additions and 37 deletions

View File

@ -35,6 +35,10 @@ func NewConn(netConn net.Conn) *Conn {
return cn return cn
} }
func (cn *Conn) IsStale(timeout time.Duration) bool {
return timeout > 0 && time.Since(cn.UsedAt) > timeout
}
func (cn *Conn) SetNetConn(netConn net.Conn) { func (cn *Conn) SetNetConn(netConn net.Conn) {
cn.netConn = netConn cn.netConn = netConn
cn.UsedAt = time.Now() cn.UsedAt = time.Now()

View File

@ -0,0 +1,72 @@
package pool
import (
"sync"
"time"
)
// connStack is used as a LIFO to maintain free connections
type connStack struct {
cns []*Conn
free chan struct{}
mu sync.Mutex
}
func newConnStack(max int) *connStack {
return &connStack{
cns: make([]*Conn, 0, max),
free: make(chan struct{}, max),
}
}
func (s *connStack) Len() int { return len(s.free) }
func (s *connStack) Push(cn *Conn) {
s.mu.Lock()
s.cns = append(s.cns, cn)
s.mu.Unlock()
s.free <- struct{}{}
}
func (s *connStack) ShiftStale(timeout time.Duration) *Conn {
select {
case <-s.free:
s.mu.Lock()
defer s.mu.Unlock()
if cn := s.cns[0]; cn.IsStale(timeout) {
copy(s.cns, s.cns[1:])
s.cns = s.cns[:len(s.cns)-1]
return cn
}
return nil
default:
return nil
}
}
func (s *connStack) Pop() *Conn {
select {
case <-s.free:
return s.pop()
default:
return nil
}
}
func (s *connStack) PopWithTimeout(d time.Duration) *Conn {
select {
case <-s.free:
return s.pop()
case <-time.After(d):
return nil
}
}
func (s *connStack) pop() (cn *Conn) {
s.mu.Lock()
ci := len(s.cns) - 1
cn, s.cns = s.cns[ci], s.cns[:ci]
s.mu.Unlock()
return
}

View File

@ -50,7 +50,7 @@ type ConnPool struct {
idleTimeout time.Duration idleTimeout time.Duration
conns *connList conns *connList
freeConns chan *Conn freeConns *connStack
stats PoolStats stats PoolStats
_closed int32 _closed int32
@ -67,7 +67,7 @@ func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout time.Durati
idleTimeout: idleTimeout, idleTimeout: idleTimeout,
conns: newConnList(poolSize), conns: newConnList(poolSize),
freeConns: make(chan *Conn, poolSize), freeConns: newConnStack(poolSize),
} }
if idleTimeout > 0 { if idleTimeout > 0 {
go p.reaper() go p.reaper()
@ -87,9 +87,8 @@ func (p *ConnPool) isIdle(cn *Conn) bool {
// there are no connections. // there are no connections.
func (p *ConnPool) First() *Conn { func (p *ConnPool) First() *Conn {
for { for {
select { cn := p.freeConns.Pop()
case cn := <-p.freeConns: if cn != nil && cn.IsStale(p.idleTimeout) {
if p.isIdle(cn) {
var err error var err error
cn, err = p.replace(cn) cn, err = p.replace(cn)
if err != nil { if err != nil {
@ -98,20 +97,14 @@ func (p *ConnPool) First() *Conn {
} }
} }
return cn return cn
default:
return nil
} }
}
panic("not reached")
} }
// wait waits for free non-idle connection. It returns nil on timeout. // wait waits for free non-idle connection. It returns nil on timeout.
func (p *ConnPool) wait() *Conn { func (p *ConnPool) wait() *Conn {
deadline := time.After(p.poolTimeout)
for { for {
select { cn := p.freeConns.PopWithTimeout(p.poolTimeout)
case cn := <-p.freeConns: if cn != nil && cn.IsStale(p.idleTimeout) {
if p.isIdle(cn) {
var err error var err error
cn, err = p.replace(cn) cn, err = p.replace(cn)
if err != nil { if err != nil {
@ -120,11 +113,7 @@ func (p *ConnPool) wait() *Conn {
} }
} }
return cn return cn
case <-deadline:
return nil
} }
}
panic("not reached")
} }
func (p *ConnPool) dial() (net.Conn, error) { func (p *ConnPool) dial() (net.Conn, error) {
@ -198,7 +187,7 @@ func (p *ConnPool) Put(cn *Conn) error {
Logger.Print(err) Logger.Print(err)
return p.Replace(cn, err) return p.Replace(cn, err)
} }
p.freeConns <- cn p.freeConns.Push(cn)
return nil return nil
} }
@ -223,7 +212,7 @@ func (p *ConnPool) Replace(cn *Conn, reason error) error {
if err != nil { if err != nil {
return err return err
} }
p.freeConns <- newcn p.freeConns.Push(newcn)
return nil return nil
} }
@ -234,7 +223,7 @@ func (p *ConnPool) Len() int {
// FreeLen returns number of free connections. // FreeLen returns number of free connections.
func (p *ConnPool) FreeLen() int { func (p *ConnPool) FreeLen() int {
return len(p.freeConns) return p.freeConns.Len()
} }
func (p *ConnPool) Stats() *PoolStats { func (p *ConnPool) Stats() *PoolStats {
@ -273,11 +262,12 @@ func (p *ConnPool) reaper() {
break break
} }
// pool.First removes idle connections from the pool and for {
// returns first non-idle connection. So just put returned cn := p.freeConns.ShiftStale(p.idleTimeout)
// connection back. if cn == nil {
if cn := p.First(); cn != nil { break
p.Put(cn) }
_ = p.conns.Remove(cn)
} }
} }
} }