| | | 1 | | namespace AsyncResponse.Transports.MongoDB; |
| | | 2 | | |
| | | 3 | | internal static class MongoDbTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(MongoDbAsyncResponseTransportOptions options) |
| | | 6 | | { |
| | 921 | 7 | | ValidateCollectionName(options.MessageCollection, nameof(options.MessageCollection)); |
| | 913 | 8 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 913 | 9 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 913 | 10 | | Required(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | 913 | 11 | | Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 911 | 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). |
| | 911 | 16 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue) |
| | 911 | 17 | | || StringComparer.Ordinal.Equals(options.WorkerQueue, options.DeadLetterQueue) |
| | 911 | 18 | | || StringComparer.Ordinal.Equals(options.ResponseQueue, options.DeadLetterQueue)) |
| | | 19 | | { |
| | 2 | 20 | | throw new InvalidOperationException( |
| | 2 | 21 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.WorkerQueue)}, " + |
| | 2 | 22 | | $"{nameof(options.ResponseQueue)}, and {nameof(options.DeadLetterQueue)} must be distinct; they share on |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | // Timer-armed knobs get the .NET timer ceiling (LockTimeout also drives the in-process |
| | | 26 | | // lease-renewal Task.Delay at a third of its value); DeadLetterRetention is a server-side |
| | | 27 | | // "now - retention" prune cutoff and gets the persistence bound instead. |
| | 909 | 28 | | if (options.DeadLetterRetention is { } deadLetterRetention) |
| | 5 | 29 | | AsyncResponseChannelOptions.EnsurePersistedTtl(deadLetterRetention, nameof(MongoDbAsyncResponseTransportOpti |
| | | 30 | | |
| | 907 | 31 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.LockTimeout, nameof(MongoDbAsyncResponseTransportOptions), |
| | 903 | 32 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryBaseDelay, nameof(MongoDbAsyncResponseTranspor |
| | 903 | 33 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryMaxDelay, nameof(MongoDbAsyncResponseTransport |
| | 903 | 34 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryBaseDelay, nameof(MongoDbAsyncResponseTrans |
| | 903 | 35 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryMaxDelay, nameof(MongoDbAsyncResponseTransp |
| | 903 | 36 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.ShutdownTimeout, nameof(MongoDbAsyncResponseTransportOptio |
| | | 37 | | |
| | 903 | 38 | | if (options.PublishMaxAttempts <= 0) |
| | 2 | 39 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Publish |
| | 901 | 40 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 2 | 41 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Publish |
| | 899 | 42 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 2 | 43 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.Subscri |
| | 897 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 47 | | public static void ValidateSubscriber( |
| | | 48 | | MongoDbAsyncResponseTransportOptions transportOptions, |
| | | 49 | | MongoDbSubscriberOptions subscriber, |
| | | 50 | | string role) |
| | | 51 | | { |
| | 906 | 52 | | ValidateSubscriber(subscriber, role); |
| | | 53 | | |
| | 902 | 54 | | if (subscriber.AckMode is not MongoDbAckMode.AckAfterEnqueue) |
| | 858 | 55 | | return; |
| | | 56 | | |
| | | 57 | | // MongoDB spends the background drain plus the change-stream listen-task join |
| | | 58 | | // (ShutdownTimeout) at shutdown; both must fit inside the host budget. |
| | 44 | 59 | | ShutdownBudgetValidator.Validate( |
| | 44 | 60 | | "MongoDB", |
| | 44 | 61 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 44 | 62 | | transportOptions.HostShutdownTimeout, |
| | 44 | 63 | | ($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.Back |
| | 44 | 64 | | ($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(transportOptions.ShutdownTimeout)}", transportOpti |
| | 40 | 65 | | } |
| | | 66 | | |
| | | 67 | | public static void ValidateSubscriber(MongoDbSubscriberOptions subscriber, string role) |
| | | 68 | | { |
| | 922 | 69 | | if (subscriber.BatchSize <= 0) |
| | 2 | 70 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.BatchSize)} ({ro |
| | 920 | 71 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 2 | 72 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAttem |
| | | 73 | | |
| | | 74 | | // RedeliveryDelay is a server-side "$$NOW + delay" visibility stamp (persistence bound); |
| | | 75 | | // EmptyPollDelay arms the idle-poll Task.Delay (timer ceiling). |
| | 918 | 76 | | AsyncResponseChannelOptions.EnsurePersistedTtl(subscriber.RedeliveryDelay, nameof(MongoDbSubscriberOptions), $"{ |
| | 916 | 77 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.EmptyPollDelay, nameof(MongoDbSubscriberOptions), $"{na |
| | | 78 | | |
| | 916 | 79 | | switch (subscriber.AckMode) |
| | | 80 | | { |
| | | 81 | | case MongoDbAckMode.AckAfterHandlerCompletes: |
| | 860 | 82 | | return; |
| | | 83 | | case MongoDbAckMode.AckAfterEnqueue: |
| | 54 | 84 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 6 | 85 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.Backgrou |
| | 48 | 86 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 2 | 87 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.Backgrou |
| | 46 | 88 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.BackgroundDrainTimeout, nameof(MongoDbSubscribe |
| | 46 | 89 | | return; |
| | | 90 | | default: |
| | 2 | 91 | | throw new InvalidOperationException($"{nameof(MongoDbSubscriberOptions)}.{nameof(subscriber.AckMode)} ({ |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public static string Required(string? value, string name) |
| | 4599 | 96 | | => !string.IsNullOrWhiteSpace(value) |
| | 4599 | 97 | | ? value |
| | 4599 | 98 | | : throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} must be config |
| | | 99 | | |
| | | 100 | | public static void ValidateCollectionName(string? value, string name) |
| | | 101 | | { |
| | 921 | 102 | | if (string.IsNullOrWhiteSpace(value)) |
| | 2 | 103 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} must be configur |
| | 919 | 104 | | if (value.Contains('$') || value.Contains('\0') |
| | 919 | 105 | | || value.StartsWith("system.", StringComparison.Ordinal) || value.Contains(".system.", StringComparison.Ordi |
| | 6 | 106 | | throw new InvalidOperationException( |
| | 6 | 107 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} '{value}' must be a valid MongoDB collection nam |
| | 913 | 108 | | if (string.Equals(value, AsyncResponse.Internal.MongoOwnershipLedger.CollectionName, StringComparison.Ordinal)) |
| | 0 | 109 | | throw new InvalidOperationException( |
| | 0 | 110 | | $"{nameof(MongoDbAsyncResponseTransportOptions)}.{name} '{value}' is reserved for the cross-component ow |
| | 913 | 111 | | } |
| | | 112 | | |
| | | 113 | | } |