From 4e0a1858f99f5ae70e904bdab135d7fe22810f60 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Tue, 26 Jul 2016 06:41:23 +0200 Subject: [PATCH] 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. --- pkg/util/goroutinemap/goroutinemap.go | 9 ++++----- .../nestedpendingoperations/nestedpendingoperations.go | 5 ++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/util/goroutinemap/goroutinemap.go b/pkg/util/goroutinemap/goroutinemap.go index 202634f15b9..c5f6884a0c9 100644 --- a/pkg/util/goroutinemap/goroutinemap.go +++ b/pkg/util/goroutinemap/goroutinemap.go @@ -36,11 +36,11 @@ const ( // that GoRoutineMap will refuse to allow another operation to start with // the same operation name (if exponentialBackOffOnError is enabled). Each // 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 // 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. @@ -65,10 +65,9 @@ func NewGoRoutineMap(exponentialBackOffOnError bool) GoRoutineMap { g := &goRoutineMap{ operations: make(map[string]operation), exponentialBackOffOnError: exponentialBackOffOnError, - lock: &sync.Mutex{}, } - g.cond = sync.NewCond(g.lock) + g.cond = sync.NewCond(&g.lock) return g } @@ -76,7 +75,7 @@ type goRoutineMap struct { operations map[string]operation exponentialBackOffOnError bool cond *sync.Cond - lock *sync.Mutex + lock sync.Mutex } type operation struct { diff --git a/pkg/volume/util/nestedpendingoperations/nestedpendingoperations.go b/pkg/volume/util/nestedpendingoperations/nestedpendingoperations.go index 4aaddf299e8..9b3743f7bf5 100644 --- a/pkg/volume/util/nestedpendingoperations/nestedpendingoperations.go +++ b/pkg/volume/util/nestedpendingoperations/nestedpendingoperations.go @@ -65,9 +65,8 @@ func NewNestedPendingOperations(exponentialBackOffOnError bool) NestedPendingOpe g := &nestedPendingOperations{ operations: []operation{}, exponentialBackOffOnError: exponentialBackOffOnError, - lock: &sync.Mutex{}, } - g.cond = sync.NewCond(g.lock) + g.cond = sync.NewCond(&g.lock) return g } @@ -75,7 +74,7 @@ type nestedPendingOperations struct { operations []operation exponentialBackOffOnError bool cond *sync.Cond - lock *sync.Mutex + lock sync.Mutex } type operation struct {