mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
Allow FIFO pool in redis client (#1820)
* Initial commit for FIFO pool * Change PoolType string to PoolFIFO bool * Remove redundant type * add PoolFIFO option to all clients Signed-off-by: monkey92t <golang@88.com> Co-authored-by: Kim Tae Kwon <taekwon.kim@shopee.com> Co-authored-by: monkey92t <golang@88.com>
This commit is contained in:
@ -57,6 +57,7 @@ type Options struct {
|
||||
Dialer func(context.Context) (net.Conn, error)
|
||||
OnClose func(*Conn) error
|
||||
|
||||
PoolFIFO bool
|
||||
PoolSize int
|
||||
MinIdleConns int
|
||||
MaxConnAge time.Duration
|
||||
@ -308,13 +309,21 @@ func (p *ConnPool) freeTurn() {
|
||||
}
|
||||
|
||||
func (p *ConnPool) popIdle() *Conn {
|
||||
if len(p.idleConns) == 0 {
|
||||
n := len(p.idleConns)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
idx := len(p.idleConns) - 1
|
||||
cn := p.idleConns[idx]
|
||||
p.idleConns = p.idleConns[:idx]
|
||||
var cn *Conn
|
||||
if p.opt.PoolFIFO {
|
||||
cn = p.idleConns[0]
|
||||
copy(p.idleConns, p.idleConns[1:])
|
||||
p.idleConns = p.idleConns[:n-1]
|
||||
} else {
|
||||
idx := n - 1
|
||||
cn = p.idleConns[idx]
|
||||
p.idleConns = p.idleConns[:idx]
|
||||
}
|
||||
p.idleConnsLen--
|
||||
p.checkMinIdleConns()
|
||||
return cn
|
||||
|
Reference in New Issue
Block a user