Skip to content

Commit

Permalink
fix: Gauge concurrency (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson authored Jul 31, 2023
1 parent 2f089b2 commit c140895
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/output/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"time"
)

Expand All @@ -26,12 +27,15 @@ type ProgressGauge struct {
current int
capacity int
startTime time.Time
lock sync.RWMutex
}

func (g *ProgressGauge) IsReady() bool {
if g == nil {
return false
}
g.lock.RLock()
defer g.lock.RUnlock()
if g.current < 0 {
return false
}
Expand All @@ -54,41 +58,53 @@ func (g *ProgressGauge) SetCapacity(capacity int) {
if capacity < 0 {
return
}
g.lock.Lock()
defer g.lock.Unlock()
g.capacity = capacity
}

func (g *ProgressGauge) SetStatus(status string) {
if g == nil {
return
}
g.lock.Lock()
defer g.lock.Unlock()
g.status = status
}

func (g *ProgressGauge) Set(current int) {
if g == nil {
return
}
g.lock.Lock()
defer g.lock.Unlock()
g.current = current
}

func (g *ProgressGauge) Inc() {
if g == nil {
return
}
g.lock.Lock()
defer g.lock.Unlock()
g.current += 1
}

func (g *ProgressGauge) Dec() {
if g == nil {
return
}
g.lock.Lock()
defer g.lock.Unlock()
g.current -= 1
}

func (g *ProgressGauge) InitStartTime() {
if g == nil {
return
}
g.lock.Lock()
defer g.lock.Unlock()
if g.startTime.IsZero() {
g.startTime = time.Now()
}
Expand All @@ -101,6 +117,9 @@ func (g *ProgressGauge) String() string {
if g == nil {
return ""
}

g.lock.RLock()
defer g.lock.RUnlock()
if g.startTime.IsZero() {
g.startTime = time.Now()
}
Expand Down

0 comments on commit c140895

Please sign in to comment.