Conversation
- Security: Add MessageTypeResolver allowlist for type deserialization, replacing unsafe Type.GetType() in DistributedNotificationWorker - Add PubSubEntry type to align IPubSubClient.PublishAsync with QueueEntry pattern - Add IAsyncDisposable to IQueueClient and IPubSubClient interfaces - Make IQueueClient.DeadLetterAsync a required method (no silent default) - Replace ContinueWith with async/await in IQueueJobStateStore defaults - Cache queue metadata in QueueMiddleware via ConcurrentDictionary keyed by DescriptorId, eliminating per-call reflection - Thread CancellationToken through QueueMiddleware to SendAsync/SetJobStateAsync - Replace lock(Random) with Random.Shared in QueueWorker retry delay - Rename DistributedOptions to DistributedQueueOptions for clarity - Rename AddSnsSqsPubSubClient to AddMediatorSnsSqsPubSub for naming consistency - Update all test files for new PubSubEntry API
This reverts commit a25913d.
# Conflicts: # samples/CleanArchitectureSample/src/Api/Api.csproj
- Fix potential null dereference in InMemoryQueueJobStateStore.GetJobStateAsync by separating TryGetValue and IsExpired checks into distinct branches - Fix double-checked locking false positive in SqsPubSubClient.EnsureSharedQueueAsync by reading _sharedQueue into a local variable before each null check - Add using declarations to all SemaphoreSlim locals in SqsPubSubClientTests and InMemoryPubSubClientTests for proper deterministic disposal
| try | ||
| { | ||
| current = _sharedQueue; | ||
| if (current is not null) |
| { | ||
| // Filter out SQS long-polling (ReceiveMessage) to reduce trace noise | ||
| o.FilterHttpRequestMessage = req => | ||
| req.Headers.TryGetValues("X-Amz-Target", out var values) != true |
| var queueNames = workers.Select(w => w.QueueName).ToList(); | ||
| IReadOnlyList<QueueStats> allStats = []; | ||
| try { allStats = await _queueClient.GetQueueStatsAsync(queueNames, ct).ConfigureAwait(false); } | ||
| catch { /* Transport may not support stats */ } |
| var statsList = await _queueClient.GetQueueStatsAsync([query.QueueName], ct).ConfigureAwait(false); | ||
| stats = statsList.FirstOrDefault(); | ||
| } | ||
| catch { /* Transport may not support stats */ } |
| }).ToList() | ||
| }; | ||
| } | ||
| catch { /* State store may not support counters */ } |
| if (_stateStore is not null) | ||
| { | ||
| try { counterStats = await _stateStore.GetCounterStatsAsync(worker.QueueName, TimeSpan.FromHours(24), ct).ConfigureAwait(false); } | ||
| catch { /* State store may not be available */ } |
| if (worker.TrackProgress) | ||
| { | ||
| try { processingCount = await _stateStore.GetJobCountByStatusAsync(worker.QueueName, QueueJobStatus.Processing, ct).ConfigureAwait(false); } | ||
| catch { /* State store may not be available */ } |
Comment on lines
+76
to
+80
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "Failed to initialize distributed infrastructure"); | ||
| ready.SetFailed(ex); | ||
| } |
Comment on lines
+158
to
+162
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "Failed to publish distributed notification {MessageType} to bus", | ||
| notification.GetType().Name); | ||
| } |
| // Cancel all active subscription consumer tasks | ||
| foreach (var cts in _activeCts) | ||
| { | ||
| try { cts.Cancel(); cts.Dispose(); } catch { } |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds distributed messaging infrastructure to Foundatio.Mediator — durable queues, pub/sub, and background workers — all integrated with the existing mediator pipeline.
New Packages
Foundatio.Mediator.DistributedCore distributed abstractions and in-memory implementations:
IQueueClient— Durable message queue abstraction with acknowledge/reject/dead-letter semanticsIPubSubClient— Publish/subscribe abstraction for fan-out notificationsIQueueJobStateStore— Job state tracking for long-running queue work itemsQueueWorker— Background hosted service that dequeues messages and dispatches through the mediator pipelineQueueMiddleware— Middleware that intercepts mediator calls and routes[Queue]-attributed messages to queuesDistributedNotificationWorker— Bridges pub/sub messages into the local mediator for cross-process notificationsQueueContext— Rich context for queue handlers (acknowledge, reject, defer, progress reporting)[Queue]attribute — Declarative queue routing with configurable concurrency, retry policies, and visibility timeoutsQueueRetryDelay— Static helper for computing retry delays with exponential backoff, jitter, and configurable capInMemoryQueueClient/InMemoryPubSubClient/InMemoryQueueJobStateStore— In-memory implementations for development and testingFoundatio.Mediator.Distributed.AwsAWS implementations:
SqsQueueClient— Amazon SQS queue client with FIFO support, dead-letter queues, and automatic infrastructure provisioningSqsPubSubClient— SNS fan-out publishing with per-node SQS subscription queues for true pub/sub across nodesFoundatio.Mediator.Distributed.RedisRedis implementations:
RedisQueueJobStateStore— Redis-backed job state tracking with atomicMULTI/EXECtransactions, TTL-based cleanup, and pub/sub change notificationsKey Features
[Queue]to message types to automatically route through queues instead of in-process dispatchSample Updates
Tests
Foundatio.Mediator.Distributed.Tests— In-memory queue client, pub/sub, job state store, queue worker integration, retry delay computationFoundatio.Mediator.Distributed.Aws.Tests— SQS queue client and SQS pub/sub tests against LocalStackFoundatio.Mediator.Distributed.Redis.Tests— Redis job state store testsFiles Changed
156 files changed, ~12,900 insertions, ~1,300 deletions