Codebase list golang-github-vbauerster-mpb / 655922b
drop shutdownListeners slice from bState struct Instead traverse once at shutdown. Vladimir Bauer 1 year, 10 months ago
1 changed file(s) with 33 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
3030
3131 // bState is actual bar's state.
3232 type bState struct {
33 id int
34 priority int
35 reqWidth int
36 shutdown int
37 total int64
38 current int64
39 refill int64
40 trimSpace bool
41 aborted bool
42 triggerComplete bool
43 rmOnComplete bool
44 noPop bool
45 autoRefresh bool
46 buffers [3]*bytes.Buffer
47 decorators [2][]decor.Decorator
48 ewmaDecorators []decor.EwmaDecorator
49 shutdownListeners []decor.ShutdownListener
50 filler BarFiller
51 extender extenderFunc
52 renderReq chan<- time.Time
53 waitBar *Bar // key for (*pState).queueBars
33 id int
34 priority int
35 reqWidth int
36 shutdown int
37 total int64
38 current int64
39 refill int64
40 trimSpace bool
41 aborted bool
42 triggerComplete bool
43 rmOnComplete bool
44 noPop bool
45 autoRefresh bool
46 buffers [3]*bytes.Buffer
47 decorators [2][]decor.Decorator
48 ewmaDecorators []decor.EwmaDecorator
49 filler BarFiller
50 extender extenderFunc
51 renderReq chan<- time.Time
52 waitBar *Bar // key for (*pState).queueBars
5453 }
5554
5655 type renderFrame struct {
400399 }
401400
402401 func (b *Bar) serve(bs *bState) {
402 decoratorsOnShutdown := func(decorators []decor.Decorator) {
403 for _, d := range decorators {
404 if d, ok := unwrap(d).(decor.ShutdownListener); ok {
405 b.container.bwg.Add(1)
406 go func() {
407 d.OnShutdown()
408 b.container.bwg.Done()
409 }()
410 }
411 }
412 }
403413 for {
404414 select {
405415 case op := <-b.operateState:
406416 op(bs)
407417 case <-b.ctx.Done():
408 for _, d := range bs.shutdownListeners {
409 b.container.bwg.Add(1)
410 d := d
411 go func() {
412 d.OnShutdown()
413 b.container.bwg.Done()
414 }()
415 }
418 decoratorsOnShutdown(bs.decorators[0])
419 decoratorsOnShutdown(bs.decorators[1])
416420 bs.aborted = !bs.completed()
417421 b.bs = bs
418422 close(b.bsOk)
563567 if d, ok := d.(decor.EwmaDecorator); ok {
564568 s.ewmaDecorators = append(s.ewmaDecorators, d)
565569 }
566 if d, ok := d.(decor.ShutdownListener); ok {
567 s.shutdownListeners = append(s.shutdownListeners, d)
568 }
569570 }
570571 }
571572