Skip to content

Commit

Permalink
Closing ticker.Stop() to avoid leak and allow to restart the listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
henvic committed May 19, 2016
1 parent f0722e1 commit 528664d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Progress struct {

lw *uilive.Writer
ticker *time.Ticker
tdone chan bool
mtx *sync.RWMutex
}

Expand Down Expand Up @@ -86,14 +87,19 @@ func (p *Progress) AddBar(total int) *Bar {
func (p *Progress) Listen() {
p.lw.Out = p.Out

go func() {
for _ = range p.ticker.C {
for {
select {
case <-p.ticker.C:
p.mtx.RLock()
p.print()
p.lw.Flush()
p.mtx.RUnlock()
case <-p.tdone:
p.ticker.Stop()
p.ticker = nil
return
}
}()
}
}

func (p *Progress) print() {
Expand All @@ -107,6 +113,7 @@ func (p *Progress) Start() {
p.mtx.Lock()
if p.ticker == nil {
p.ticker = time.NewTicker(RefreshInterval)
p.tdone = make(chan bool, 1)
}
p.mtx.Unlock()

Expand All @@ -116,8 +123,7 @@ func (p *Progress) Start() {
// Stop stops listening
func (p *Progress) Stop() {
p.mtx.Lock()
p.ticker.Stop()
p.ticker = nil
close(p.tdone)
p.print()
p.lw.Flush()
p.mtx.Unlock()
Expand Down

0 comments on commit 528664d

Please sign in to comment.