Skip to content

Commit

Permalink
Added lock to token refresh; changed logging format --ECL
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Sep 6, 2023
1 parent bffb807 commit 20e12c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ClearMLAuthenticationService : BackgroundService, IClearMLAuthentic
private readonly HttpClient _httpClient;
private readonly IOptionsMonitor<ClearMLNmtEngineOptions> _options;
private readonly ILogger<ClearMLAuthenticationService> _logger;
private readonly AsyncLock _lock = new();

// technically, the token should be good for 30 days, but let's refresh each hour
// to know well ahead of time if something is wrong.
Expand All @@ -25,11 +26,14 @@ ILogger<ClearMLAuthenticationService> logger

public async Task<string> GetAuthToken(CancellationToken cancellationToken = default)
{
if (_authToken is "")
using (await _lock.LockAsync())
{
//Should only happen once, so no different in cost than previous solution
_logger.LogInformation("Token was empty; refreshing");
await AuthorizeAsync(cancellationToken);
if (_authToken is "")
{
//Should only happen once, so no different in cost than previous solution
_logger.LogInformation("Token was empty; refreshing");
await AuthorizeAsync(cancellationToken);
}
}
return _authToken;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(
}
catch (Exception e)
{
_logger.LogError(0, exception: e, null);
_logger.LogError(e, null);
return HealthCheckResult.Unhealthy(exception: e);
}
}
Expand Down

0 comments on commit 20e12c9

Please sign in to comment.