fix panic: close of closed channel
Vladimir Bauer
9 years ago
| 170 | 170 | func (p *Progress) Stop() { |
| 171 | 171 | p.wg.Wait() |
| 172 | 172 | if !p.isDone() { |
| 173 | close(p.done) | |
| 174 | 173 | close(p.operationCh) |
| 175 | 174 | } |
| 176 | 175 | } |
| 186 | 185 | |
| 187 | 186 | // server monitors underlying channels and renders any progress bars |
| 188 | 187 | func (p *Progress) server(cw *cwriter.Writer, t *time.Ticker) { |
| 188 | defer func() { | |
| 189 | t.Stop() | |
| 190 | close(p.done) | |
| 191 | }() | |
| 189 | 192 | const numDrawers = 4 |
| 190 | 193 | bars := make([]*Bar, 0, 4) |
| 191 | 194 | for { |
| 195 | 198 | cw = cwriter.New(w) |
| 196 | 199 | case op, ok := <-p.operationCh: |
| 197 | 200 | if !ok { |
| 198 | t.Stop() | |
| 199 | 201 | return |
| 200 | 202 | } |
| 201 | 203 | switch op.kind { |
| 257 | 259 | t.Stop() |
| 258 | 260 | t = time.NewTicker(d) |
| 259 | 261 | case <-p.ctx.Done(): |
| 260 | t.Stop() | |
| 261 | close(p.done) | |
| 262 | 262 | return |
| 263 | 263 | } |
| 264 | 264 | } |