| | | 1 | | namespace AsyncResponse.Transports.AzureServiceBus; |
| | | 2 | | |
| | | 3 | | internal static class AzureServiceBusOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(AzureServiceBusAsyncResponseOptions options) |
| | | 6 | | { |
| | 3 | 7 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 3 | 8 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 3 | 9 | | Required(options.CorrelationIdProperty, nameof(options.CorrelationIdProperty)); |
| | 3 | 10 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 11 | | |
| | 3 | 12 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue)) |
| | | 13 | | { |
| | 3 | 14 | | throw new InvalidOperationException( |
| | 3 | 15 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.WorkerQueue)} and " + |
| | 3 | 16 | | $"{nameof(options.ResponseQueue)} must be distinct so worker and response subscribers do not consume eac |
| | | 17 | | } |
| | | 18 | | |
| | 3 | 19 | | if (options.MaxMessagesPerReceive <= 0) |
| | 3 | 20 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.MaxMessa |
| | 3 | 21 | | if (options.PublishMaxAttempts <= 0) |
| | 3 | 22 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.PublishM |
| | | 23 | | |
| | 3 | 24 | | Positive(options.ReceiveWaitTime, nameof(options.ReceiveWaitTime)); |
| | 3 | 25 | | Positive(options.PublishRetryBaseDelay, nameof(options.PublishRetryBaseDelay)); |
| | 3 | 26 | | Positive(options.PublishRetryMaxDelay, nameof(options.PublishRetryMaxDelay)); |
| | 3 | 27 | | Positive(options.SubscriberRetryBaseDelay, nameof(options.SubscriberRetryBaseDelay)); |
| | 3 | 28 | | Positive(options.SubscriberRetryMaxDelay, nameof(options.SubscriberRetryMaxDelay)); |
| | 3 | 29 | | Positive(options.ShutdownTimeout, nameof(options.ShutdownTimeout)); |
| | | 30 | | |
| | 3 | 31 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 3 | 32 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.PublishR |
| | 3 | 33 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 3 | 34 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.Subscrib |
| | 3 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static void ValidateSubscriber( |
| | | 38 | | AzureServiceBusAsyncResponseOptions transportOptions, |
| | | 39 | | AzureServiceBusSubscriberOptions subscriberOptions, |
| | | 40 | | AzureServiceBusSubscriberRole role) |
| | | 41 | | { |
| | 3 | 42 | | var optionPath = role is AzureServiceBusSubscriberRole.Worker |
| | 3 | 43 | | ? $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.WorkerSubscrib |
| | 3 | 44 | | : $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.ResponseSubscr |
| | | 45 | | |
| | 3 | 46 | | if (subscriberOptions.MaxDeliveryAttempts < 0) |
| | 3 | 47 | | throw new InvalidOperationException($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.MaxDeliveryAttem |
| | 3 | 48 | | if (subscriberOptions.PrefetchCount < 0) |
| | 3 | 49 | | throw new InvalidOperationException($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.PrefetchCount)} |
| | 3 | 50 | | if (subscriberOptions.LockRenewalInterval is { } lockRenewalInterval && lockRenewalInterval <= TimeSpan.Zero) |
| | 3 | 51 | | throw new InvalidOperationException($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.LockRenewalInter |
| | | 52 | | |
| | 3 | 53 | | switch (subscriberOptions.AckMode) |
| | | 54 | | { |
| | | 55 | | case AzureServiceBusAckMode.AckAfterHandlerCompletes: |
| | 3 | 56 | | return; |
| | | 57 | | |
| | | 58 | | case AzureServiceBusAckMode.AckAfterEnqueue: |
| | 3 | 59 | | if (subscriberOptions.BackgroundWorkerCount <= 0) |
| | | 60 | | { |
| | 3 | 61 | | throw new InvalidOperationException( |
| | 3 | 62 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundWorkerCount)} must be explicit |
| | 3 | 63 | | $"when {nameof(AzureServiceBusSubscriberOptions.AckMode)} is {nameof(AzureServiceBusAckMode.AckA |
| | | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | if (subscriberOptions.BackgroundQueueCapacity <= 0) |
| | | 67 | | { |
| | 3 | 68 | | throw new InvalidOperationException( |
| | 3 | 69 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundQueueCapacity)} must be explic |
| | 3 | 70 | | $"when {nameof(AzureServiceBusSubscriberOptions.AckMode)} is {nameof(AzureServiceBusAckMode.AckA |
| | | 71 | | } |
| | | 72 | | |
| | 3 | 73 | | if (subscriberOptions.BackgroundDrainTimeout <= TimeSpan.Zero) |
| | | 74 | | { |
| | 3 | 75 | | throw new InvalidOperationException( |
| | 3 | 76 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundDrainTimeout)} must be positiv |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // Service Bus spends the background drain plus receiver close / renewal-task join |
| | | 80 | | // (ShutdownTimeout) at shutdown; both must fit inside the host budget. |
| | 3 | 81 | | ShutdownBudgetValidator.Validate( |
| | 3 | 82 | | "Azure Service Bus", |
| | 3 | 83 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.HostShut |
| | 3 | 84 | | transportOptions.HostShutdownTimeout, |
| | 3 | 85 | | ($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundDrainTimeout)}", subscriberOption |
| | 3 | 86 | | ($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Shutdow |
| | | 87 | | |
| | 3 | 88 | | return; |
| | | 89 | | |
| | | 90 | | default: |
| | 3 | 91 | | throw new InvalidOperationException( |
| | 3 | 92 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.AckMode)} has unsupported value '{subscriber |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public static string Required(string? value, string name) |
| | 3 | 97 | | => !string.IsNullOrWhiteSpace(value) |
| | 3 | 98 | | ? value |
| | 3 | 99 | | : throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{name} must be configu |
| | | 100 | | |
| | | 101 | | private static void Positive(TimeSpan value, string name) |
| | | 102 | | { |
| | 3 | 103 | | if (value <= TimeSpan.Zero) |
| | 3 | 104 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{name} must be positive. |
| | 3 | 105 | | } |
| | | 106 | | } |