Merge pull request #6788 from hashicorp/bar_add_lock
BasicProgresssBar: protect add/NewProxyReader with a lock
This commit is contained in:
commit
a26106055c
|
@ -31,8 +31,8 @@ type ProgressBar interface {
|
||||||
// StackableProgressBar will clean itself to a default
|
// StackableProgressBar will clean itself to a default
|
||||||
// state.
|
// state.
|
||||||
type StackableProgressBar struct {
|
type StackableProgressBar struct {
|
||||||
mtx sync.Mutex // locks in Start & Finish
|
mtx sync.Mutex // locks in Start, Finish, Add & NewProxyReader
|
||||||
BasicProgressBar
|
Bar BasicProgressBar
|
||||||
items int32
|
items int32
|
||||||
total int64
|
total int64
|
||||||
|
|
||||||
|
@ -42,10 +42,10 @@ type StackableProgressBar struct {
|
||||||
var _ ProgressBar = new(StackableProgressBar)
|
var _ ProgressBar = new(StackableProgressBar)
|
||||||
|
|
||||||
func (spb *StackableProgressBar) start() {
|
func (spb *StackableProgressBar) start() {
|
||||||
spb.BasicProgressBar.ProgressBar = pb.New(0)
|
spb.Bar.ProgressBar = pb.New(0)
|
||||||
spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES)
|
spb.Bar.ProgressBar.SetUnits(pb.U_BYTES)
|
||||||
|
|
||||||
spb.BasicProgressBar.ProgressBar.Start()
|
spb.Bar.ProgressBar.Start()
|
||||||
spb.started = true
|
spb.started = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,13 +58,25 @@ func (spb *StackableProgressBar) Start(total int64) {
|
||||||
if !spb.started {
|
if !spb.started {
|
||||||
spb.start()
|
spb.start()
|
||||||
}
|
}
|
||||||
spb.SetTotal64(spb.total)
|
spb.Bar.SetTotal64(spb.total)
|
||||||
spb.prefix()
|
spb.prefix()
|
||||||
spb.mtx.Unlock()
|
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() {
|
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() {
|
func (spb *StackableProgressBar) Finish() {
|
||||||
|
@ -74,8 +86,8 @@ func (spb *StackableProgressBar) Finish() {
|
||||||
spb.items--
|
spb.items--
|
||||||
if spb.items == 0 {
|
if spb.items == 0 {
|
||||||
// slef cleanup
|
// slef cleanup
|
||||||
spb.BasicProgressBar.ProgressBar.Finish()
|
spb.Bar.ProgressBar.Finish()
|
||||||
spb.BasicProgressBar.ProgressBar = nil
|
spb.Bar.ProgressBar = nil
|
||||||
spb.started = false
|
spb.started = false
|
||||||
spb.total = 0
|
spb.total = 0
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue