Skip to content

Commit

Permalink
add own scope and dbcontext to GetSubjectResources to avoid concurren…
Browse files Browse the repository at this point in the history
…cy errors
  • Loading branch information
knuhau committed Jan 3, 2025
1 parent 1486da6 commit 0b0672b
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions;
using Digdir.Domain.Dialogporten.Domain.SubjectResources;
using Digdir.Domain.Dialogporten.Infrastructure.Common.Exceptions;
using Digdir.Domain.Dialogporten.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;

Expand All @@ -27,6 +29,7 @@ internal sealed class AltinnAuthorizationClient : IAltinnAuthorization
private readonly IUser _user;
private readonly IDialogDbContext _dialogDbContext;
private readonly ILogger _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;

private static readonly JsonSerializerOptions SerializerOptions = new()
{
Expand All @@ -39,7 +42,8 @@ public AltinnAuthorizationClient(
IFusionCacheProvider cacheProvider,
IUser user,
IDialogDbContext dialogDbContext,
ILogger<AltinnAuthorizationClient> logger)
ILogger<AltinnAuthorizationClient> logger,
IServiceScopeFactory serviceScopeFactory)
{
_httpClient = client ?? throw new ArgumentNullException(nameof(client));
_pdpCache = cacheProvider.GetCache(nameof(Authorization)) ?? throw new ArgumentNullException(nameof(cacheProvider));
Expand All @@ -48,6 +52,7 @@ public AltinnAuthorizationClient(
_user = user ?? throw new ArgumentNullException(nameof(user));
_dialogDbContext = dialogDbContext ?? throw new ArgumentNullException(nameof(dialogDbContext));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
}

public async Task<DialogDetailsAuthorizationResult> GetDialogDetailsAuthorization(
Expand Down Expand Up @@ -180,10 +185,13 @@ await AuthorizationHelper.CollapseSubjectResources(

return dialogSearchAuthorizationResult;
}

private async Task<List<SubjectResource>> GetAllSubjectResources(CancellationToken cancellationToken) =>
await _subjectResourcesCache.GetOrSetAsync(nameof(SubjectResource), async ct
=> await _dialogDbContext.SubjectResources.ToListAsync(cancellationToken: ct),
await _subjectResourcesCache.GetOrSetAsync(nameof(SubjectResource), async ct =>
{
using var scope = _serviceScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<DialogDbContext>();
return await dbContext.SubjectResources.ToListAsync(cancellationToken: ct);
},
token: cancellationToken);

private async Task<DialogDetailsAuthorizationResult> PerformDialogDetailsAuthorization(
Expand Down

0 comments on commit 0b0672b

Please sign in to comment.