Codebase list golang-github-vbauerster-mpb / 339d222
no need for *pState.barShutdownQueue Vladimir Bauer 4 years ago
2 changed file(s) with 32 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
4141 lastIncrement int64
4242 trimSpace bool
4343 completed bool
44 completeFlushed bool
4544 aborted bool
4645 triggerComplete bool
4746 dropOnComplete bool
6463 type frame struct {
6564 reader io.Reader
6665 lines int
67 abort bool
68 complete bool
66 shutdown bool
6967 }
7068
7169 func newBar(container *Progress, bs *bState) *Bar {
337335 b.frameCh <- &frame{
338336 reader: reader,
339337 lines: lines + 1,
340 abort: s.aborted && !s.completeFlushed,
341 complete: s.completed && !s.completeFlushed,
342 }
343 s.completeFlushed = s.completed || s.aborted
338 shutdown: s.completed || s.aborted,
339 }
344340 }()
345341 if b.recoveredPanic == nil {
346342 reader = s.draw(stat)
528524 Total: s.total,
529525 Current: s.current,
530526 Refill: s.refill,
531 Completed: s.completeFlushed && !s.aborted,
527 Completed: s.completed,
532528 Aborted: s.aborted,
533529 }
534530 }
3232
3333 // pState holds bars in its priorityQueue, it gets passed to (*Progress).serve monitor goroutine.
3434 type pState struct {
35 bHeap priorityQueue
36 heapUpdated bool
37 pMatrix map[int][]chan int
38 aMatrix map[int][]chan int
39 barShutdownQueue []*Bar
35 bHeap priorityQueue
36 heapUpdated bool
37 pMatrix map[int][]chan int
38 aMatrix map[int][]chan int
4039
4140 // following are provided/overrided by user
4241 idCount int
259258
260259 func (s *pState) flush(cw *cwriter.Writer) error {
261260 var totalLines int
262 bm := make(map[*Bar]*frame, s.bHeap.Len())
261 pool := make([]*Bar, 0, s.bHeap.Len())
263262 for s.bHeap.Len() > 0 {
264263 b := heap.Pop(&s.bHeap).(*Bar)
265264 frame := <-b.frameCh
265 totalLines += frame.lines
266266 _, err := cw.ReadFrom(frame.reader)
267267 if err != nil {
268268 return err
269269 }
270 if frame.complete {
271 // shutdown at next flush
272 // this ensures no bar ends up with less than 100% rendered
273 defer func() {
274 s.barShutdownQueue = append(s.barShutdownQueue, b)
275 }()
276 } else if frame.abort {
277 s.barShutdownQueue = append(s.barShutdownQueue, b)
278 }
279 totalLines += frame.lines
280 bm[b] = frame
281 }
282
283 for _, b := range s.barShutdownQueue {
284 b.cancel()
285 <-b.done // waiting for b.done, so it's safe to read b.bs
286 var toDrop bool
287 if qb, ok := s.queueBars[b]; ok {
288 delete(s.queueBars, b)
289 qb.priority = b.priority
290 heap.Push(&s.bHeap, qb)
291 toDrop = true
292 } else if s.popCompleted && !b.bs.noPop {
293 frame := bm[b]
294 totalLines -= frame.lines
295 toDrop = true
296 }
297 if toDrop || b.bs.dropOnComplete {
298 delete(bm, b)
299 s.heapUpdated = true
300 }
301 }
302 s.barShutdownQueue = s.barShutdownQueue[0:0]
303
304 for b := range bm {
270 if frame.shutdown {
271 b.cancel()
272 <-b.done // waiting for b.done, so it's safe to read b.bs
273 var toDrop bool
274 if qb, ok := s.queueBars[b]; ok {
275 delete(s.queueBars, b)
276 qb.priority = b.priority
277 pool = append(pool, qb)
278 toDrop = true
279 } else if s.popCompleted && !b.bs.noPop {
280 totalLines -= frame.lines
281 toDrop = true
282 }
283 if toDrop || b.bs.dropOnComplete {
284 s.heapUpdated = true
285 continue
286 }
287 }
288 pool = append(pool, b)
289 }
290
291 for _, b := range pool {
305292 heap.Push(&s.bHeap, b)
306293 }
307294