Skip to content

Commit

Permalink
feat: rename IdleConnTTL to BlockingPoolCleanup and improve test cove…
Browse files Browse the repository at this point in the history
…rage

Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian committed Nov 26, 2024
1 parent d19c2f6 commit cde3c52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func newMux(dst string, option *ClientOption, init, dead wire, wireFn wireFn, wi
m.wire[i].Store(init)
}

m.dpool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireFn)
m.spool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireNoBgFn)
m.dpool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireFn)
m.spool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireNoBgFn)
return m
}

Expand Down
55 changes: 26 additions & 29 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ func newPool(cap int, dead wire, idleConnTTL time.Duration, minSize int, makeFn
}

return &pool{
size: 0,
minSize: minSize,
cap: cap,
dead: dead,
make: makeFn,
list: make([]wire, 0, 4),
cond: sync.NewCond(&sync.Mutex{}),
idleConnTTL: idleConnTTL,
size: 0,
minSize: minSize,
cap: cap,
dead: dead,
make: makeFn,
list: make([]wire, 0, 4),
cond: sync.NewCond(&sync.Mutex{}),
cleanup: idleConnTTL,
}
}

type pool struct {
dead wire
cond *sync.Cond
make func() wire
list []wire
size int
minSize int
cap int
down bool
idleConnTTL time.Duration
timer *time.Timer
timerIsActive bool
dead wire
cond *sync.Cond
timer *time.Timer
make func() wire
list []wire
cleanup time.Duration
size int
minSize int
cap int
down bool
timerOn bool
}

func (p *pool) Acquire() (v wire) {
Expand All @@ -49,6 +49,7 @@ func (p *pool) Acquire() (v wire) {
} else {
i := len(p.list) - 1
v = p.list[i]
p.list[i] = nil
p.list = p.list[:i]
}
p.cond.L.Unlock()
Expand Down Expand Up @@ -80,26 +81,22 @@ func (p *pool) Close() {
}

func (p *pool) startTimerIfNeeded() {
if p.idleConnTTL == 0 || p.timerIsActive || len(p.list) <= p.minSize {
if p.cleanup == 0 || p.timerOn || len(p.list) <= p.minSize {
return
}

p.timerIsActive = true
p.timerOn = true
if p.timer == nil {
p.timer = time.AfterFunc(p.idleConnTTL, p.removeIdleConns)
p.timer = time.AfterFunc(p.cleanup, p.removeIdleConns)
} else {
p.timer.Reset(p.idleConnTTL)
p.timer.Reset(p.cleanup)
}
}

func (p *pool) removeIdleConns() {
p.cond.L.Lock()
defer p.cond.L.Unlock()

if p.down || len(p.list) <= p.minSize {
return
}

newLen := min(p.minSize, len(p.list))
for i, w := range p.list[newLen:] {
w.Close()
Expand All @@ -108,11 +105,11 @@ func (p *pool) removeIdleConns() {
}

p.list = p.list[:newLen]
p.timerIsActive = false
p.timerOn = false
}

func (p *pool) stopTimer() {
p.timerIsActive = false
p.timerOn = false
if p.timer != nil {
p.timer.Stop()
}
Expand Down
10 changes: 5 additions & 5 deletions rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ type ClientOption struct {
// WriteBufferEachConn is the size of the bufio.NewWriterSize for each connection, default to DefaultWriteBuffer (0.5 MiB).
WriteBufferEachConn int

// IdleConnTTL is the duration for which a connection will be closed if it is idle.
// If IdleConnTTL is 0, then idle connections will not be closed.
IdleConnTTL time.Duration
// BlockingPoolCleanup is the duration for cleaning up idle connections.
// If BlockingPoolCleanup is 0, then idle connections will not be cleaned up.
BlockingPoolCleanup time.Duration
// BlockingPoolMinSize is the minimum size of the connection pool
// shared by blocking commands (ex BLPOP, XREAD with BLOCK).
// Only relevant if IdleConnTTL is not 0. This parameter limits
// the number of idle connections that can be removed by TTL.
// Only relevant if BlockingPoolCleanup is not 0. This parameter limits
// the number of idle connections that can be removed by BlockingPoolCleanup.
BlockingPoolMinSize int

// BlockingPoolSize is the size of the connection pool shared by blocking commands (ex BLPOP, XREAD with BLOCK).
Expand Down

0 comments on commit cde3c52

Please sign in to comment.