Skip to content

imihalcea/MsgFlux

Repository files navigation

MsgFlux

MsgFlux is a lightweight in-process messaging library for .NET, designed to facilitate asynchronous communication between components via a producer-consumer model. It natively integrates resilience (via Polly) and observability (via OpenTelemetry).

Features

  • In-process message bus: Decoupled communication between components.
  • Pub/Sub Model: Message publication and consumption via typed handlers.
  • Built-in Resilience: Uses Polly for retry management (automatic retries on failure).
  • Observability: OpenTelemetry support (ActivitySource "MsgFlux") for distributed tracing.
  • Dependency Injection: Seamless integration with Microsoft.Extensions.DependencyInjection.
  • Asynchronous Processing: Uses System.Threading.Channels for efficient, non-blocking processing.

Installation

(To be completed with specific installation instructions, e.g., via NuGet if published)

Usage

1. Configuration

Add MsgFlux to your service container in Program.cs or Startup.cs. You must specify the assemblies containing your consumers.

using MsgFlux.Core;

// ...

builder.Services.AddMsgFlux(typeof(Program).Assembly);

2. Defining a Message

A message can be any class or record.

public record UserCreated(string UserId, string Email);

3. Creating a Consumer

Implement the IConsume<T> interface to define how to process a message.

using MsgFlux.Core;

public class UserCreatedConsumer : IConsume<UserCreated>
{
    private readonly ILogger<UserCreatedConsumer> _logger;

    public UserCreatedConsumer(ILogger<UserCreatedConsumer> logger)
    {
        _logger = logger;
    }

    public async Task HandleAsync(UserCreated message, CancellationToken ct)
    {
        _logger.LogInformation("New user created: {UserId}, Email: {Email}", message.UserId, message.Email);
        await Task.CompletedTask;
    }
}

4. Publishing a Message

Inject IPublish to send messages.

using MsgFlux.Core;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
    private readonly IPublish _publisher;

    public UserController(IPublish publisher)
    {
        _publisher = publisher;
    }

    [HttpPost]
    public async Task<IActionResult> CreateUser([FromBody] CreateUserRequest request)
    {
        // Creation logic...
        var userId = Guid.NewGuid().ToString();

        // Publishing the event
        await _publisher.PublishAsync(new UserCreated(userId, request.Email));

        return Ok(new { UserId = userId });
    }
}

Architecture

  • Engine: Hosted service (BackgroundService) that listens to channels and distributes messages to the appropriate consumers.
  • Publisher: Service responsible for serializing and sending messages into channels.
  • Registry: Maintains the list of message types and associated consumers.
  • RxTx: Abstraction over System.Threading.Channels for message transmission.

Resilience

MsgFlux uses a default resilience pipeline configured with:

  • 3 retry attempts.
  • Exponential backoff starting at 200ms.

License

See the LICENSE file.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages