Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Letting Memory Cache takes care of the locks cause of concurrent requ… #115

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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)
{
token = (string)_memoryCache.Get(authenticationSettings);
return token != null ? token : RefreshToken(authenticationSettings, tokenClient);
}
}

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