diff --git a/apis/duck/cached_test.go b/apis/duck/cached_test.go index 9ab0369f9..656425129 100644 --- a/apis/duck/cached_test.go +++ b/apis/duck/cached_test.go @@ -19,10 +19,10 @@ package duck import ( "context" "fmt" + "sync/atomic" "testing" "time" - "go.uber.org/atomic" "golang.org/x/sync/errgroup" "k8s.io/apimachinery/pkg/runtime/schema" @@ -37,7 +37,7 @@ type BlockingInformerFactory struct { var _ InformerFactory = (*BlockingInformerFactory)(nil) func (bif *BlockingInformerFactory) Get(ctx context.Context, gvr schema.GroupVersionResource) (cache.SharedIndexInformer, cache.GenericLister, error) { - bif.nCalls.Inc() + bif.nCalls.Add(1) // Wait here until we can acquire the lock <-bif.block @@ -58,7 +58,7 @@ func TestSameGVR(t *testing.T) { } // counts the number of calls to cif.Get that returned - retGetCount := atomic.NewInt32(0) + var retGetCount atomic.Int32 errGrp, ctx := errgroup.WithContext(context.Background()) @@ -75,7 +75,7 @@ func TestSameGVR(t *testing.T) { for i := 0; i < iter; i++ { errGrp.Go(func() error { _, _, err := cif.Get(ctx, gvr) - retGetCount.Inc() + retGetCount.Add(1) return err }) } @@ -119,7 +119,7 @@ func TestDifferentGVRs(t *testing.T) { } // counts the number of calls to cif.Get that returned - retGetCount := atomic.NewInt32(0) + var retGetCount atomic.Int32 errGrp, ctx := errgroup.WithContext(context.Background()) @@ -136,7 +136,7 @@ func TestDifferentGVRs(t *testing.T) { errGrp.Go(func() error { _, _, err := cif.Get(ctx, gvr) - retGetCount.Inc() + retGetCount.Add(1) return err }) } diff --git a/controller/controller_test.go b/controller/controller_test.go index e75be6469..e6920d5d5 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -21,11 +21,11 @@ import ( "errors" "fmt" "sync" + "sync/atomic" "testing" "time" "github.com/google/go-cmp/cmp" - "go.uber.org/atomic" coordinationv1 "k8s.io/api/coordination/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -874,7 +874,7 @@ type CountingReconciler struct { } func (cr *CountingReconciler) Reconcile(context.Context, string) error { - cr.count.Inc() + cr.count.Add(1) return nil } @@ -927,7 +927,7 @@ type countingLeaderAwareReconciler struct { var _ reconciler.LeaderAware = (*countingLeaderAwareReconciler)(nil) func (cr *countingLeaderAwareReconciler) Promote(b reconciler.Bucket, enq func(reconciler.Bucket, types.NamespacedName)) error { - cr.promotionCount.Inc() + cr.promotionCount.Add(1) return cr.LeaderAwareFuncs.Promote(b, enq) } @@ -941,7 +941,7 @@ func (cr *countingLeaderAwareReconciler) Reconcile(ctx context.Context, key stri Namespace: namespace, Name: name, }) { - cr.reconcileCount.Inc() + cr.reconcileCount.Add(1) } return nil } diff --git a/go.mod b/go.mod index 42081bf68..ed3f7efad 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,6 @@ require ( github.com/spf13/pflag v1.0.5 github.com/tsenart/vegeta/v12 v12.11.0 go.opencensus.io v0.24.0 - go.uber.org/atomic v1.9.0 go.uber.org/automaxprocs v1.4.0 go.uber.org/zap v1.24.0 golang.org/x/net v0.12.0 @@ -91,6 +90,7 @@ require ( github.com/prometheus/procfs v0.10.1 // indirect github.com/prometheus/statsd_exporter v0.21.0 // indirect github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect + go.uber.org/atomic v1.9.0 // indirect go.uber.org/goleak v1.2.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/crypto v0.11.0 // indirect diff --git a/metrics/metrics.go b/metrics/metrics.go index 453e1b84a..b65f470cc 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -19,12 +19,12 @@ package metrics import ( "context" "net/url" + "sync/atomic" "time" "go.opencensus.io/stats" "go.opencensus.io/stats/view" "go.opencensus.io/tag" - "go.uber.org/atomic" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/metrics" "k8s.io/client-go/util/workqueue" @@ -74,13 +74,13 @@ var _ workqueue.GaugeMetric = (*gaugeMetric)(nil) // Inc implements CounterMetric func (m *gaugeMetric) Inc() { - total := m.total.Inc() + total := m.total.Add(1) Record(context.Background(), m.measure.M(total), stats.WithTags(m.mutators...)) } // Dec implements GaugeMetric func (m *gaugeMetric) Dec() { - total := m.total.Dec() + total := m.total.Add(-1) Record(context.Background(), m.measure.M(total), stats.WithTags(m.mutators...)) } diff --git a/profiling/server.go b/profiling/server.go index 36e08b4b4..f362f36fa 100644 --- a/profiling/server.go +++ b/profiling/server.go @@ -22,9 +22,9 @@ import ( "net/http/pprof" "os" "strconv" + "sync/atomic" "time" - "go.uber.org/atomic" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" ) @@ -63,8 +63,11 @@ func NewHandler(logger *zap.SugaredLogger, enableProfiling bool) *Handler { mux.HandleFunc(pprofPrefix+"trace", pprof.Trace) logger.Info("Profiling enabled: ", enableProfiling) + var enabled atomic.Bool + enabled.Store(enableProfiling) + return &Handler{ - enabled: atomic.NewBool(enableProfiling), + enabled: &enabled, handler: mux, log: logger, } diff --git a/reconciler/testing/context.go b/reconciler/testing/context.go index 150c59bf2..0a36caefc 100644 --- a/reconciler/testing/context.go +++ b/reconciler/testing/context.go @@ -18,11 +18,10 @@ package testing import ( "context" + "sync/atomic" "testing" "time" - "go.uber.org/atomic" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -94,7 +93,7 @@ func RunAndSyncInformers(ctx context.Context, informers ...controller.Informer) c.PrependReactor("list", "*", func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { // Every list (before actual informer usage) is going to be followed by a Watch call. - watchesPending.Inc() + watchesPending.Add(1) return false, nil, nil }) @@ -108,7 +107,7 @@ func RunAndSyncInformers(ctx context.Context, informers ...controller.Informer) return false, nil, err } - watchesPending.Dec() + watchesPending.Add(-1) return true, watch, nil }) diff --git a/reconciler/testing/hooks.go b/reconciler/testing/hooks.go index c89be34a0..c4129c612 100644 --- a/reconciler/testing/hooks.go +++ b/reconciler/testing/hooks.go @@ -20,9 +20,9 @@ package testing import ( "errors" "sync" + "sync/atomic" "time" - "go.uber.org/atomic" "k8s.io/apimachinery/pkg/runtime" kubetesting "k8s.io/client-go/testing" ) @@ -94,16 +94,18 @@ type Hooks struct { // more fake clients and wait for all hooks to complete. // TODO(grantr): Allow validating that a hook never fires func NewHooks() *Hooks { + var ci atomic.Int32 + ci.Store(-1) return &Hooks{ completionCh: make(chan int32, 100), - completionIndex: atomic.NewInt32(-1), + completionIndex: &ci, } } // OnCreate attaches a create hook to the given Fake. The hook function is // executed every time a resource of the given type is created. func (h *Hooks) OnCreate(fake *kubetesting.Fake, resource string, rf CreateHookFunc) { - index := h.completionIndex.Inc() + index := h.completionIndex.Add(1) fake.PrependReactor("create", resource, func(a kubetesting.Action) (bool, runtime.Object, error) { obj := a.(kubetesting.CreateActionImpl).Object @@ -119,7 +121,7 @@ func (h *Hooks) OnCreate(fake *kubetesting.Fake, resource string, rf CreateHookF // OnUpdate attaches an update hook to the given Fake. The hook function is // executed every time a resource of the given type is updated. func (h *Hooks) OnUpdate(fake *kubetesting.Fake, resource string, rf UpdateHookFunc) { - index := h.completionIndex.Inc() + index := h.completionIndex.Add(1) fake.PrependReactor("update", resource, func(a kubetesting.Action) (bool, runtime.Object, error) { obj := a.(kubetesting.UpdateActionImpl).Object @@ -135,7 +137,7 @@ func (h *Hooks) OnUpdate(fake *kubetesting.Fake, resource string, rf UpdateHookF // OnDelete attaches a delete hook to the given Fake. The hook function is // executed every time a resource of the given type is deleted. func (h *Hooks) OnDelete(fake *kubetesting.Fake, resource string, rf DeleteHookFunc) { - index := h.completionIndex.Inc() + index := h.completionIndex.Add(1) fake.PrependReactor("delete", resource, func(a kubetesting.Action) (bool, runtime.Object, error) { name := a.(kubetesting.DeleteActionImpl).Name @@ -173,7 +175,7 @@ func (h *Hooks) WaitForHooks(timeout time.Duration) error { case i := <-h.completionCh: hookCompletions[i] = HookComplete if len(hookCompletions) == ci { - h.completionIndex.Dec() + h.completionIndex.Add(-1) return nil } case <-timer: diff --git a/test/spoof/spoof_test.go b/test/spoof/spoof_test.go index f1da85238..df07f4094 100644 --- a/test/spoof/spoof_test.go +++ b/test/spoof/spoof_test.go @@ -24,11 +24,10 @@ import ( "fmt" "net/http" "net/url" + "sync/atomic" "time" "testing" - - "go.uber.org/atomic" ) var ( @@ -49,7 +48,7 @@ type fakeTransport struct { } func (ft *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) { - call := ft.calls.Inc() + call := ft.calls.Add(1) if ft.response != nil && call == 2 { // If both a response and an error is defined, we return just the response on // the second call to simulate a retry that passes eventually. @@ -159,7 +158,7 @@ func TestSpoofingClient_WaitForEndpointState(t *testing.T) { inState: func() ResponseChecker { var calls atomic.Int32 return func(resp *Response) (done bool, err error) { - val := calls.Inc() + val := calls.Add(1) // Stop the looping on the third invocation return val == 3, nil } diff --git a/test/zipkin/util.go b/test/zipkin/util.go index 72d867f19..2e9b8dbae 100644 --- a/test/zipkin/util.go +++ b/test/zipkin/util.go @@ -28,10 +28,10 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "testing" "time" - "go.uber.org/atomic" tracingconfig "knative.dev/pkg/tracing/config" "github.com/openzipkin/zipkin-go/model"