2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-29 07:00:49 +00:00

ui/progress: Load both values in a single Lock/Unlock

We always need both values, except in a test, so we don't need to lock
twice and risk scheduling in between.

Also, removed the resetting in Done. This copied a mutex, which isn't
allowed. Static analyzers tend to trip over that.
This commit is contained in:
greatroar 2022-10-25 07:41:44 +02:00
parent b57d42905c
commit 2dafda9164
2 changed files with 14 additions and 18 deletions

View File

@ -110,7 +110,8 @@ func TestFindUsedBlobs(t *testing.T) {
continue continue
} }
test.Equals(t, p.Get(), uint64(i+1)) v, _ := p.Get()
test.Equals(t, v, uint64(i+1))
goldenFilename := filepath.Join("testdata", fmt.Sprintf("used_blobs_snapshot%d", i)) goldenFilename := filepath.Join("testdata", fmt.Sprintf("used_blobs_snapshot%d", i))
want := loadIDSet(t, goldenFilename) want := loadIDSet(t, goldenFilename)
@ -151,7 +152,8 @@ func TestMultiFindUsedBlobs(t *testing.T) {
for i := 1; i < 3; i++ { for i := 1; i < 3; i++ {
err := restic.FindUsedBlobs(context.TODO(), repo, snapshotTrees, usedBlobs, p) err := restic.FindUsedBlobs(context.TODO(), repo, snapshotTrees, usedBlobs, p)
test.OK(t, err) test.OK(t, err)
test.Equals(t, p.Get(), uint64(i*len(snapshotTrees))) v, _ := p.Get()
test.Equals(t, v, uint64(i*len(snapshotTrees)))
if !want.Equals(usedBlobs) { if !want.Equals(usedBlobs) {
t.Errorf("wrong list of blobs returned:\n missing blobs: %v\n extra blobs: %v", t.Errorf("wrong list of blobs returned:\n missing blobs: %v\n extra blobs: %v",

View File

@ -78,32 +78,25 @@ func (c *Counter) Done() {
c.tick.Stop() c.tick.Stop()
} }
close(c.stop) close(c.stop)
<-c.stopped // Wait for last progress report. <-c.stopped // Wait for last progress report.
*c = Counter{} // Prevent reuse.
} }
// Get the current Counter value. This method is concurrency-safe. // Get returns the current value and the maximum of c.
func (c *Counter) Get() uint64 { // This method is concurrency-safe.
func (c *Counter) Get() (v, max uint64) {
c.valueMutex.Lock() c.valueMutex.Lock()
v := c.value v, max = c.value, c.max
c.valueMutex.Unlock() c.valueMutex.Unlock()
return v return v, max
}
func (c *Counter) getMax() uint64 {
c.valueMutex.Lock()
max := c.max
c.valueMutex.Unlock()
return max
} }
func (c *Counter) run() { func (c *Counter) run() {
defer close(c.stopped) defer close(c.stopped)
defer func() { defer func() {
// Must be a func so that time.Since isn't called at defer time. // Must be a func so that time.Since isn't called at defer time.
c.report(c.Get(), c.getMax(), time.Since(c.start), true) v, max := c.Get()
c.report(v, max, time.Since(c.start), true)
}() }()
var tick <-chan time.Time var tick <-chan time.Time
@ -123,6 +116,7 @@ func (c *Counter) run() {
return return
} }
c.report(c.Get(), c.getMax(), now.Sub(c.start), false) v, max := c.Get()
c.report(v, max, now.Sub(c.start), false)
} }
} }