| | | 1 | | namespace AsyncResponse.Transports.NATS; |
| | | 2 | | |
| | | 3 | | internal static class NatsTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | /// <summary>Validates the supplied options.</summary> |
| | | 6 | | public static string Required(string? value, string name) |
| | 3 | 7 | | => !string.IsNullOrWhiteSpace(value) |
| | 3 | 8 | | ? value |
| | 3 | 9 | | : throw new InvalidOperationException($"{nameof(NatsAsyncResponseTransportOptions)}.{name} must be configure |
| | | 10 | | |
| | | 11 | | /// <summary>Validates the supplied options.</summary> |
| | | 12 | | public static void Positive(TimeSpan value, string name) |
| | | 13 | | { |
| | 3 | 14 | | if (value <= TimeSpan.Zero) |
| | 3 | 15 | | throw new InvalidOperationException($"{nameof(NatsAsyncResponseTransportOptions)}.{name} must be positive.") |
| | 3 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary>Validates the supplied options.</summary> |
| | | 19 | | public static void PositiveOrNull(long? value, string name) |
| | | 20 | | { |
| | 3 | 21 | | if (value is <= 0) |
| | 3 | 22 | | throw new InvalidOperationException($"{nameof(NatsAsyncResponseTransportOptions)}.{name} must be positive wh |
| | 3 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary>Validates the supplied options.</summary> |
| | | 26 | | public static void ValidateCommon(NatsAsyncResponseTransportOptions options) |
| | | 27 | | { |
| | 3 | 28 | | _ = Required(options.SubjectPrefix, nameof(options.SubjectPrefix)); |
| | 3 | 29 | | _ = Required(options.WorkerConsumer, nameof(options.WorkerConsumer)); |
| | 3 | 30 | | _ = Required(options.ResponseConsumer, nameof(options.ResponseConsumer)); |
| | 3 | 31 | | _ = Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 3 | 32 | | _ = Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 33 | | |
| | | 34 | | // A subject prefix becomes leading tokens of every transport subject; it must not contain |
| | | 35 | | // whitespace or the NATS subject wildcards. |
| | 3 | 36 | | if (options.SubjectPrefix.IndexOfAny([' ', '\t', '*', '>', '\r', '\n']) >= 0) |
| | 3 | 37 | | throw new InvalidOperationException( |
| | 3 | 38 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.SubjectPrefix)} '{options.SubjectPrefix}' |
| | | 39 | | |
| | 3 | 40 | | Positive(options.AckWait, nameof(options.AckWait)); |
| | 3 | 41 | | Positive(options.PublishRetryBaseDelay, nameof(options.PublishRetryBaseDelay)); |
| | 3 | 42 | | Positive(options.PublishRetryMaxDelay, nameof(options.PublishRetryMaxDelay)); |
| | 3 | 43 | | Positive(options.SubscriberRetryBaseDelay, nameof(options.SubscriberRetryBaseDelay)); |
| | 3 | 44 | | Positive(options.SubscriberRetryMaxDelay, nameof(options.SubscriberRetryMaxDelay)); |
| | 3 | 45 | | PositiveOrNull(options.StreamMaxMessages, nameof(options.StreamMaxMessages)); |
| | 3 | 46 | | PositiveOrNull(options.DeadLetterStreamMaxMessages, nameof(options.DeadLetterStreamMaxMessages)); |
| | | 47 | | |
| | 3 | 48 | | if (options.PublishMaxAttempts <= 0) |
| | 3 | 49 | | throw new InvalidOperationException($"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.PublishMax |
| | | 50 | | |
| | 3 | 51 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 3 | 52 | | throw new InvalidOperationException( |
| | 3 | 53 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.PublishRetryBaseDelay)} cannot exceed " + |
| | 3 | 54 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.PublishRetryMaxDelay)}."); |
| | | 55 | | |
| | 3 | 56 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 3 | 57 | | throw new InvalidOperationException( |
| | 3 | 58 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.SubscriberRetryBaseDelay)} cannot exceed " |
| | 3 | 59 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(options.SubscriberRetryMaxDelay)}."); |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 63 | | public static void ValidateSubscriber( |
| | | 64 | | NatsAsyncResponseTransportOptions transportOptions, |
| | | 65 | | NatsSubscriberOptions subscriber, |
| | | 66 | | string role) |
| | | 67 | | { |
| | 3 | 68 | | ValidateSubscriber(subscriber, role); |
| | | 69 | | |
| | 3 | 70 | | if (subscriber.AckMode is not NatsAckMode.AckAfterEnqueue) |
| | 3 | 71 | | return; |
| | | 72 | | |
| | | 73 | | // NATS subscribers spend only the background drain at shutdown; the consume loop stops |
| | | 74 | | // with the host token and the connection teardown is not separately bounded. |
| | 3 | 75 | | ShutdownBudgetValidator.Validate( |
| | 3 | 76 | | "NATS", |
| | 3 | 77 | | $"{nameof(NatsAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 3 | 78 | | transportOptions.HostShutdownTimeout, |
| | 3 | 79 | | ($"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.Backgro |
| | 3 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary>Validates the supplied options.</summary> |
| | | 83 | | public static void ValidateSubscriber(NatsSubscriberOptions subscriber, string role) |
| | | 84 | | { |
| | 3 | 85 | | if (subscriber.BatchSize <= 0) |
| | 3 | 86 | | throw new InvalidOperationException($"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.BatchSize)} ({role} |
| | | 87 | | |
| | 3 | 88 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 3 | 89 | | throw new InvalidOperationException($"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAttempts |
| | | 90 | | |
| | 3 | 91 | | Positive(subscriber.RedeliveryDelay, $"{nameof(subscriber.RedeliveryDelay)} ({role})"); |
| | | 92 | | |
| | 3 | 93 | | switch (subscriber.AckMode) |
| | | 94 | | { |
| | | 95 | | case NatsAckMode.AckAfterHandlerCompletes: |
| | 3 | 96 | | return; |
| | | 97 | | |
| | | 98 | | case NatsAckMode.AckAfterEnqueue: |
| | 3 | 99 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 3 | 100 | | throw new InvalidOperationException($"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.BackgroundW |
| | 3 | 101 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 3 | 102 | | throw new InvalidOperationException($"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.BackgroundQ |
| | 3 | 103 | | Positive(subscriber.BackgroundDrainTimeout, $"{nameof(subscriber.BackgroundDrainTimeout)} ({role})"); |
| | 3 | 104 | | return; |
| | | 105 | | |
| | | 106 | | default: |
| | 3 | 107 | | throw new InvalidOperationException( |
| | 3 | 108 | | $"{nameof(NatsSubscriberOptions)}.{nameof(subscriber.AckMode)} ({role}) has unsupported value '{subs |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |