mirror of
https://github.com/bitwarden/server
synced 2025-12-22 03:03:33 +00:00
* Initial POC of Distributed Events * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * Clean up files to support accepted changes. Address PR Feedback * Removed unneeded using to fix lint warning * Moved config into a common EventLogging top-level item. Fixed issues from PR review * Optimized per suggestion from justinbaur Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * Updated to add IAsyncDisposable as suggested in PR review * Updated with suggestion to use KeyedSingleton for the IEventWriteService * Changed key case to lowercase --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
using System.Text.Json;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Settings;
|
|
using RabbitMQ.Client;
|
|
|
|
namespace Bit.Core.Services;
|
|
public class RabbitMqEventWriteService : IEventWriteService, IAsyncDisposable
|
|
{
|
|
private readonly ConnectionFactory _factory;
|
|
private readonly Lazy<Task<IConnection>> _lazyConnection;
|
|
private readonly string _exchangeName;
|
|
|
|
public RabbitMqEventWriteService(GlobalSettings globalSettings)
|
|
{
|
|
_factory = new ConnectionFactory
|
|
{
|
|
HostName = globalSettings.EventLogging.RabbitMq.HostName,
|
|
UserName = globalSettings.EventLogging.RabbitMq.Username,
|
|
Password = globalSettings.EventLogging.RabbitMq.Password
|
|
};
|
|
_exchangeName = globalSettings.EventLogging.RabbitMq.ExchangeName;
|
|
|
|
_lazyConnection = new Lazy<Task<IConnection>>(CreateConnectionAsync);
|
|
}
|
|
|
|
public async Task CreateAsync(IEvent e)
|
|
{
|
|
var connection = await _lazyConnection.Value;
|
|
using var channel = await connection.CreateChannelAsync();
|
|
|
|
await channel.ExchangeDeclareAsync(exchange: _exchangeName, type: ExchangeType.Fanout, durable: true);
|
|
|
|
var body = JsonSerializer.SerializeToUtf8Bytes(e);
|
|
|
|
await channel.BasicPublishAsync(exchange: _exchangeName, routingKey: string.Empty, body: body);
|
|
}
|
|
|
|
public async Task CreateManyAsync(IEnumerable<IEvent> events)
|
|
{
|
|
var connection = await _lazyConnection.Value;
|
|
using var channel = await connection.CreateChannelAsync();
|
|
await channel.ExchangeDeclareAsync(exchange: _exchangeName, type: ExchangeType.Fanout, durable: true);
|
|
|
|
foreach (var e in events)
|
|
{
|
|
var body = JsonSerializer.SerializeToUtf8Bytes(e);
|
|
|
|
await channel.BasicPublishAsync(exchange: _exchangeName, routingKey: string.Empty, body: body);
|
|
}
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_lazyConnection.IsValueCreated)
|
|
{
|
|
var connection = await _lazyConnection.Value;
|
|
await connection.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
private async Task<IConnection> CreateConnectionAsync()
|
|
{
|
|
return await _factory.CreateConnectionAsync();
|
|
}
|
|
}
|