Skip to content

Commit d10d092

Browse files
committed
better ETA computation
1 parent 11dd99a commit d10d092

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

bar.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type (
8181

8282
func newBar(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) *Bar {
8383
b := &Bar{
84-
stateReqCh: make(chan chan state, 1),
84+
stateReqCh: make(chan chan state),
8585
widthCh: make(chan int),
8686
formatCh: make(chan string),
8787
etaAlphaCh: make(chan float64),
@@ -255,15 +255,16 @@ func (b *Bar) getState() state {
255255
if isClosed(b.done) {
256256
return b.state
257257
}
258-
ch := make(chan state, 1)
258+
ch := make(chan state)
259259
b.stateReqCh <- ch
260260
return <-ch
261261
}
262262

263263
func (b *Bar) server(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) {
264264
var completed bool
265265
timeStarted := time.Now()
266-
blockStartTime := timeStarted
266+
prevStartTime := timeStarted
267+
var blockStartTime time.Time
267268
barState := state{
268269
id: id,
269270
width: width,
@@ -283,20 +284,20 @@ func (b *Bar) server(id int, total int64, width int, format string, wg *sync.Wai
283284
for {
284285
select {
285286
case i := <-b.incrCh:
287+
blockStartTime = time.Now()
286288
n := barState.current + i
287289
if total > 0 && n > total {
288290
barState.current = total
289291
completed = true
290-
blockStartTime = time.Now()
291292
break // break out of select
292293
}
293294
barState.timeElapsed = time.Since(timeStarted)
294-
barState.timePerItem = calcTimePerItemEstimate(barState.timePerItem, blockStartTime, barState.etaAlpha, i)
295+
barState.timePerItem = calcTimePerItemEstimate(barState.timePerItem, prevStartTime, barState.etaAlpha, i)
295296
if n == total {
296297
completed = true
297298
}
298299
barState.current = n
299-
blockStartTime = time.Now()
300+
prevStartTime = blockStartTime
300301
case d := <-b.decoratorCh:
301302
switch d.kind {
302303
case decAppend:

0 commit comments

Comments
 (0)