dotnet-cqrs/Svrnty.CQRS.Events.ConsumerGroups.Abstractions/ConsumerGroupOptions.cs

95 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
namespace Svrnty.CQRS.Events.ConsumerGroups.Abstractions;
/// <summary>
/// Configuration options for consumer group behavior.
/// </summary>
public class ConsumerGroupOptions
{
/// <summary>
/// Number of events to fetch in each batch from the stream.
/// Higher values improve throughput but increase memory usage.
/// Default: 100.
/// </summary>
public int BatchSize { get; set; } = 100;
/// <summary>
/// Polling interval when no events are available in the stream.
/// The consumer will sleep for this duration before checking for new events.
/// Default: 1 second.
/// </summary>
public TimeSpan PollingInterval { get; set; } = TimeSpan.FromSeconds(1);
/// <summary>
/// Offset commit strategy.
/// Determines when and how offsets are committed.
/// Default: AfterBatch.
/// </summary>
public OffsetCommitStrategy CommitStrategy { get; set; } = OffsetCommitStrategy.AfterBatch;
/// <summary>
/// Interval for periodic offset commits when using OffsetCommitStrategy.Periodic.
/// Ignored for other commit strategies.
/// Default: 5 seconds.
/// </summary>
public TimeSpan PeriodicCommitInterval { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>
/// Heartbeat interval for consumer liveness.
/// The consumer will send heartbeats at this interval to signal it's alive.
/// Must be less than SessionTimeout.
/// Default: 10 seconds.
/// </summary>
public TimeSpan HeartbeatInterval { get; set; } = TimeSpan.FromSeconds(10);
/// <summary>
/// Consumer session timeout.
/// If a consumer doesn't send a heartbeat within this period, it's considered dead
/// and will be removed from the group.
/// Must be greater than HeartbeatInterval.
/// Default: 30 seconds.
/// </summary>
public TimeSpan SessionTimeout { get; set; } = TimeSpan.FromSeconds(30);
/// <summary>
/// Whether to start from the beginning of the stream if no offset has been committed.
/// If false, starts from the current end of the stream (only new events).
/// Default: true (start from beginning).
/// </summary>
public bool StartFromBeginning { get; set; } = true;
/// <summary>
/// Optional metadata to include with consumer registration.
/// Useful for debugging and monitoring (e.g., hostname, version, environment).
/// </summary>
public IReadOnlyDictionary<string, string>? Metadata { get; set; }
/// <summary>
/// Validates the options and throws if invalid.
/// </summary>
/// <exception cref="ArgumentException">Thrown if options are invalid.</exception>
public void Validate()
{
if (BatchSize <= 0)
throw new ArgumentException("BatchSize must be greater than 0", nameof(BatchSize));
if (PollingInterval <= TimeSpan.Zero)
throw new ArgumentException("PollingInterval must be greater than 0", nameof(PollingInterval));
if (HeartbeatInterval <= TimeSpan.Zero)
throw new ArgumentException("HeartbeatInterval must be greater than 0", nameof(HeartbeatInterval));
if (SessionTimeout <= HeartbeatInterval)
throw new ArgumentException(
"SessionTimeout must be greater than HeartbeatInterval",
nameof(SessionTimeout));
if (CommitStrategy == OffsetCommitStrategy.Periodic && PeriodicCommitInterval <= TimeSpan.Zero)
throw new ArgumentException(
"PeriodicCommitInterval must be greater than 0 when using Periodic commit strategy",
nameof(PeriodicCommitInterval));
}
}