forked from mirror/redis
internal/pool: more idiomatic work with channels.
This commit is contained in:
parent
4b0862b5fd
commit
455abb1906
|
@ -84,9 +84,6 @@ func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout, idleCheckF
|
||||||
conns: make([]*Conn, 0, poolSize),
|
conns: make([]*Conn, 0, poolSize),
|
||||||
freeConns: make([]*Conn, 0, poolSize),
|
freeConns: make([]*Conn, 0, poolSize),
|
||||||
}
|
}
|
||||||
for i := 0; i < poolSize; i++ {
|
|
||||||
p.queue <- struct{}{}
|
|
||||||
}
|
|
||||||
if idleTimeout > 0 && idleCheckFrequency > 0 {
|
if idleTimeout > 0 && idleCheckFrequency > 0 {
|
||||||
go p.reaper(idleCheckFrequency)
|
go p.reaper(idleCheckFrequency)
|
||||||
}
|
}
|
||||||
|
@ -125,7 +122,7 @@ func (p *ConnPool) PopFree() *Conn {
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-p.queue:
|
case p.queue <- struct{}{}:
|
||||||
timers.Put(timer)
|
timers.Put(timer)
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
timers.Put(timer)
|
timers.Put(timer)
|
||||||
|
@ -138,7 +135,7 @@ func (p *ConnPool) PopFree() *Conn {
|
||||||
p.freeConnsMu.Unlock()
|
p.freeConnsMu.Unlock()
|
||||||
|
|
||||||
if cn == nil {
|
if cn == nil {
|
||||||
p.queue <- struct{}{}
|
<-p.queue
|
||||||
}
|
}
|
||||||
return cn
|
return cn
|
||||||
}
|
}
|
||||||
|
@ -168,7 +165,7 @@ func (p *ConnPool) Get() (*Conn, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-p.queue:
|
case p.queue <- struct{}{}:
|
||||||
timers.Put(timer)
|
timers.Put(timer)
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
timers.Put(timer)
|
timers.Put(timer)
|
||||||
|
@ -190,7 +187,7 @@ func (p *ConnPool) Get() (*Conn, error) {
|
||||||
|
|
||||||
newcn, err := p.NewConn()
|
newcn, err := p.NewConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.queue <- struct{}{}
|
<-p.queue
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,13 +210,13 @@ func (p *ConnPool) Put(cn *Conn) error {
|
||||||
p.freeConnsMu.Lock()
|
p.freeConnsMu.Lock()
|
||||||
p.freeConns = append(p.freeConns, cn)
|
p.freeConns = append(p.freeConns, cn)
|
||||||
p.freeConnsMu.Unlock()
|
p.freeConnsMu.Unlock()
|
||||||
p.queue <- struct{}{}
|
<-p.queue
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) Remove(cn *Conn, reason error) error {
|
func (p *ConnPool) Remove(cn *Conn, reason error) error {
|
||||||
p.remove(cn, reason)
|
p.remove(cn, reason)
|
||||||
p.queue <- struct{}{}
|
<-p.queue
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,13 +319,13 @@ func (p *ConnPool) reapStaleConn() bool {
|
||||||
func (p *ConnPool) ReapStaleConns() (int, error) {
|
func (p *ConnPool) ReapStaleConns() (int, error) {
|
||||||
var n int
|
var n int
|
||||||
for {
|
for {
|
||||||
<-p.queue
|
p.queue <- struct{}{}
|
||||||
p.freeConnsMu.Lock()
|
p.freeConnsMu.Lock()
|
||||||
|
|
||||||
reaped := p.reapStaleConn()
|
reaped := p.reapStaleConn()
|
||||||
|
|
||||||
p.freeConnsMu.Unlock()
|
p.freeConnsMu.Unlock()
|
||||||
p.queue <- struct{}{}
|
<-p.queue
|
||||||
|
|
||||||
if reaped {
|
if reaped {
|
||||||
n++
|
n++
|
||||||
|
|
Loading…
Reference in New Issue