From 699ab1ce22db0def0b0694b04893d8698d51ec96 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Fri, 8 Sep 2023 07:49:47 -0400 Subject: [PATCH 1/4] Expose atomic update to current stage The stager is often used in different go routines. The lack of an atomic setter was forcing clients to either manager their own locks or accept a minor race condition. Signed-off-by: Will Murphy --- stage.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/stage.go b/stage.go index 4ee1efd..65b47b5 100644 --- a/stage.go +++ b/stage.go @@ -1,15 +1,40 @@ package progress +import "sync/atomic" + type Stager interface { Stage() string } type Stage struct { - Current string + currentStage atomic.Value +} + +func (s *Stage) getCurrentStage() string { + return s.currentStage.Load().(string) +} + +func (s *Stage) setCurrentStage(newStage string) { + s.currentStage.Store(newStage) +} + +func (s *Stage) Stage() string { + return s.getCurrentStage() +} + +func (s *Stage) Set(newStage string) { + s.setCurrentStage(newStage) } -func (s Stage) Stage() string { - return s.Current +func NewStage(current string) *Stage { + result := Stage{} + result.currentStage.Store(current) + return &result +} + +type StagerSettable interface { + Stager + Set(newStage string) } type StagedMonitorable interface { From 92254fcd38e73776287af42a181bf6a2d5352b64 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Fri, 8 Sep 2023 09:34:57 -0400 Subject: [PATCH 2/4] separate new struct instead Signed-off-by: Will Murphy --- atomic_stage.go | 29 +++++++++++++++++++++++++++++ stage.go | 31 +++---------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 atomic_stage.go diff --git a/atomic_stage.go b/atomic_stage.go new file mode 100644 index 0000000..09e3bb1 --- /dev/null +++ b/atomic_stage.go @@ -0,0 +1,29 @@ +package progress + +import "sync/atomic" + +type AtomicStage struct { + current atomic.Value +} + +func (s *AtomicStage) getCurrentStage() string { + return s.current.Load().(string) +} + +func (s *AtomicStage) setCurrent(new string) { + s.current.Store(new) +} + +func (s *AtomicStage) Stage() string { + return s.getCurrentStage() +} + +func (s *AtomicStage) Set(new string) { + s.setCurrent(new) +} + +func NewAtomicStage(current string) *AtomicStage { + result := AtomicStage{} + result.current.Store(current) + return &result +} diff --git a/stage.go b/stage.go index 65b47b5..4ee1efd 100644 --- a/stage.go +++ b/stage.go @@ -1,40 +1,15 @@ package progress -import "sync/atomic" - type Stager interface { Stage() string } type Stage struct { - currentStage atomic.Value -} - -func (s *Stage) getCurrentStage() string { - return s.currentStage.Load().(string) -} - -func (s *Stage) setCurrentStage(newStage string) { - s.currentStage.Store(newStage) -} - -func (s *Stage) Stage() string { - return s.getCurrentStage() -} - -func (s *Stage) Set(newStage string) { - s.setCurrentStage(newStage) + Current string } -func NewStage(current string) *Stage { - result := Stage{} - result.currentStage.Store(current) - return &result -} - -type StagerSettable interface { - Stager - Set(newStage string) +func (s Stage) Stage() string { + return s.Current } type StagedMonitorable interface { From 1b5c0cfdc959336fed3e93be72e68410fc81fe52 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Fri, 8 Sep 2023 09:54:52 -0400 Subject: [PATCH 3/4] enforce interface Signed-off-by: Will Murphy --- atomic_stage.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atomic_stage.go b/atomic_stage.go index 09e3bb1..2902621 100644 --- a/atomic_stage.go +++ b/atomic_stage.go @@ -2,6 +2,8 @@ package progress import "sync/atomic" +var _ Stager = (*AtomicStage)(nil) + type AtomicStage struct { current atomic.Value } From d30a4f8192a4ae5fd073e0ce9516a852614005a5 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Fri, 8 Sep 2023 10:08:47 -0400 Subject: [PATCH 4/4] finish rename Signed-off-by: Will Murphy --- atomic_stage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atomic_stage.go b/atomic_stage.go index 2902621..811fd02 100644 --- a/atomic_stage.go +++ b/atomic_stage.go @@ -8,7 +8,7 @@ type AtomicStage struct { current atomic.Value } -func (s *AtomicStage) getCurrentStage() string { +func (s *AtomicStage) getCurrent() string { return s.current.Load().(string) } @@ -17,7 +17,7 @@ func (s *AtomicStage) setCurrent(new string) { } func (s *AtomicStage) Stage() string { - return s.getCurrentStage() + return s.getCurrent() } func (s *AtomicStage) Set(new string) {