drop shutdownListeners slice from bState struct
Instead traverse once at shutdown.
Vladimir Bauer
1 year, 10 months ago
| 30 | 30 | |
| 31 | 31 | // bState is actual bar's state. |
| 32 | 32 | 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 | |
| 54 | 53 | } |
| 55 | 54 | |
| 56 | 55 | type renderFrame struct { |
| 400 | 399 | } |
| 401 | 400 | |
| 402 | 401 | 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 | } | |
| 403 | 413 | for { |
| 404 | 414 | select { |
| 405 | 415 | case op := <-b.operateState: |
| 406 | 416 | op(bs) |
| 407 | 417 | 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]) | |
| 416 | 420 | bs.aborted = !bs.completed() |
| 417 | 421 | b.bs = bs |
| 418 | 422 | close(b.bsOk) |
| 563 | 567 | if d, ok := d.(decor.EwmaDecorator); ok { |
| 564 | 568 | s.ewmaDecorators = append(s.ewmaDecorators, d) |
| 565 | 569 | } |
| 566 | if d, ok := d.(decor.ShutdownListener); ok { | |
| 567 | s.shutdownListeners = append(s.shutdownListeners, d) | |
| 568 | } | |
| 569 | 570 | } |
| 570 | 571 | } |
| 571 | 572 | |