| | | 1 | | namespace AsyncResponse.Transports.MongoDB; |
| | | 2 | | |
| | | 3 | | internal static class MongoDbTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(MongoDbAsyncResponseTransportOptions options) |
| | | 6 | | { |
| | 3 | 7 | | ValidateCollectionName(options.MessageCollection, nameof(options.MessageCollection)); |
| | 3 | 8 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 3 | 9 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 3 | 10 | | Required(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | 3 | 11 | | Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 3 | 12 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 13 | | |
| | | 14 | | // All three logical queues share one collection, distinguished only by the queue field. Equal |
| | | 15 | | // names would make subscribers consume each other's documents (or re-consume dead letters). |
| | 3 | 16 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue) |
| | 3 | 17 | | || StringComparer.Ordinal.Equals(options.WorkerQueue, options.DeadLetterQueue) |
| | 3 | 18 | | || StringComparer.Ordinal.Equals(options.ResponseQueue, options.DeadLetterQueue)) |
| | | 19 | | { |
| | 3 | 20 | | throw new InvalidOperationException( |
| | 3 | 21 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.WorkerQueue)}, " + |
| | 3 | 22 | | $"{nameof(options.ResponseQueue)}, and {nameof(options.DeadLetterQueue)} must be distinct; they share on |
| | | 23 | | } |
| | | 24 | | |
| | 3 | 25 | | if (options.DeadLetterRetention is { } deadLetterRetention && deadLetterRetention <= TimeSpan.Zero) |
| | 3 | 26 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.DeadLet |
| | | 27 | | |
| | 3 | 28 | | Positive(options.LockTimeout, nameof(options.LockTimeout)); |
| | 3 | 29 | | Positive(options.PublishRetryBaseDelay, nameof(options.PublishRetryBaseDelay)); |
| | 3 | 30 | | Positive(options.PublishRetryMaxDelay, nameof(options.PublishRetryMaxDelay)); |
| | 3 | 31 | | Positive(options.SubscriberRetryBaseDelay, nameof(options.SubscriberRetryBaseDelay)); |
| | 3 | 32 | | Positive(options.SubscriberRetryMaxDelay, nameof(options.SubscriberRetryMaxDelay)); |
| | 3 | 33 | | Positive(options.ShutdownTimeout, nameof(options.ShutdownTimeout)); |
| | | 34 | | |
| | 3 | 35 | | if (options.PublishMaxAttempts <= 0) |
| | 3 | 36 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Publish |
| | 3 | 37 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 3 | 38 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Publish |
| | 3 | 39 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 3 | 40 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Subscri |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 44 | | public static void ValidateSubscriber( |
| | | 45 | | MongoDbAsyncResponseTransportOptions transportOptions, |
| | | 46 | | MongoDbSubscriberOptions subscriber, |
| | | 47 | | string role) |
| | | 48 | | { |
| | 3 | 49 | | ValidateSubscriber(subscriber, role); |
| | | 50 | | |
| | 3 | 51 | | if (subscriber.AckMode is not MongoDbAckMode.AckAfterEnqueue) |
| | 3 | 52 | | return; |
| | | 53 | | |
| | | 54 | | // MongoDB spends the background drain plus the change-stream listen-task join |
| | | 55 | | // (ShutdownTimeout) at shutdown; both must fit inside the host budget. |
| | 3 | 56 | | ShutdownBudgetValidator.Validate( |
| | 3 | 57 | | "MongoDB", |
| | 3 | 58 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 3 | 59 | | transportOptions.HostShutdownTimeout, |
| | 3 | 60 | | ($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.Back |
| | 3 | 61 | | ($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(transportOptions.ShutdownTimeout)}", transportOpti |
| | 3 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static void ValidateSubscriber(MongoDbSubscriberOptions subscriber, string role) |
| | | 65 | | { |
| | 3 | 66 | | if (subscriber.BatchSize <= 0) |
| | 3 | 67 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.BatchSize)} ({ro |
| | 3 | 68 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 3 | 69 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAttem |
| | | 70 | | |
| | 3 | 71 | | Positive(subscriber.RedeliveryDelay, $"{nameof(subscriber.RedeliveryDelay)} ({role})"); |
| | 3 | 72 | | Positive(subscriber.EmptyPollDelay, $"{nameof(subscriber.EmptyPollDelay)} ({role})"); |
| | | 73 | | |
| | 3 | 74 | | switch (subscriber.AckMode) |
| | | 75 | | { |
| | | 76 | | case MongoDbAckMode.AckAfterHandlerCompletes: |
| | 3 | 77 | | return; |
| | | 78 | | case MongoDbAckMode.AckAfterEnqueue: |
| | 3 | 79 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 3 | 80 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.Backgrou |
| | 3 | 81 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 3 | 82 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.Backgrou |
| | 3 | 83 | | Positive(subscriber.BackgroundDrainTimeout, $"{nameof(subscriber.BackgroundDrainTimeout)} ({role})"); |
| | 3 | 84 | | return; |
| | | 85 | | default: |
| | 3 | 86 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.AckMode)} ({ |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public static string Required(string? value, string name) |
| | 3 | 91 | | => !string.IsNullOrWhiteSpace(value) |
| | 3 | 92 | | ? value |
| | 3 | 93 | | : throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} must be config |
| | | 94 | | |
| | | 95 | | public static void ValidateCollectionName(string? value, string name) |
| | | 96 | | { |
| | 3 | 97 | | if (string.IsNullOrWhiteSpace(value)) |
| | 3 | 98 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} must be configur |
| | 3 | 99 | | if (value.Contains('$') || value.Contains('\0') || value.StartsWith("system.", StringComparison.Ordinal)) |
| | 3 | 100 | | throw new InvalidOperationException( |
| | 3 | 101 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} '{value}' must be a valid MongoDB collection nam |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | private static void Positive(TimeSpan value, string name) |
| | | 105 | | { |
| | 3 | 106 | | if (value <= TimeSpan.Zero) |
| | 3 | 107 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} must be positive |
| | 3 | 108 | | } |
| | | 109 | | } |