Skip to content

Locking optimizations #2372

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions PermissionsService/Services/PermissionsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class PermissionsStore : IPermissionsStore
private readonly string _scopesInformation;
private readonly int _defaultRefreshTimeInHours; // life span of the in-memory cache
private const string DefaultLocale = "en-US"; // default locale language
private static readonly AsyncNonKeyedLocker _permissionDataLocker = new();
private static readonly AsyncNonKeyedLocker _permissionsDocumentLocker = new();
private static readonly AsyncKeyedLocker<string> _asyncKeyedLocker = new();
private const string Delegated = "Delegated";
private const string Application = "Application";
Expand Down Expand Up @@ -90,7 +92,7 @@ public PermissionsStore(IConfiguration configuration, IHttpClientUtility httpCli

private async Task<PermissionsDataInfo> GetPermissionsDataAsync()
{
using(await _asyncKeyedLocker.LockAsync("PermissionData"))
using(await _permissionDataLocker.LockAsync())
{
return await _cache.GetOrCreateAsync("PermissionsData", async entry =>
{
Expand All @@ -103,7 +105,7 @@ private async Task<PermissionsDataInfo> GetPermissionsDataAsync()

private async Task<PermissionsDocument> LoadDocumentAsync()
{
using(await _asyncKeyedLocker.LockAsync("PermissionsDocument"))
using(await _permissionsDocumentLocker.LockAsync())
{
return await _cache.GetOrCreateAsync("PermissionsDocument", async entry =>
{
Expand Down Expand Up @@ -206,10 +208,10 @@ private async Task<IDictionary<string, IDictionary<string, ScopeInformation>>> G
SeverityLevel.Information,
_permissionsTraceProperties);

// making sure only a single thread at a time access the cache
// making sure only a single thread at a time access the cache per locale
// when already seeded, lock will resolve fast and access the cache
// when not seeded, lock will resolve slow for all other threads and seed the cache on the first thread
using (await _asyncKeyedLocker.LockAsync("scopes"))
using (await _asyncKeyedLocker.LockAsync(locale))
{
var scopesInformationDictionary = await _cache.GetOrCreateAsync($"ScopesInfoList_{locale}", async cacheEntry =>
{
Expand Down
4 changes: 2 additions & 2 deletions SamplesService/Services/SamplesStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public async Task<SampleQueriesList> FetchSampleQueriesListAsync(string locale)

string sourceMsg = $"Return sample queries list for locale '{locale}' from in-memory cache '{locale}'";

// making sure only a single thread at a time access the cache
// making sure only a single thread at a time access the cache per locale
// when already seeded, lock will resolve fast and access the cache
// when not seeded, lock will resolve slow for all other threads and seed the cache on the first thread
using (await _asyncKeyedLocker.LockAsync("samples"))
using (await _asyncKeyedLocker.LockAsync(locale))
{
// Fetch cached sample queries
var sampleQueriesList = await _samplesCache.GetOrCreateAsync(locale, async cacheEntry =>
Expand Down