BasicProgresssBar: protect add with a lock
to avoid weird race conditions.
This commit is contained in:
parent
83c713c0df
commit
611610c89d
|
@ -31,8 +31,8 @@ type ProgressBar interface {
|
|||
// StackableProgressBar will clean itself to a default
|
||||
// state.
|
||||
type StackableProgressBar struct {
|
||||
mtx sync.Mutex // locks in Start & Finish
|
||||
BasicProgressBar
|
||||
mtx sync.Mutex // locks in Start, Finish, Add & NewProxyReader
|
||||
Bar BasicProgressBar
|
||||
items int32
|
||||
total int64
|
||||
|
||||
|
@ -42,10 +42,10 @@ type StackableProgressBar struct {
|
|||
var _ ProgressBar = new(StackableProgressBar)
|
||||
|
||||
func (spb *StackableProgressBar) start() {
|
||||
spb.BasicProgressBar.ProgressBar = pb.New(0)
|
||||
spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES)
|
||||
spb.Bar.ProgressBar = pb.New(0)
|
||||
spb.Bar.ProgressBar.SetUnits(pb.U_BYTES)
|
||||
|
||||
spb.BasicProgressBar.ProgressBar.Start()
|
||||
spb.Bar.ProgressBar.Start()
|
||||
spb.started = true
|
||||
}
|
||||
|
||||
|
@ -58,13 +58,25 @@ func (spb *StackableProgressBar) Start(total int64) {
|
|||
if !spb.started {
|
||||
spb.start()
|
||||
}
|
||||
spb.SetTotal64(spb.total)
|
||||
spb.Bar.SetTotal64(spb.total)
|
||||
spb.prefix()
|
||||
spb.mtx.Unlock()
|
||||
}
|
||||
|
||||
func (spb *StackableProgressBar) Add(total int64) {
|
||||
spb.mtx.Lock()
|
||||
defer spb.mtx.Unlock()
|
||||
spb.Bar.Add(total)
|
||||
}
|
||||
|
||||
func (spb *StackableProgressBar) NewProxyReader(r io.Reader) io.Reader {
|
||||
spb.mtx.Lock()
|
||||
defer spb.mtx.Unlock()
|
||||
return spb.Bar.NewProxyReader(r)
|
||||
}
|
||||
|
||||
func (spb *StackableProgressBar) prefix() {
|
||||
spb.BasicProgressBar.ProgressBar.Prefix(fmt.Sprintf("%d items: ", atomic.LoadInt32(&spb.items)))
|
||||
spb.Bar.ProgressBar.Prefix(fmt.Sprintf("%d items: ", atomic.LoadInt32(&spb.items)))
|
||||
}
|
||||
|
||||
func (spb *StackableProgressBar) Finish() {
|
||||
|
@ -74,8 +86,8 @@ func (spb *StackableProgressBar) Finish() {
|
|||
spb.items--
|
||||
if spb.items == 0 {
|
||||
// slef cleanup
|
||||
spb.BasicProgressBar.ProgressBar.Finish()
|
||||
spb.BasicProgressBar.ProgressBar = nil
|
||||
spb.Bar.ProgressBar.Finish()
|
||||
spb.Bar.ProgressBar = nil
|
||||
spb.started = false
|
||||
spb.total = 0
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue