refactoring flush frame.shutdown path
Vladimir Bauer
2 years ago
| 356 | 356 | |
| 357 | 357 | func (s *pState) flush(cw *cwriter.Writer, height int) error { |
| 358 | 358 | var wg sync.WaitGroup |
| 359 | defer wg.Wait() // waiting for all s.hm.push to complete | |
| 359 | defer wg.Wait() // waiting for all s.push to complete | |
| 360 | 360 | |
| 361 | 361 | var popCount int |
| 362 | 362 | var rows []io.Reader |
| 380 | 380 | _, _ = io.Copy(io.Discard, row) |
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | if frame.shutdown != 0 && !frame.done { | |
| 383 | ||
| 384 | switch frame.shutdown { | |
| 385 | case 1: | |
| 386 | b.cancel() | |
| 384 | 387 | if qb, ok := s.queueBars[b]; ok { |
| 385 | b.cancel() | |
| 386 | 388 | delete(s.queueBars, b) |
| 387 | 389 | qb.priority = b.priority |
| 388 | 390 | wg.Add(1) |
| 389 | go func(b *Bar) { | |
| 390 | s.hm.push(b, true) | |
| 391 | wg.Done() | |
| 392 | }(qb) | |
| 393 | continue | |
| 394 | } | |
| 391 | go s.push(&wg, qb, true) | |
| 392 | } else if s.popCompleted && !frame.noPop { | |
| 393 | b.priority = s.popPriority | |
| 394 | s.popPriority++ | |
| 395 | wg.Add(1) | |
| 396 | go s.push(&wg, b, false) | |
| 397 | } else if !frame.rmOnComplete { | |
| 398 | wg.Add(1) | |
| 399 | go s.push(&wg, b, false) | |
| 400 | } | |
| 401 | case 2: | |
| 395 | 402 | if s.popCompleted && !frame.noPop { |
| 396 | switch frame.shutdown { | |
| 397 | case 1: | |
| 398 | b.priority = s.popPriority | |
| 399 | s.popPriority++ | |
| 400 | default: | |
| 401 | b.cancel() | |
| 402 | popCount += usedRows | |
| 403 | continue | |
| 404 | } | |
| 405 | } else if frame.rmOnComplete { | |
| 406 | b.cancel() | |
| 407 | continue | |
| 408 | } else { | |
| 409 | b.cancel() | |
| 410 | } | |
| 411 | } | |
| 412 | wg.Add(1) | |
| 413 | go func(b *Bar) { | |
| 414 | s.hm.push(b, false) | |
| 415 | wg.Done() | |
| 416 | }(b) | |
| 403 | popCount += usedRows | |
| 404 | } | |
| 405 | default: | |
| 406 | wg.Add(1) | |
| 407 | go s.push(&wg, b, false) | |
| 408 | } | |
| 417 | 409 | } |
| 418 | 410 | |
| 419 | 411 | for i := len(rows) - 1; i >= 0; i-- { |
| 424 | 416 | } |
| 425 | 417 | |
| 426 | 418 | return cw.Flush(len(rows) - popCount) |
| 419 | } | |
| 420 | ||
| 421 | func (s pState) push(wg *sync.WaitGroup, b *Bar, sync bool) { | |
| 422 | s.hm.push(b, sync) | |
| 423 | wg.Done() | |
| 427 | 424 | } |
| 428 | 425 | |
| 429 | 426 | func (s pState) makeBarState(total int64, filler BarFiller, options ...BarOption) *bState { |