Repository: kubernetes/kubernetes
File: pkg/controller/serviceaccount/tokens_controller.go (Go)

Code before:
		e.syncSecretQueue.ShutDown()
		wg.Wait()
	}()

	if !cache.WaitForNamedCacheSyncWithContext(ctx, e.serviceAccountSynced, e.secretSynced) {
		return
	}

	logger.V(5).Info("Starting workers")
	for i := 0; i < workers; i++ {
		wg.Go(func() {
			wait.UntilWithContext(ctx, e.syncServiceAccount, 0)
		})
		wg.Go(func() {
			wait.UntilWithContext(ctx, e.syncSecret, 0)
		})
	}
	<-ctx.Done()
}

func (e *TokensController) queueServiceAccountSync(obj interface{}) {
	if serviceAccount, ok := obj.(*v1.ServiceAccount); ok {
		e.syncServiceAccountQueue.Add(makeServiceAccountKey(serviceAccount))
		return
	}
	if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
		if serviceAccount, ok := tombstone.Obj.(*v1.ServiceAccount); ok {
			e.syncServiceAccountQueue.Add(makeServiceAccountKey(serviceAccount))
			return
		}
		utilruntime.HandleError(fmt.Errorf("error decoding object, invalid type: %T", tombstone.Obj))
	}
}

func (e *TokensController) queueServiceAccountUpdateSync(oldObj interface{}, newObj interface{}) {
	if serviceAccount, ok := newObj.(*v1.ServiceAccount); ok {
		e.syncServiceAccountQueue.Add(makeServiceAccountKey(serviceAccount))
	}
}

// complete optionally requeues key, then calls queue.Done(key)
func retryOrForget[T comparable](logger klog.Logger, queue workqueue.TypedRateLimitingInterface[T], key T, requeue bool, maxRetries int) {
	if !requeue {
		queue.Forget(key)
		return
	}

	requeueCount := queue.NumRequeues(key)
	if requeueCount < maxRetries {
		queue.AddRateLimited(key)
		return

Review comment (performance):
can simplify this by hoisting this to the top

```diff
+ if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
+ 	obj = tombstone.Obj
+ }
  if serviceAccount, ok := obj.(*v1.ServiceAccount); ok {
    ...
```

Code after:
		e.syncSecretQueue.ShutDown()
		wg.Wait()
	}()

	if !cache.WaitForNamedCacheSyncWithContext(ctx, e.serviceAccountSynced, e.secretSynced) {
		return
	}

	logger.V(5).Info("Starting workers")
	for i := 0; i < workers; i++ {
		wg.Go(func() {
			wait.UntilWithContext(ctx, e.syncServiceAccount, 0)
		})
		wg.Go(func() {
			wait.UntilWithContext(ctx, e.syncSecret, 0)
		})
	}
	<-ctx.Done()
}

func (e *TokensController) queueServiceAccountSync(obj interface{}) {
	if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
		obj = tombstone.Obj
	}
	if serviceAccount, ok := obj.(*v1.ServiceAccount); ok {
		e.syncServiceAccountQueue.Add(makeServiceAccountKey(serviceAccount))
		return
	}
	utilruntime.HandleError(fmt.Errorf("error decoding object, invalid type: %T", obj))
}

func (e *TokensController) queueServiceAccountUpdateSync(oldObj interface{}, newObj interface{}) {
	if serviceAccount, ok := newObj.(*v1.ServiceAccount); ok {
		e.syncServiceAccountQueue.Add(makeServiceAccountKey(serviceAccount))
	}
}

// complete optionally requeues key, then calls queue.Done(key)
func retryOrForget[T comparable](logger klog.Logger, queue workqueue.TypedRateLimitingInterface[T], key T, requeue bool, maxRetries int) {
	if !requeue {
		queue.Forget(key)
		return
	}

	requeueCount := queue.NumRequeues(key)
	if requeueCount < maxRetries {
		queue.AddRateLimited(key)
		return
	}

	logger.V(4).Info("retried several times", "key", key, "count", requeueCount)