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

Cache TokenCredential #2845

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using DurableTask.AzureStorage;
using Microsoft.Azure.WebJobs.Extensions.DurableTask.Options;
using Microsoft.Extensions.Configuration;
using System.Collections.Concurrent;

#if !FUNCTIONS_V1
using Microsoft.Azure.WebJobs.Extensions.DurableTask.Auth;
using Microsoft.WindowsAzure.Storage.Auth;
Expand All @@ -19,6 +21,9 @@ internal sealed class AzureStorageAccountProvider : IStorageAccountProvider
#if !FUNCTIONS_V1
private readonly ITokenCredentialFactory credentialFactory;

private readonly ConcurrentDictionary<string, TokenCredential> cachedTokenCredentials =
new ConcurrentDictionary<string, TokenCredential>();

bachuv marked this conversation as resolved.
Show resolved Hide resolved
public AzureStorageAccountProvider(IConnectionInfoResolver connectionInfoResolver, ITokenCredentialFactory credentialFactory)
{
this.connectionInfoResolver = connectionInfoResolver ?? throw new ArgumentNullException(nameof(connectionInfoResolver));
Expand All @@ -44,7 +49,11 @@ public StorageAccountDetails GetStorageAccountDetails(string connectionName)
AzureStorageAccountOptions account = connectionInfo.Get<AzureStorageAccountOptions>();
if (account != null)
{
TokenCredential credential = this.credentialFactory.Create(connectionInfo);
string key = !string.IsNullOrEmpty(account.AccountName) ? account.AccountName : account.BlobServiceUri.ToString();
bachuv marked this conversation as resolved.
Show resolved Hide resolved
bachuv marked this conversation as resolved.
Show resolved Hide resolved

TokenCredential credential = this.cachedTokenCredentials.GetOrAdd(
key,
attr => this.credentialFactory.Create(connectionInfo));
AnatoliB marked this conversation as resolved.
Show resolved Hide resolved

return new StorageAccountDetails
{
Expand Down
10 changes: 5 additions & 5 deletions test/Common/AzureStorageAccountProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public void GetStorageAccountDetails_ConfigSection_Endpoints()
Assert.Equal(options.TableServiceUri, actual.TableServiceUri);

// Get CloudStorageAccount
CloudStorageAccount acount = actual.ToCloudStorageAccount();
Assert.Same(actual.StorageCredentials, acount.Credentials);
Assert.Equal(options.BlobServiceUri, acount.BlobEndpoint);
Assert.Equal(options.QueueServiceUri, acount.QueueEndpoint);
Assert.Equal(options.TableServiceUri, acount.TableEndpoint);
CloudStorageAccount account = actual.ToCloudStorageAccount();
Assert.Same(actual.StorageCredentials, account.Credentials);
Assert.Equal(options.BlobServiceUri, account.BlobEndpoint);
Assert.Equal(options.QueueServiceUri, account.QueueEndpoint);
Assert.Equal(options.TableServiceUri, account.TableEndpoint);
}

[Fact]
Expand Down
Loading