Skip to content

Commit

Permalink
using noop locker as waitlist add is atomic operation
Browse files Browse the repository at this point in the history
Signed-off-by: pmahindrakar-oss <[email protected]>
  • Loading branch information
pmahindrakar-oss committed May 31, 2024
1 parent 2c5ada9 commit 9ca45a3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions flyteidl/clients/go/admin/cache/token_cache_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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),
}
}

0 comments on commit 9ca45a3

Please sign in to comment.