Skip to content

Commit 0c9229b

Browse files
committed
fix: resolve the progress bar stuck when build 10k files
Signed-off-by: chlins <chlins.zhang@gmail.com>
1 parent 3a2aab2 commit 0c9229b

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

internal/pb/pb.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"sync"
23+
"time"
2324

2425
humanize "github.com/dustin/go-humanize"
2526
mpbv8 "github.com/vbauerster/mpb/v8"
@@ -47,7 +48,7 @@ type progressBar struct {
4748
// NewProgressBar creates a new progress bar.
4849
func NewProgressBar() *ProgressBar {
4950
return &ProgressBar{
50-
mpb: mpbv8.New(mpbv8.WithWidth(60)),
51+
mpb: mpbv8.New(mpbv8.WithWidth(60), mpbv8.WithRefreshRate(300*time.Millisecond)),
5152
bars: make(map[string]*progressBar),
5253
}
5354
}
@@ -62,21 +63,14 @@ func (p *ProgressBar) Add(prompt, name string, size int64, reader io.Reader) io.
6263
return reader
6364
}
6465

66+
newBar := &progressBar{size: size, msg: fmt.Sprintf("%s %s", prompt, name)}
6567
// Create a new bar if it does not exist.
66-
bar := p.mpb.New(size,
68+
newBar.Bar = p.mpb.New(size,
6769
mpbv8.BarStyle(),
6870
mpbv8.BarFillerOnComplete("|"),
6971
mpbv8.PrependDecorators(
7072
decor.Any(func(s decor.Statistics) string {
71-
p.mu.RLock()
72-
defer p.mu.RUnlock()
73-
74-
bar, ok := p.bars[name]
75-
if ok && bar.msg != "" {
76-
return bar.msg
77-
}
78-
79-
return fmt.Sprintf("%s %s", prompt, name)
73+
return newBar.msg
8074
}, decor.WCSyncSpaceR),
8175
),
8276
mpbv8.AppendDecorators(
@@ -89,10 +83,10 @@ func (p *ProgressBar) Add(prompt, name string, size int64, reader io.Reader) io.
8983
)
9084

9185
p.mu.Lock()
92-
p.bars[name] = &progressBar{Bar: bar, size: size}
86+
p.bars[name] = newBar
9387
p.mu.Unlock()
9488

95-
return bar.ProxyReader(reader)
89+
return newBar.Bar.ProxyReader(reader)
9690
}
9791

9892
// Complete completes the progress bar.

0 commit comments

Comments
 (0)