Path: redb-route/redb.Route.Sqs/SqsComponent.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.Sqs/SqsComponent.cs ---- using Amazon.SQS; using Amazon.SQS.Model; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Extensions; using redb.Route.Core; namespace redb.Route.Sqs; /// /// Amazon SQS transport component for redb.Route. Scheme: sqs. /// Consumer: sqs://my-queue?waitTimeSeconds=20&concurrentConsumers=4 /// Producer: sqs://my-queue?region=us-east-1 (send to the queue). /// FIFO queues are addressed by their .fifo name; LocalStack/ElasticMQ via serviceUrl=. /// public sealed class SqsComponent : ComponentBase { /// public override string Scheme => "sqs"; /// public override IEndpoint CreateEndpoint(EndpointUri uri) { ArgumentNullException.ThrowIfNull(uri); var options = new SqsEndpointOptions(); options.BindFromUri(uri.RawParameters); options.Validate(); options.ResolveBatch(EndpointUri.Sanitize(uri.BaseKey)); return new SqsEndpoint(uri, this, options); } } /// /// SQS endpoint — owns the shared client and resolves the queue URL /// (creating the queue on demand when autoCreateQueue=true). /// public sealed class SqsEndpoint : EndpointBase, IDisposable { private IAmazonSQS? _client; private readonly SemaphoreSlim _clientLock = new(1, 1); private string? _queueUrl; private readonly SemaphoreSlim _urlLock = new(1, 1); /// Queue name parsed from the URI path. public string QueueName { get; } /// True when the queue is FIFO (the name or an explicit queue URL ends in .fifo). public bool IsFifo => QueueName.EndsWith(".fifo", StringComparison.Ordinal) || Options.QueueUrl.EndsWith(".fifo", StringComparison.Ordinal); /// Logger from the parent component. internal ILogger? Logger { get; } /// Typed options (exposed for consumer/producer). internal SqsEndpointOptions EndpointOptions => Options; /// Creates an SQS endpoint. public SqsEndpoint(EndpointUri uri, SqsComponent component, SqsEndpointOptions options) : base(uri, component, options) { Logger = component.Logger; QueueName = uri.Path.TrimStart('/'); if (string.IsNullOrEmpty(QueueName) && string.IsNullOrEmpty(options.QueueUrl)) throw new ArgumentException("SQS URI must include a queue name in the path (sqs://queue-name)."); _headerFilter = new EndpointHeaderFilter(this, options.HeaderFilterStrategy); } private readonly EndpointHeaderFilter _headerFilter; /// /// The header filter strategy of this endpoint: the one headerFilterStrategy=#name names, or /// . A name the registry does not hold fails when the producer or /// consumer is built. /// internal IHeaderFilterStrategy HeaderFilterStrategy => _headerFilter.Or(SqsHeaderFilterStrategy.Default); /// public override IProducer CreateProducer() => new SqsProducer(this, Options); /// public override IConsumer CreateConsumer(IProcessor processor) { ArgumentNullException.ThrowIfNull(processor); return new SqsConsumer(this, processor, Options); } /// Gets or creates the shared client. internal async Task GetOrCreateClientAsync(CancellationToken ct = default) { if (_client is not null) return _client; await _clientLock.WaitAsync(ct).ConfigureAwait(false); try { if (_client is not null) return _client; if (!string.IsNullOrEmpty(Options.ConnectionFactory)) { // A set-but-unknown name fails loud — never a silent fallback. var context = (Component as SqsComponent)?.Context; _client = context.GetRequiredFromRegistry(Options.ConnectionFactory).BuildSqs(); } else { var config = new AmazonSQSConfig(); AwsClientSupport.ApplyCommonConfig(config, Options); _client = new AmazonSQSClient(AwsClientSupport.ResolveCredentials(Options), config); } var (region, serviceUrl) = AwsClientSupport.Target(_client.Config); Logger?.LogInformation("SQS client created: Region={Region}, ServiceUrl={ServiceUrl}, Queue={Queue}, ConnectionFactory={ConnectionFactory}", region, serviceUrl, QueueName, string.IsNullOrEmpty(Options.ConnectionFactory) ? "-" : Options.ConnectionFactory); return _client; } finally { _clientLock.Release(); } } /// Resolves (and caches) the queue URL, creating the queue first when autoCreateQueue=true. internal async Task GetQueueUrlAsync(CancellationToken ct = default) { if (_queueUrl is not null) return _queueUrl; await _urlLock.WaitAsync(ct).ConfigureAwait(false); try { if (_queueUrl is not null) return _queueUrl; if (!string.IsNullOrEmpty(Options.QueueUrl)) return _queueUrl = Options.QueueUrl; var client = await GetOrCreateClientAsync(ct).ConfigureAwait(false); if (Options.AutoCreateQueue) { var request = new CreateQueueRequest { QueueName = QueueName }; if (IsFifo) { // AWS SDK v4 does not initialize request collections — assign the dictionary. request.Attributes = new Dictionary { ["FifoQueue"] = "true", ["ContentBasedDeduplication"] = "true", }; } var created = await client.CreateQueueAsync(request, ct).ConfigureAwait(false); return _queueUrl = created.QueueUrl; } var resp = await client.GetQueueUrlAsync(QueueName, ct).ConfigureAwait(false); return _queueUrl = resp.QueueUrl; } finally { _urlLock.Release(); } } /// public override async Task Stop(CancellationToken ct = default) { if (_client is not null) { try { _client.Dispose(); } catch (Exception ex) { Logger?.LogWarning(ex, "Error disposing SQS client"); } _client = null; } Logger?.LogInformation("SQS endpoint stopped: {Uri}", EndpointUri.ForLog(Uri.ToString(), Logger)); await Task.CompletedTask.ConfigureAwait(false); } /// public void Dispose() { _client?.Dispose(); _client = null; _clientLock.Dispose(); _urlLock.Dispose(); } }