-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from cnblogs/add-event-queue
feat: add event buffer
- Loading branch information
Showing
32 changed files
with
526 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/BufferedIntegrationEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// The integration event stored in buffer. | ||
/// </summary> | ||
/// <param name="Name">The event name.</param> | ||
/// <param name="Event">The event data.</param> | ||
public record BufferedIntegrationEvent(string Name, IntegrationEvent Event); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/EventBusOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Options for event bus. | ||
/// </summary> | ||
public class EventBusOptions | ||
{ | ||
/// <summary> | ||
/// The service collection for | ||
/// </summary> | ||
public IServiceCollection? Services { get; set; } | ||
|
||
/// <summary> | ||
/// Interval for publish integration event. | ||
/// </summary> | ||
public int Interval { get; set; } = 1; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/EventBusOptionsBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Builder for <see cref="EventBusOptions"/>. | ||
/// </summary> | ||
public class EventBusOptionsBuilder | ||
{ | ||
/// <summary> | ||
/// Create a <see cref="EventBusOptionsBuilder"/>. | ||
/// </summary> | ||
/// <param name="services"></param> | ||
public EventBusOptionsBuilder(IServiceCollection services) | ||
{ | ||
Services = services; | ||
} | ||
|
||
/// <summary> | ||
/// Internal service collection. | ||
/// </summary> | ||
public IServiceCollection Services { get; } | ||
|
||
/// <summary> | ||
/// The interval in milliseconds for checking pending integration events. | ||
/// </summary> | ||
public int Interval { get; set; } = 1000; | ||
|
||
internal Action<EventBusOptions> GetConfiguration() | ||
{ | ||
return o => | ||
{ | ||
o.Interval = Interval; | ||
}; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/EventBusServiceInjector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Reflection; | ||
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Extension methods for injecting <see cref="IEventBus"/> to service collection. | ||
/// </summary> | ||
public static class EventBusServiceInjector | ||
{ | ||
/// <summary> | ||
/// Add event bus for integration event support. | ||
/// </summary> | ||
/// <param name="services">The services.</param> | ||
/// <param name="configuration">Extra configurations for event bus.</param> | ||
/// <param name="handlerAssemblies">The assemblies for handlers.</param> | ||
/// <returns><see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddEventBus( | ||
this IServiceCollection services, | ||
Action<EventBusOptionsBuilder>? configuration = null, | ||
params Assembly[] handlerAssemblies) | ||
{ | ||
services.TryAddSingleton<IEventBuffer, InMemoryEventBuffer>(); | ||
services.TryAddScoped<IEventBus, DefaultEventBus>(); | ||
services.AddHostedService<PublishIntegrationEventHostedService>(); | ||
var builder = new EventBusOptionsBuilder(services); | ||
configuration?.Invoke(builder); | ||
services.Configure(builder.GetConfiguration()); | ||
if (handlerAssemblies.Length > 0) | ||
{ | ||
services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(handlerAssemblies)); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Add event bus for integration event support. | ||
/// </summary> | ||
/// <param name="cqrsInjector">The <see cref="CqrsInjector"/>.</param> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="handlerAssemblies">The assemblies for handlers.</param> | ||
/// <returns></returns> | ||
public static CqrsInjector AddEventBus( | ||
this CqrsInjector cqrsInjector, | ||
Action<EventBusOptionsBuilder>? configuration = null, | ||
params Assembly[] handlerAssemblies) | ||
{ | ||
cqrsInjector.Services.AddEventBus(configuration, handlerAssemblies); | ||
return cqrsInjector; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/IEventBuffer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Buffer for integration events. | ||
/// </summary> | ||
public interface IEventBuffer | ||
{ | ||
/// <summary> | ||
/// Number of pending events. | ||
/// </summary> | ||
int Count { get; } | ||
|
||
/// <summary> | ||
/// Add an event to buffer. | ||
/// </summary> | ||
/// <param name="name">The name of integration event.</param> | ||
/// <param name="event">The event.</param> | ||
/// <typeparam name="TEvent">The type of integration event.</typeparam> | ||
void Add<TEvent>(string name, TEvent @event) | ||
where TEvent : IntegrationEvent; | ||
|
||
/// <summary> | ||
/// Get an integration event without removing it. | ||
/// </summary> | ||
/// <returns>The integration event, <c>null</c> will be returned if buffer is empty.</returns> | ||
BufferedIntegrationEvent? Peek(); | ||
|
||
/// <summary> | ||
/// Get an integration event and remove it. | ||
/// </summary> | ||
/// <returns>The integration event, <c>null</c> will be returned if buffer is empty.</returns> | ||
BufferedIntegrationEvent? Pop(); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/IEventBusProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Provider contract for event bus. | ||
/// </summary> | ||
public interface IEventBusProvider | ||
{ | ||
/// <summary> | ||
/// Emit an integration event. | ||
/// </summary> | ||
/// <param name="eventName">The name of the event.</param> | ||
/// <param name="event">The event body.</param> | ||
/// <returns></returns> | ||
Task PublishAsync(string eventName, IntegrationEvent @event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Cnblogs.Architecture.Ddd.EventBus.Abstractions/InMemoryEventBuffer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
|
||
/// <summary> | ||
/// Implementation of <see cref="IEventBuffer"/> using <see cref="ConcurrentQueue{T}"/>. | ||
/// </summary> | ||
public class InMemoryEventBuffer : IEventBuffer | ||
{ | ||
private readonly ConcurrentQueue<BufferedIntegrationEvent> _queue = new(); | ||
|
||
/// <inheritdoc /> | ||
public int Count => _queue.Count; | ||
|
||
/// <inheritdoc /> | ||
public void Add<TEvent>(string name, TEvent @event) | ||
where TEvent : IntegrationEvent | ||
{ | ||
_queue.Enqueue(new BufferedIntegrationEvent(name, @event)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public BufferedIntegrationEvent? Peek() | ||
{ | ||
return _queue.TryPeek(out var @event) ? @event : null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public BufferedIntegrationEvent? Pop() | ||
{ | ||
return _queue.TryDequeue(out var @event) ? @event : null; | ||
} | ||
} |
Oops, something went wrong.