mirror of
https://github.com/bitwarden/server.git
synced 2025-04-26 07:12:20 -05:00
Optimization that removes the need for EventRouteService
This commit is contained in:
parent
8d2502d8e6
commit
24750138b3
@ -1,34 +0,0 @@
|
|||||||
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,4 +1,5 @@
|
|||||||
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;
|
||||||
@ -93,7 +94,13 @@ public class Startup
|
|||||||
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
services.AddKeyedSingleton<IEventWriteService, NoopEventWriteService>("broadcast");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
services.AddScoped<IEventWriteService, EventRouteService>();
|
services.AddScoped<IEventWriteService>(sp =>
|
||||||
|
{
|
||||||
|
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,6 +4,7 @@ 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;
|
||||||
@ -365,7 +366,13 @@ 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, EventRouteService>();
|
services.AddScoped<IEventWriteService>(sp =>
|
||||||
|
{
|
||||||
|
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))
|
||||||
{
|
{
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
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