in src/AzureCacheOptionsProviderWithToken.cs [163:196]
internal async Task AcquireTokenAsync(bool throwOnFailure)
{
Exception? lastException = null;
for (var attemptCount = 0; attemptCount < _azureCacheOptions.MaxTokenRefreshAttempts; ++attemptCount)
{
try
{
TokenResult tokenResult = await IdentityClient.GetTokenAsync().ConfigureAwait(false);
var leeway = TimeSpan.FromSeconds(30); // Sometimes the updated token may actually have an expiry a few seconds shorter than the original
if (tokenResult is not null && tokenResult.ExpiresOn.UtcDateTime >= _tokenExpiry.Subtract(leeway))
{
_token = tokenResult.Token;
_tokenAcquiredTime = DateTime.UtcNow;
_tokenExpiry = tokenResult.ExpiresOn.UtcDateTime;
TokenRefreshed?.Invoke(this, tokenResult);
return;
}
}
catch (Exception ex)
{
lastException = ex;
}
await Task.Delay(_azureCacheOptions.TokenRefreshBackoff.Invoke(attemptCount, lastException)).ConfigureAwait(false);
}
// If we get here, we never successfully acquired a token
TokenRefreshFailed?.Invoke(this, new(lastException, _tokenExpiry));
if (throwOnFailure && lastException is not null)
{
throw lastException;
}
}