Commit 4e0a1858 authored by Matt T. Proud's avatar Matt T. Proud

pkg/util/goroutinemap: apply idiomatic Go cleanups

Package goroutinemap can be structurally simplified to be more idiomatic, concise, and free of error potential. No structural changes are made. It is unconventional declare `sync.Mutex` directly as a pointerized field in a parent structure. The `sync.Mutex` operates on pointer receivers of itself; and by relying on that, the types that contain those fields can be safely constructed using https://golang.org/ref/spec#The_zero_value. The duration constants are already of type `time.Duration`, so re-declaring that is redundant.
parent ed3a29bd
...@@ -36,11 +36,11 @@ const ( ...@@ -36,11 +36,11 @@ const (
// that GoRoutineMap will refuse to allow another operation to start with // that GoRoutineMap will refuse to allow another operation to start with
// the same operation name (if exponentialBackOffOnError is enabled). Each // the same operation name (if exponentialBackOffOnError is enabled). Each
// successive error results in a wait 2x times the previous. // successive error results in a wait 2x times the previous.
initialDurationBeforeRetry time.Duration = 500 * time.Millisecond initialDurationBeforeRetry = 500 * time.Millisecond
// maxDurationBeforeRetry is the maximum amount of time that // maxDurationBeforeRetry is the maximum amount of time that
// durationBeforeRetry will grow to due to exponential backoff. // durationBeforeRetry will grow to due to exponential backoff.
maxDurationBeforeRetry time.Duration = 2 * time.Minute maxDurationBeforeRetry = 2 * time.Minute
) )
// GoRoutineMap defines the supported set of operations. // GoRoutineMap defines the supported set of operations.
...@@ -65,10 +65,9 @@ func NewGoRoutineMap(exponentialBackOffOnError bool) GoRoutineMap { ...@@ -65,10 +65,9 @@ func NewGoRoutineMap(exponentialBackOffOnError bool) GoRoutineMap {
g := &goRoutineMap{ g := &goRoutineMap{
operations: make(map[string]operation), operations: make(map[string]operation),
exponentialBackOffOnError: exponentialBackOffOnError, exponentialBackOffOnError: exponentialBackOffOnError,
lock: &sync.Mutex{},
} }
g.cond = sync.NewCond(g.lock) g.cond = sync.NewCond(&g.lock)
return g return g
} }
...@@ -76,7 +75,7 @@ type goRoutineMap struct { ...@@ -76,7 +75,7 @@ type goRoutineMap struct {
operations map[string]operation operations map[string]operation
exponentialBackOffOnError bool exponentialBackOffOnError bool
cond *sync.Cond cond *sync.Cond
lock *sync.Mutex lock sync.Mutex
} }
type operation struct { type operation struct {
......
...@@ -65,9 +65,8 @@ func NewNestedPendingOperations(exponentialBackOffOnError bool) NestedPendingOpe ...@@ -65,9 +65,8 @@ func NewNestedPendingOperations(exponentialBackOffOnError bool) NestedPendingOpe
g := &nestedPendingOperations{ g := &nestedPendingOperations{
operations: []operation{}, operations: []operation{},
exponentialBackOffOnError: exponentialBackOffOnError, exponentialBackOffOnError: exponentialBackOffOnError,
lock: &sync.Mutex{},
} }
g.cond = sync.NewCond(g.lock) g.cond = sync.NewCond(&g.lock)
return g return g
} }
...@@ -75,7 +74,7 @@ type nestedPendingOperations struct { ...@@ -75,7 +74,7 @@ type nestedPendingOperations struct {
operations []operation operations []operation
exponentialBackOffOnError bool exponentialBackOffOnError bool
cond *sync.Cond cond *sync.Cond
lock *sync.Mutex lock sync.Mutex
} }
type operation struct { type operation struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment