Skip to content

Commit 7cd0a8b

Browse files
committed
fix race
1 parent 4284b81 commit 7cd0a8b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

ticker.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewTicker() (ot *Ticker) {
2323
stopCh: make(chan struct{}),
2424
doneCh: make(chan struct{}),
2525
}
26-
go ot.run()
26+
go ot.run(ot.stopCh)
2727
return
2828
}
2929

@@ -63,7 +63,7 @@ func (ot *Ticker) WaitCh() (ch <-chan struct{}) {
6363
return
6464
}
6565

66-
func (ot *Ticker) run() {
66+
func (ot *Ticker) run(stopCh chan struct{}) {
6767
defer func() {
6868
close(ot.ch)
6969
close(ot.doneCh)
@@ -75,12 +75,13 @@ func (ot *Ticker) run() {
7575
for {
7676
select {
7777
case <-tckr.C:
78+
newCh := make(chan struct{})
7879
ot.mu.Lock()
7980
oldCh := ot.ch
80-
ot.ch = make(chan struct{})
81+
ot.ch = newCh
8182
ot.mu.Unlock()
8283
close(oldCh)
83-
case <-ot.stopCh:
84+
case <-stopCh:
8485
return
8586
}
8687
}

0 commit comments

Comments
 (0)