Skip to content

Commit

Permalink
makeTotalBar: revert to AverageSpeed
Browse files Browse the repository at this point in the history
EwmaSpeed is not appropriate way to measure sum speed of all bars.
  • Loading branch information
vbauerster committed Apr 16, 2024
1 parent decf1bb commit 097abed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions part.go
Expand Up @@ -39,7 +39,7 @@ type Part struct {
single bool
progress *mpb.Progress
dlogger *log.Logger
totalBarIncr func(int, time.Duration)
totalBarIncr func(int)
}

type flashBar struct {
Expand Down Expand Up @@ -326,7 +326,7 @@ func (p *Part) download(client *http.Client, req *http.Request, timeout, sleep t
if p.total() <= 0 {
bar.SetTotal(p.Written, false)
} else {
p.totalBarIncr(n, dur)
p.totalBarIncr(n)
}
bar.EwmaIncrBy(n, dur)
<-sleepCtx.Done()
Expand Down
20 changes: 8 additions & 12 deletions session.go
Expand Up @@ -181,9 +181,9 @@ func (s Session) makeTotalBar(
progress *mpb.Progress,
doneCount *uint32,
quiet bool,
) (func(int, time.Duration), func(bool), error) {
) (func(int), func(bool), error) {
if len(s.Parts) <= 1 || quiet {
return func(int, time.Duration) {}, func(bool) {}, nil
return func(int) {}, func(bool) {}, nil
}
bar, err := progress.Add(s.ContentLength,
distinctRefiller(baseBarStyle()).Build(),
Expand All @@ -201,7 +201,7 @@ func (s Session) makeTotalBar(
),
mpb.AppendDecorators(
decor.OnCompleteOrOnAbort(decor.AverageETA(decor.ET_STYLE_MMSS, decor.WCSyncWidth), ":"),
decor.EwmaSpeed(decor.SizeB1024(0), "%.1f", 30, decor.WCSyncSpace),
decor.AverageSpeed(decor.SizeB1024(0), "%.1f", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
),
Expand All @@ -214,20 +214,16 @@ func (s Session) makeTotalBar(
bar.SetRefill(written)
bar.DecoratorAverageAdjust(time.Now().Add(-s.Elapsed))
}
type ewmaPayload struct {
n int
d time.Duration
}
ch := make(chan ewmaPayload, len(s.Parts)-int(atomic.LoadUint32(doneCount)))
ch := make(chan int, len(s.Parts)-int(atomic.LoadUint32(doneCount)))
ctx, cancel := context.WithCancel(ctx)
dropCtx, dropCancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer close(done)
for {
select {
case p := <-ch:
bar.EwmaIncrBy(p.n, p.d)
case n := <-ch:
bar.IncrBy(n)
case <-dropCtx.Done():
cancel()
bar.Abort(true)
Expand All @@ -239,9 +235,9 @@ func (s Session) makeTotalBar(
}
}
}()
return func(n int, dur time.Duration) {
return func(n int) {
select {
case ch <- ewmaPayload{n, dur}:
case ch <- n:
case <-done:
}
}, func(drop bool) {
Expand Down

0 comments on commit 097abed

Please sign in to comment.