Path: redb-route/redb.Route.Mail/MailIdempotency.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Mail/MailIdempotency.cs
----
using redb.Route.Abstractions;
using redb.Route.Components;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Mail;
///
/// The idempotency of a mail consumer, as Apache Camel's mail consumer keeps it: a message's key is added to the
/// repository before its route and the message is skipped when the repository already holds it; a failed exchange
/// removes the key, so the message is routed again; a successful one confirms it, or removes it when
/// says so.
///
internal sealed class MailIdempotency
{
private readonly IIdempotentRepository _repository;
private readonly bool _removeOnCommit;
private MailIdempotency(IIdempotentRepository repository, bool removeOnCommit)
{
_repository = repository;
_removeOnCommit = removeOnCommit;
}
///
/// The idempotency the options ask for: the registered ,
/// else the consumer's own in-memory set for , else none.
///
public static MailIdempotency? Create(MailEndpointOptions options, IEndpoint endpoint)
{
if (!string.IsNullOrEmpty(options.IdempotentRepository))
{
var context = (endpoint.Component as ComponentBase)?.Context
?? throw new InvalidOperationException(
$"idempotentRepository '{options.IdempotentRepository}' needs a route context: register the " +
"repository with context.AddIdempotentRepository(name, repository).");
return new MailIdempotency(
context.GetIdempotentRepositoryProvider().Get(options.IdempotentRepository),
options.IdempotentRepositoryRemoveOnCommit);
}
// idempotent=true: the messages this consumer routed successfully are skipped until it restarts.
return options.Idempotent ? new MailIdempotency(new InMemoryIdempotentRepository(), removeOnCommit: false) : null;
}
/// Adds the key before the route; false when the repository already holds it (a duplicate).
public Task TryAdd(string key, CancellationToken ct) => _repository.Add(key, ct);
/// The exchange succeeded and the message is committed.
public Task Commit(string key, CancellationToken ct) =>
_removeOnCommit ? _repository.Remove(key, ct) : _repository.Confirm(key, ct);
/// The exchange failed and the message is left as it is: it may be routed again.
public Task Rollback(string key, CancellationToken ct) => _repository.Remove(key, ct);
}