mirror of
https://github.com/bitwarden/server.git
synced 2025-05-11 06:32:22 -05:00
[PM-17562] Revert event route optimization (#5766)
This commit is contained in:
parent
75a2da3c4b
commit
4b49b04409
@ -0,0 +1,34 @@
|
|||||||
|
using Bit.Core.Models.Data;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Bit.Core.Services;
|
||||||
|
|
||||||
|
public class EventRouteService(
|
||||||
|
[FromKeyedServices("broadcast")] IEventWriteService broadcastEventWriteService,
|
||||||
|
[FromKeyedServices("storage")] IEventWriteService storageEventWriteService,
|
||||||
|
IFeatureService _featureService) : IEventWriteService
|
||||||
|
{
|
||||||
|
public async Task CreateAsync(IEvent e)
|
||||||
|
{
|
||||||
|
if (_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations))
|
||||||
|
{
|
||||||
|
await broadcastEventWriteService.CreateAsync(e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await storageEventWriteService.CreateAsync(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CreateManyAsync(IEnumerable<IEvent> e)
|
||||||
|
{
|
||||||
|
if (_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations))
|
||||||
|
{
|
||||||
|
await broadcastEventWriteService.CreateManyAsync(e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await storageEventWriteService.CreateManyAsync(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using Bit.Core;
|
|
||||||
using Bit.Core.AdminConsole.Services.Implementations;
|
using Bit.Core.AdminConsole.Services.Implementations;
|
||||||
using Bit.Core.AdminConsole.Services.NoopImplementations;
|
using Bit.Core.AdminConsole.Services.NoopImplementations;
|
||||||
using Bit.Core.Context;
|
using Bit.Core.Context;
|
||||||
@ -94,13 +93,7 @@ public class Startup
|
|||||||
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
services.AddScoped<IEventWriteService>(sp =>
|
services.AddScoped<IEventWriteService, EventRouteService>();
|
||||||
{
|
|
||||||
var featureService = sp.GetRequiredService<IFeatureService>();
|
|
||||||
var key = featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations)
|
|
||||||
? "broadcast" : "storage";
|
|
||||||
return sp.GetRequiredKeyedService<IEventWriteService>(key);
|
|
||||||
});
|
|
||||||
services.AddScoped<IEventService, EventService>();
|
services.AddScoped<IEventService, EventService>();
|
||||||
|
|
||||||
services.AddOptionality();
|
services.AddOptionality();
|
||||||
|
@ -4,7 +4,6 @@ using System.Security.Claims;
|
|||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using AspNetCoreRateLimit;
|
using AspNetCoreRateLimit;
|
||||||
using Azure.Storage.Queues;
|
using Azure.Storage.Queues;
|
||||||
using Bit.Core;
|
|
||||||
using Bit.Core.AdminConsole.Models.Business.Tokenables;
|
using Bit.Core.AdminConsole.Models.Business.Tokenables;
|
||||||
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
||||||
using Bit.Core.AdminConsole.Services;
|
using Bit.Core.AdminConsole.Services;
|
||||||
@ -366,13 +365,7 @@ public static class ServiceCollectionExtensions
|
|||||||
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("storage");
|
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("storage");
|
||||||
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
||||||
}
|
}
|
||||||
services.AddScoped<IEventWriteService>(sp =>
|
services.AddScoped<IEventWriteService, EventRouteService>();
|
||||||
{
|
|
||||||
var featureService = sp.GetRequiredService<IFeatureService>();
|
|
||||||
var key = featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations)
|
|
||||||
? "broadcast" : "storage";
|
|
||||||
return sp.GetRequiredKeyedService<IEventWriteService>(key);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
|
if (CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
using Bit.Core.Models.Data;
|
||||||
|
using Bit.Core.Services;
|
||||||
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
using NSubstitute;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.Services;
|
||||||
|
|
||||||
|
[SutProviderCustomize]
|
||||||
|
public class EventRouteServiceTests
|
||||||
|
{
|
||||||
|
private readonly IEventWriteService _broadcastEventWriteService = Substitute.For<IEventWriteService>();
|
||||||
|
private readonly IEventWriteService _storageEventWriteService = Substitute.For<IEventWriteService>();
|
||||||
|
private readonly IFeatureService _featureService = Substitute.For<IFeatureService>();
|
||||||
|
private readonly EventRouteService Subject;
|
||||||
|
|
||||||
|
public EventRouteServiceTests()
|
||||||
|
{
|
||||||
|
Subject = new EventRouteService(_broadcastEventWriteService, _storageEventWriteService, _featureService);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateAsync_FlagDisabled_EventSentToStorageService(EventMessage eventMessage)
|
||||||
|
{
|
||||||
|
_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations).Returns(false);
|
||||||
|
|
||||||
|
await Subject.CreateAsync(eventMessage);
|
||||||
|
|
||||||
|
_broadcastEventWriteService.DidNotReceiveWithAnyArgs().CreateAsync(Arg.Any<EventMessage>());
|
||||||
|
_storageEventWriteService.Received(1).CreateAsync(eventMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateAsync_FlagEnabled_EventSentToBroadcastService(EventMessage eventMessage)
|
||||||
|
{
|
||||||
|
_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations).Returns(true);
|
||||||
|
|
||||||
|
await Subject.CreateAsync(eventMessage);
|
||||||
|
|
||||||
|
_broadcastEventWriteService.Received(1).CreateAsync(eventMessage);
|
||||||
|
_storageEventWriteService.DidNotReceiveWithAnyArgs().CreateAsync(Arg.Any<EventMessage>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateManyAsync_FlagDisabled_EventsSentToStorageService(IEnumerable<EventMessage> eventMessages)
|
||||||
|
{
|
||||||
|
_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations).Returns(false);
|
||||||
|
|
||||||
|
await Subject.CreateManyAsync(eventMessages);
|
||||||
|
|
||||||
|
_broadcastEventWriteService.DidNotReceiveWithAnyArgs().CreateManyAsync(Arg.Any<IEnumerable<EventMessage>>());
|
||||||
|
_storageEventWriteService.Received(1).CreateManyAsync(eventMessages);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateManyAsync_FlagEnabled_EventsSentToBroadcastService(IEnumerable<EventMessage> eventMessages)
|
||||||
|
{
|
||||||
|
_featureService.IsEnabled(FeatureFlagKeys.EventBasedOrganizationIntegrations).Returns(true);
|
||||||
|
|
||||||
|
await Subject.CreateManyAsync(eventMessages);
|
||||||
|
|
||||||
|
_broadcastEventWriteService.Received(1).CreateManyAsync(eventMessages);
|
||||||
|
_storageEventWriteService.DidNotReceiveWithAnyArgs().CreateManyAsync(Arg.Any<IEnumerable<EventMessage>>());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user