refactoring: barShutdownQueue
Vladimir Bauer
7 years ago
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | type pState struct { |
| 36 | bHeap *priorityQueue | |
| 37 | shutdownPending []*Bar | |
| 38 | heapUpdated bool | |
| 39 | idCounter int | |
| 40 | width int | |
| 41 | rr time.Duration | |
| 42 | pMatrix map[int][]chan int | |
| 43 | aMatrix map[int][]chan int | |
| 44 | output io.Writer | |
| 36 | bHeap *priorityQueue | |
| 37 | heapUpdated bool | |
| 38 | pMatrix map[int][]chan int | |
| 39 | aMatrix map[int][]chan int | |
| 40 | barShutdownQueue []chan struct{} | |
| 45 | 41 | |
| 46 | 42 | // following are provided/overrided by user |
| 43 | idCount int | |
| 44 | width int | |
| 45 | rr time.Duration | |
| 47 | 46 | uwg *sync.WaitGroup |
| 48 | 47 | manualRefreshCh <-chan time.Time |
| 49 | 48 | shutdownNotifier chan struct{} |
| 50 | 49 | parkedBars map[*Bar]*Bar |
| 50 | output io.Writer | |
| 51 | 51 | debugOut io.Writer |
| 52 | 52 | } |
| 53 | 53 | |
| 123 | 123 | bs := &bState{ |
| 124 | 124 | total: total, |
| 125 | 125 | filler: filler, |
| 126 | priority: ps.idCounter, | |
| 127 | id: ps.idCounter, | |
| 126 | priority: ps.idCount, | |
| 127 | id: ps.idCount, | |
| 128 | 128 | width: ps.width, |
| 129 | 129 | } |
| 130 | 130 | for _, opt := range options { |
| 137 | 137 | prefix := fmt.Sprintf("%sbar#%02d ", p.dlogger.Prefix(), bs.id) |
| 138 | 138 | bar.dlogger = log.New(ps.debugOut, prefix, log.Lshortfile) |
| 139 | 139 | if bs.runningBar != nil { |
| 140 | if bar.priority == ps.idCounter { | |
| 140 | if bar.priority == ps.idCount { | |
| 141 | 141 | bar.priority = bs.runningBar.priority |
| 142 | 142 | } |
| 143 | 143 | ps.parkedBars[bs.runningBar] = bar |
| 145 | 145 | heap.Push(ps.bHeap, bar) |
| 146 | 146 | ps.heapUpdated = true |
| 147 | 147 | } |
| 148 | ps.idCounter++ | |
| 148 | ps.idCount++ | |
| 149 | 149 | result <- bar |
| 150 | 150 | }: |
| 151 | 151 | return <-result |
| 168 | 168 | if remove { |
| 169 | 169 | s.heapUpdated = heap.Remove(s.bHeap, b.index) != nil |
| 170 | 170 | } |
| 171 | s.shutdownPending = append(s.shutdownPending, b) | |
| 171 | s.barShutdownQueue = append(s.barShutdownQueue, b.shutdown) | |
| 172 | 172 | }: |
| 173 | 173 | case <-p.done: |
| 174 | 174 | } |
| 268 | 268 | // shutdown at next flush, in other words decrement underlying WaitGroup |
| 269 | 269 | // only after the bar with completed state has been flushed. this |
| 270 | 270 | // ensures no bar ends up with less than 100% rendered. |
| 271 | s.shutdownPending = append(s.shutdownPending, bar) | |
| 271 | s.barShutdownQueue = append(s.barShutdownQueue, bar.shutdown) | |
| 272 | 272 | if parkedBar := s.parkedBars[bar]; parkedBar != nil { |
| 273 | 273 | heap.Push(s.bHeap, parkedBar) |
| 274 | 274 | s.heapUpdated = true |
| 285 | 285 | lineCount += bar.extendedLines + 1 |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | for i := len(s.shutdownPending) - 1; i >= 0; i-- { | |
| 289 | close(s.shutdownPending[i].shutdown) | |
| 290 | s.shutdownPending = s.shutdownPending[:i] | |
| 288 | for i := len(s.barShutdownQueue) - 1; i >= 0; i-- { | |
| 289 | close(s.barShutdownQueue[i]) | |
| 290 | s.barShutdownQueue = s.barShutdownQueue[:i] | |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | return cw.Flush(lineCount) |