Skip to content

Commit 35f4704

Browse files
author
zhaohuiliu
committed
close timer when possible
1 parent 26b9e29 commit 35f4704

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

pipe.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type pipe struct {
9090
r2ps bool // identify this pipe is used for resp2 pubsub or not
9191
noNoDelay bool
9292
optin bool
93+
pingTimer *time.Timer
9394
}
9495

9596
type pipeFn func(ctx context.Context, connFn func(ctx context.Context) (net.Conn, error), option *ClientOption) (p *pipe, err error)
@@ -630,17 +631,18 @@ func (p *pipe) _backgroundRead() (err error) {
630631

631632
func (p *pipe) backgroundPing() {
632633
var prev, recv int32
633-
var timer *time.Timer
634634

635635
prev = atomic.LoadInt32(&p.recvs)
636-
timer = time.AfterFunc(p.pinggap, func() {
636+
p.pingTimer = time.AfterFunc(p.pinggap, func() {
637637
var err error
638638
recv = atomic.LoadInt32(&p.recvs)
639639
reset := false
640640
defer func(){
641641
prev = atomic.LoadInt32(&p.recvs)
642-
if reset && timer != nil {
643-
timer.Reset(p.pinggap)
642+
if reset {
643+
p.pingTimer.Reset(p.pinggap)
644+
} else {
645+
p.pingTimer.Stop()
644646
}
645647
}()
646648
if recv != prev || atomic.LoadInt32(&p.blcksig) != 0 || (atomic.LoadInt32(&p.state) == 0 && atomic.LoadInt32(&p.waits) != 0) {
@@ -654,7 +656,7 @@ func (p *pipe) backgroundPing() {
654656
case <-tm.C:
655657
err = os.ErrDeadlineExceeded
656658
case err = <-ch:
657-
// noop
659+
tm.Stop()
658660
}
659661
if err != nil && atomic.LoadInt32(&p.blcksig) != 0 {
660662
err = nil
@@ -1595,6 +1597,9 @@ func (p *pipe) Close() {
15951597
}
15961598
atomic.AddInt32(&p.waits, -1)
15971599
atomic.AddInt32(&p.blcksig, -1)
1600+
if p.pingTimer != nil {
1601+
p.pingTimer.Stop()
1602+
}
15981603
if p.conn != nil {
15991604
p.conn.Close()
16001605
}

0 commit comments

Comments
 (0)