diff --git a/flyteidl/clients/go/admin/cache/token_cache_inmemory.go b/flyteidl/clients/go/admin/cache/token_cache_inmemory.go index 703eb9555f..477b28ace4 100644 --- a/flyteidl/clients/go/admin/cache/token_cache_inmemory.go +++ b/flyteidl/clients/go/admin/cache/token_cache_inmemory.go @@ -9,9 +9,10 @@ import ( ) type TokenCacheInMemoryProvider struct { - token atomic.Value - mu *sync.Mutex - cond *sync.Cond + token atomic.Value + mu *sync.Mutex + condLocker *NoopLocker + cond *sync.Cond } func (t *TokenCacheInMemoryProvider) SaveToken(token *oauth2.Token) error { @@ -48,10 +49,33 @@ func (t *TokenCacheInMemoryProvider) Unlock() { // The current usage is that one who was able to acquire the lock using TryLock is the one who gets a valid token and notifies all the waitlist requesters so that they can use the new valid token. // It also locks the Locker in the condition variable as the semantics of Wait is that it unlocks the Locker after adding // the consumer to the waitlist and before blocking on notification. +// We use the condLocker which is noOp locker to get added to waitlist for notifications. +// The underlying notifcationList doesn't need to be guarded as it implmentation is atomic and is thread safe +// Refer https://go.dev/src/runtime/sema.go +// Following is the function and its comments +// notifyListAdd adds the caller to a notify list such that it can receive +// notifications. The caller must eventually call notifyListWait to wait for +// such a notification, passing the returned ticket number. +// +// func notifyListAdd(l *notifyList) uint32 { +// // This may be called concurrently, for example, when called from +// // sync.Cond.Wait while holding a RWMutex in read mode. +// return l.wait.Add(1) - 1 +// } func (t *TokenCacheInMemoryProvider) CondWait() { - t.cond.L.Lock() + t.condLocker.Lock() t.cond.Wait() - t.cond.L.Unlock() + t.condLocker.Unlock() +} + +// NoopLocker has empty implementation of Locker interface +type NoopLocker struct { +} + +func (*NoopLocker) Lock() { + +} +func (*NoopLocker) Unlock() { } // CondBroadcast signals the condition. @@ -60,9 +84,11 @@ func (t *TokenCacheInMemoryProvider) CondBroadcast() { } func NewTokenCacheInMemoryProvider() *TokenCacheInMemoryProvider { + condLocker := &NoopLocker{} return &TokenCacheInMemoryProvider{ - mu: &sync.Mutex{}, - token: atomic.Value{}, - cond: sync.NewCond(&sync.Mutex{}), + mu: &sync.Mutex{}, + token: atomic.Value{}, + condLocker: condLocker, + cond: sync.NewCond(condLocker), } }