Skip to content

Commit cde3c52

Browse files
committed
feat: rename IdleConnTTL to BlockingPoolCleanup and improve test coverage
Signed-off-by: Rueian <[email protected]>
1 parent d19c2f6 commit cde3c52

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

mux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func newMux(dst string, option *ClientOption, init, dead wire, wireFn wireFn, wi
100100
m.wire[i].Store(init)
101101
}
102102

103-
m.dpool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireFn)
104-
m.spool = newPool(option.BlockingPoolSize, dead, option.IdleConnTTL, option.BlockingPoolMinSize, wireNoBgFn)
103+
m.dpool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireFn)
104+
m.spool = newPool(option.BlockingPoolSize, dead, option.BlockingPoolCleanup, option.BlockingPoolMinSize, wireNoBgFn)
105105
return m
106106
}
107107

pool.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ func newPool(cap int, dead wire, idleConnTTL time.Duration, minSize int, makeFn
1111
}
1212

1313
return &pool{
14-
size: 0,
15-
minSize: minSize,
16-
cap: cap,
17-
dead: dead,
18-
make: makeFn,
19-
list: make([]wire, 0, 4),
20-
cond: sync.NewCond(&sync.Mutex{}),
21-
idleConnTTL: idleConnTTL,
14+
size: 0,
15+
minSize: minSize,
16+
cap: cap,
17+
dead: dead,
18+
make: makeFn,
19+
list: make([]wire, 0, 4),
20+
cond: sync.NewCond(&sync.Mutex{}),
21+
cleanup: idleConnTTL,
2222
}
2323
}
2424

2525
type pool struct {
26-
dead wire
27-
cond *sync.Cond
28-
make func() wire
29-
list []wire
30-
size int
31-
minSize int
32-
cap int
33-
down bool
34-
idleConnTTL time.Duration
35-
timer *time.Timer
36-
timerIsActive bool
26+
dead wire
27+
cond *sync.Cond
28+
timer *time.Timer
29+
make func() wire
30+
list []wire
31+
cleanup time.Duration
32+
size int
33+
minSize int
34+
cap int
35+
down bool
36+
timerOn bool
3737
}
3838

3939
func (p *pool) Acquire() (v wire) {
@@ -49,6 +49,7 @@ func (p *pool) Acquire() (v wire) {
4949
} else {
5050
i := len(p.list) - 1
5151
v = p.list[i]
52+
p.list[i] = nil
5253
p.list = p.list[:i]
5354
}
5455
p.cond.L.Unlock()
@@ -80,26 +81,22 @@ func (p *pool) Close() {
8081
}
8182

8283
func (p *pool) startTimerIfNeeded() {
83-
if p.idleConnTTL == 0 || p.timerIsActive || len(p.list) <= p.minSize {
84+
if p.cleanup == 0 || p.timerOn || len(p.list) <= p.minSize {
8485
return
8586
}
8687

87-
p.timerIsActive = true
88+
p.timerOn = true
8889
if p.timer == nil {
89-
p.timer = time.AfterFunc(p.idleConnTTL, p.removeIdleConns)
90+
p.timer = time.AfterFunc(p.cleanup, p.removeIdleConns)
9091
} else {
91-
p.timer.Reset(p.idleConnTTL)
92+
p.timer.Reset(p.cleanup)
9293
}
9394
}
9495

9596
func (p *pool) removeIdleConns() {
9697
p.cond.L.Lock()
9798
defer p.cond.L.Unlock()
9899

99-
if p.down || len(p.list) <= p.minSize {
100-
return
101-
}
102-
103100
newLen := min(p.minSize, len(p.list))
104101
for i, w := range p.list[newLen:] {
105102
w.Close()
@@ -108,11 +105,11 @@ func (p *pool) removeIdleConns() {
108105
}
109106

110107
p.list = p.list[:newLen]
111-
p.timerIsActive = false
108+
p.timerOn = false
112109
}
113110

114111
func (p *pool) stopTimer() {
115-
p.timerIsActive = false
112+
p.timerOn = false
116113
if p.timer != nil {
117114
p.timer.Stop()
118115
}

rueidis.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ type ClientOption struct {
133133
// WriteBufferEachConn is the size of the bufio.NewWriterSize for each connection, default to DefaultWriteBuffer (0.5 MiB).
134134
WriteBufferEachConn int
135135

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

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

0 commit comments

Comments
 (0)