Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Conductor/Client/Authentication/TokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TokenHandler
{
private static int REFRESH_TOKEN_RETRY_COUNTER_LIMIT = 5;
private static readonly object _lockObject = new object();
private static readonly object _getTokenlockObject = new object();
private readonly MemoryCache _memoryCache;
private static ILogger _logger;

Expand All @@ -18,15 +19,19 @@ public TokenHandler()
_memoryCache = new MemoryCache(new MemoryCacheOptions());
_logger = ApplicationLogging.CreateLogger<TokenHandler>();
}

public string GetToken(OrkesAuthenticationSettings authenticationSettings, TokenResourceApi tokenClient)
{
string token = (string)_memoryCache.Get(authenticationSettings);
if (token != null)
{
return token;
}
return RefreshToken(authenticationSettings, tokenClient);

lock (_getTokenlockObject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this should be used:

string token = _memoryCache.GetOrCreate(authenticationSettings, entry =>
{
    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
    
    return GetTokenFromServer(authenticationSettings, tokenClient);
});

{
token = (string)_memoryCache.Get(authenticationSettings);
return token != null ? token : RefreshToken(authenticationSettings, tokenClient);
}
}

public string RefreshToken(OrkesAuthenticationSettings authenticationSettings, TokenResourceApi tokenClient)
Expand Down