Skip to content

Commit d18bb76

Browse files
elsandoskogstad
andauthored
fix: Reenable party list cache, log party name look failure with negative cache TTL (#1395)
## Description Party list cache was by a mistake disabled, this reenables it. Also, add a log entry when party name lookup fails due to remote API response indicates no name (usually due to missing party profile). Cache negative responses for just 10 seconds (instead of the default 24 hours). ## Related Issue(s) - N/A ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced caching for retrieving authorized parties, improving efficiency. - Introduced logging capabilities in the Party Name Registry client for better error tracking. - **Bug Fixes** - Improved error handling in the Party Name Registry client to log issues with missing names. - **Refactor** - Updated caching strategy in the Party Name Registry client for optimized performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Ole Jørgen Skogstad <skogstad@softis.net>
1 parent 304f4da commit d18bb76

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ public async Task<AuthorizedPartiesResult> GetAuthorizedParties(IPartyIdentifier
8181
CancellationToken cancellationToken = default)
8282
{
8383
var authorizedPartiesRequest = new AuthorizedPartiesRequest(authenticatedParty);
84-
// Disabled until https://github.com/digdir/dialogporten/issues/1226 is fixed
85-
// var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token
86-
// => await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);
87-
var authorizedParties = await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, cancellationToken);
84+
var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token
85+
=> await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);
86+
8887
return flatten ? GetFlattenedAuthorizedParties(authorizedParties) : authorizedParties;
8988
}
9089

src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/NameRegistry/PartyNameRegistryClient.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Digdir.Domain.Dialogporten.Application.Externals;
55
using Digdir.Domain.Dialogporten.Domain.Parties;
66
using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions;
7+
using Microsoft.Extensions.Logging;
78
using ZiggyCreatures.Caching.Fusion;
89

910
namespace Digdir.Domain.Dialogporten.Infrastructure.Altinn.NameRegistry;
@@ -12,24 +13,36 @@ internal sealed class PartyNameRegistryClient : IPartyNameRegistry
1213
{
1314
private readonly IFusionCache _cache;
1415
private readonly HttpClient _client;
16+
private readonly ILogger<PartyNameRegistryClient> _logger;
1517

1618
private static readonly JsonSerializerOptions SerializerOptions = new()
1719
{
1820
PropertyNameCaseInsensitive = true,
1921
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
2022
};
2123

22-
public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider)
24+
public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider, ILogger<PartyNameRegistryClient> logger)
2325
{
2426
_client = client ?? throw new ArgumentNullException(nameof(client));
27+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2528
_cache = cacheProvider.GetCache(nameof(NameRegistry)) ?? throw new ArgumentNullException(nameof(cacheProvider));
2629
}
2730

2831
public async Task<string?> GetName(string externalIdWithPrefix, CancellationToken cancellationToken)
2932
{
30-
return await _cache.GetOrSetAsync(
33+
return await _cache.GetOrSetAsync<string?>(
3134
$"Name_{externalIdWithPrefix}",
32-
ct => GetNameFromRegister(externalIdWithPrefix, ct),
35+
async (ctx, ct) =>
36+
{
37+
var name = await GetNameFromRegister(externalIdWithPrefix, ct);
38+
if (name is null)
39+
{
40+
// Short negative cache
41+
ctx.Options.Duration = TimeSpan.FromSeconds(10);
42+
}
43+
44+
return name;
45+
},
3346
token: cancellationToken);
3447
}
3548

@@ -48,7 +61,14 @@ public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProv
4861
serializerOptions: SerializerOptions,
4962
cancellationToken: cancellationToken);
5063

51-
return nameLookupResult.PartyNames.FirstOrDefault()?.Name;
64+
var name = nameLookupResult.PartyNames.FirstOrDefault()?.Name;
65+
if (name is null)
66+
{
67+
// This is PII, but this is an error condition (probably due to missing Altinn profile)
68+
_logger.LogError("Failed to get name from party name registry for external id {ExternalId}", externalIdWithPrefix);
69+
}
70+
71+
return name;
5272
}
5373

5474
private static bool TryParse(string externalIdWithPrefix, [NotNullWhen(true)] out NameLookup? nameLookup)

0 commit comments

Comments
 (0)