better ETA computation
Vladimir Bauer
9 years ago
| 80 | 80 | |
| 81 | 81 | func newBar(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) *Bar { |
| 82 | 82 | b := &Bar{ |
| 83 | stateReqCh: make(chan chan state, 1), | |
| 83 | stateReqCh: make(chan chan state), | |
| 84 | 84 | widthCh: make(chan int), |
| 85 | 85 | formatCh: make(chan string), |
| 86 | 86 | etaAlphaCh: make(chan float64), |
| 254 | 254 | if isClosed(b.done) { |
| 255 | 255 | return b.state |
| 256 | 256 | } |
| 257 | ch := make(chan state, 1) | |
| 257 | ch := make(chan state) | |
| 258 | 258 | b.stateReqCh <- ch |
| 259 | 259 | return <-ch |
| 260 | 260 | } |
| 262 | 262 | func (b *Bar) server(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) { |
| 263 | 263 | var completed bool |
| 264 | 264 | timeStarted := time.Now() |
| 265 | blockStartTime := timeStarted | |
| 265 | prevStartTime := timeStarted | |
| 266 | var blockStartTime time.Time | |
| 266 | 267 | barState := state{ |
| 267 | 268 | id: id, |
| 268 | 269 | width: width, |
| 282 | 283 | for { |
| 283 | 284 | select { |
| 284 | 285 | case i := <-b.incrCh: |
| 286 | blockStartTime = time.Now() | |
| 285 | 287 | n := barState.current + i |
| 286 | 288 | if total > 0 && n > total { |
| 287 | 289 | barState.current = total |
| 288 | 290 | completed = true |
| 289 | blockStartTime = time.Now() | |
| 290 | 291 | break // break out of select |
| 291 | 292 | } |
| 292 | 293 | barState.timeElapsed = time.Since(timeStarted) |
| 293 | barState.timePerItem = calcTimePerItemEstimate(barState.timePerItem, blockStartTime, barState.etaAlpha, i) | |
| 294 | barState.timePerItem = calcTimePerItemEstimate(barState.timePerItem, prevStartTime, barState.etaAlpha, i) | |
| 294 | 295 | if n == total { |
| 295 | 296 | completed = true |
| 296 | 297 | } |
| 297 | 298 | barState.current = n |
| 298 | blockStartTime = time.Now() | |
| 299 | prevStartTime = blockStartTime | |
| 299 | 300 | case d := <-b.decoratorCh: |
| 300 | 301 | switch d.kind { |
| 301 | 302 | case decAppend: |