fix panic of the future using a mutex instead of some atomic calls + more docs

This commit is contained in:
Adrien Delorme 2018-09-06 18:12:15 +02:00
parent 8a851efcc8
commit 0ac7b64364
1 changed files with 33 additions and 21 deletions

View File

@ -24,17 +24,19 @@ type ProgressBar interface {
// will display the number of current loadings. // will display the number of current loadings.
// Every call to Start will add total to an internal // Every call to Start will add total to an internal
// total that is the total displayed. // total that is the total displayed.
// // First call to Start will start a goroutine
// that is waiting for every download to be finished.
// Last call to Finish triggers a cleanup.
// When all active downloads are finished // When all active downloads are finished
// StackableProgressBar will clean itself to a default // StackableProgressBar will clean itself to a default
// state. // state.
type StackableProgressBar struct { type StackableProgressBar struct {
items int32 mtx sync.Mutex // locks in Start & Finish
total int64
started bool
BasicProgressBar BasicProgressBar
startOnce sync.Once items int32
group sync.WaitGroup total int64
started bool
} }
var _ ProgressBar = new(StackableProgressBar) var _ ProgressBar = new(StackableProgressBar)
@ -44,21 +46,21 @@ func (spb *StackableProgressBar) start() {
spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES) spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES)
spb.BasicProgressBar.ProgressBar.Start() spb.BasicProgressBar.ProgressBar.Start()
go func() { spb.started = true
spb.group.Wait()
spb.BasicProgressBar.ProgressBar.Finish()
spb.startOnce = sync.Once{}
spb.BasicProgressBar.ProgressBar = nil
}()
} }
func (spb *StackableProgressBar) Start(total int64) { func (spb *StackableProgressBar) Start(total int64) {
atomic.AddInt64(&spb.total, total) spb.mtx.Lock()
atomic.AddInt32(&spb.items, 1)
spb.group.Add(1) spb.total += total
spb.startOnce.Do(spb.start) spb.items++
spb.SetTotal64(atomic.LoadInt64(&spb.total))
if !spb.started {
spb.start()
}
spb.SetTotal64(spb.total)
spb.prefix() spb.prefix()
spb.mtx.Unlock()
} }
func (spb *StackableProgressBar) prefix() { func (spb *StackableProgressBar) prefix() {
@ -66,9 +68,19 @@ func (spb *StackableProgressBar) prefix() {
} }
func (spb *StackableProgressBar) Finish() { func (spb *StackableProgressBar) Finish() {
atomic.AddInt32(&spb.items, -1) spb.mtx.Lock()
spb.group.Done()
spb.items--
if spb.items == 0 {
// slef cleanup
spb.BasicProgressBar.ProgressBar.Finish()
spb.BasicProgressBar.ProgressBar = nil
spb.started = false
spb.total = 0
return
}
spb.prefix() spb.prefix()
spb.mtx.Unlock()
} }
// BasicProgressBar is packer's basic progress bar. // BasicProgressBar is packer's basic progress bar.
@ -81,12 +93,12 @@ type BasicProgressBar struct {
var _ ProgressBar = new(BasicProgressBar) var _ ProgressBar = new(BasicProgressBar)
func (bpb *BasicProgressBar) Start(total int64) { func (bpb *BasicProgressBar) Start(total int64) {
bpb.SetTotal64(int64(total)) bpb.SetTotal64(total)
bpb.ProgressBar.Start() bpb.ProgressBar.Start()
} }
func (bpb *BasicProgressBar) Add(current int64) { func (bpb *BasicProgressBar) Add(current int64) {
bpb.ProgressBar.Add64(int64(current)) bpb.ProgressBar.Add64(current)
} }
func (bpb *BasicProgressBar) NewProxyReader(r io.Reader) io.Reader { func (bpb *BasicProgressBar) NewProxyReader(r io.Reader) io.Reader {
return &ProxyReader{ return &ProxyReader{