Skip to content

Commit

Permalink
throts
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed Sep 24, 2024
1 parent cdceec2 commit 0270b64
Showing 1 changed file with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Digdir.Library.Entity.Abstractions.Features.EventPublisher;
using HotChocolate.Subscriptions;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Constants = Digdir.Domain.Dialogporten.Infrastructure.GraphQl.GraphQlSubscriptionConstants;

Expand All @@ -16,6 +17,8 @@ internal sealed class ConvertDomainEventsToOutboxMessagesInterceptor : SaveChang
private readonly ITransactionTime _transactionTime;
private readonly ITopicEventSender _topicEventSender;
private readonly ILogger<ConvertDomainEventsToOutboxMessagesInterceptor> _logger;
private static readonly MemoryCache UpdateEventThrottleCache = new(new MemoryCacheOptions());


private List<IDomainEvent> _domainEvents = [];

Expand Down Expand Up @@ -69,18 +72,14 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent
{
try
{
// If you are adding any additional cases to this switch,
// please consider making the running of the tasks parallel
var task = domainEvent switch
{
DialogUpdatedDomainEvent dialogUpdatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogUpdatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogUpdatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
DialogUpdatedDomainEvent dialogUpdatedDomainEvent =>
SendDialogUpdated(dialogUpdatedDomainEvent.DialogId, cancellationToken),

DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent =>
SendDialogUpdated(dialogActivityCreatedDomainEvent.DialogId, cancellationToken),

DialogDeletedDomainEvent dialogDeletedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogDeletedDomainEvent.DialogId}",
new DialogEventPayload
Expand All @@ -89,14 +88,6 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent
Type = DialogEventType.DialogDeleted
},
cancellationToken),
DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogActivityCreatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogActivityCreatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
_ => ValueTask.CompletedTask
};

Expand All @@ -110,4 +101,23 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent

return await base.SavedChangesAsync(eventData, result, cancellationToken);
}

private async ValueTask SendDialogUpdated(Guid dialogId, CancellationToken cancellationToken)
{
if (UpdateEventThrottleCache.TryGetValue(dialogId, out _))
{
return;
}

UpdateEventThrottleCache.Set(dialogId, true, TimeSpan.FromMilliseconds(50));

await _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogId}",
new DialogEventPayload
{
Id = dialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken);
}
}

0 comments on commit 0270b64

Please sign in to comment.