| | | 1 | | namespace AsyncResponse.Transports.AzureServiceBus; |
| | | 2 | | |
| | | 3 | | internal static class AzureServiceBusOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(AzureServiceBusAsyncResponseOptions options) |
| | | 6 | | { |
| | 652 | 7 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 652 | 8 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 652 | 9 | | Required(options.CorrelationIdProperty, nameof(options.CorrelationIdProperty)); |
| | 652 | 10 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 11 | | |
| | 652 | 12 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue)) |
| | | 13 | | { |
| | 2 | 14 | | throw new InvalidOperationException( |
| | 2 | 15 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.WorkerQueue)} and " + |
| | 2 | 16 | | $"{nameof(options.ResponseQueue)} must be distinct so worker and response subscribers do not consume eac |
| | | 17 | | } |
| | | 18 | | |
| | 650 | 19 | | if (options.MaxMessagesPerReceive <= 0) |
| | 2 | 20 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.MaxMessa |
| | 648 | 21 | | if (options.PublishMaxAttempts <= 0) |
| | 2 | 22 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.PublishM |
| | | 23 | | |
| | | 24 | | // All of these arm timers: ReceiveWaitTime inside the SDK's receive call, the retry |
| | | 25 | | // delays via Task.Delay, and ShutdownTimeout as a CancellationTokenSource budget. |
| | 646 | 26 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.ReceiveWaitTime, nameof(AzureServiceBusAsyncResponseOption |
| | 642 | 27 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryBaseDelay, nameof(AzureServiceBusAsyncResponse |
| | 642 | 28 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryMaxDelay, nameof(AzureServiceBusAsyncResponseO |
| | 642 | 29 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryBaseDelay, nameof(AzureServiceBusAsyncRespo |
| | 642 | 30 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryMaxDelay, nameof(AzureServiceBusAsyncRespon |
| | 642 | 31 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.ShutdownTimeout, nameof(AzureServiceBusAsyncResponseOption |
| | | 32 | | |
| | 642 | 33 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 2 | 34 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.PublishR |
| | 640 | 35 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 2 | 36 | | throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(options.Subscrib |
| | 638 | 37 | | } |
| | | 38 | | |
| | | 39 | | public static void ValidateSubscriber( |
| | | 40 | | AzureServiceBusAsyncResponseOptions transportOptions, |
| | | 41 | | AzureServiceBusSubscriberOptions subscriberOptions, |
| | | 42 | | AzureServiceBusSubscriberRole role) |
| | | 43 | | { |
| | 878 | 44 | | var optionPath = role is AzureServiceBusSubscriberRole.Worker |
| | 878 | 45 | | ? $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.WorkerSubscrib |
| | 878 | 46 | | : $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.ResponseSubscr |
| | | 47 | | |
| | 878 | 48 | | if (subscriberOptions.MaxDeliveryAttempts < 0) |
| | 2 | 49 | | throw new InvalidOperationException($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.MaxDeliveryAttem |
| | 876 | 50 | | if (subscriberOptions.PrefetchCount < 0) |
| | 2 | 51 | | throw new InvalidOperationException($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.PrefetchCount)} |
| | 874 | 52 | | if (subscriberOptions.LockRenewalInterval is { } lockRenewalInterval) |
| | 866 | 53 | | AsyncResponseChannelOptions.EnsureTimerBacked(lockRenewalInterval, optionPath, nameof(AzureServiceBusSubscri |
| | | 54 | | |
| | 870 | 55 | | switch (subscriberOptions.AckMode) |
| | | 56 | | { |
| | | 57 | | case AzureServiceBusAckMode.AckAfterHandlerCompletes: |
| | | 58 | | // ShutdownTimeout is spent at shutdown even without a background drain (SQS |
| | | 59 | | // parity, and what this option's own XML doc and transport-semantics.md promise |
| | | 60 | | // is validated): the lock-renewal join on the final batch, then the receiver |
| | | 61 | | // close, run SEQUENTIALLY on the stop path, so with renewal on the budget must |
| | | 62 | | // fit two of them. Returning here without summing anything let a raised |
| | | 63 | | // ShutdownTimeout overrun the host's stop budget, force-terminating mid-close and |
| | | 64 | | // redelivering handled-but-uncompleted messages whose locks then lapsed. |
| | 830 | 65 | | if (subscriberOptions.LockRenewalInterval is not null) |
| | | 66 | | { |
| | 822 | 67 | | ShutdownBudgetValidator.Validate( |
| | 822 | 68 | | "Azure Service Bus", |
| | 822 | 69 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Host |
| | 822 | 70 | | transportOptions.HostShutdownTimeout, |
| | 822 | 71 | | ($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Shu |
| | 822 | 72 | | ($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Shu |
| | | 73 | | } |
| | | 74 | | else |
| | | 75 | | { |
| | 8 | 76 | | ShutdownBudgetValidator.Validate( |
| | 8 | 77 | | "Azure Service Bus", |
| | 8 | 78 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Host |
| | 8 | 79 | | transportOptions.HostShutdownTimeout, |
| | 8 | 80 | | ($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Shu |
| | | 81 | | } |
| | | 82 | | |
| | 6 | 83 | | return; |
| | | 84 | | |
| | | 85 | | case AzureServiceBusAckMode.AckAfterEnqueue: |
| | 38 | 86 | | if (subscriberOptions.BackgroundWorkerCount <= 0) |
| | | 87 | | { |
| | 4 | 88 | | throw new InvalidOperationException( |
| | 4 | 89 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundWorkerCount)} must be explicit |
| | 4 | 90 | | $"when {nameof(AzureServiceBusSubscriberOptions.AckMode)} is {nameof(AzureServiceBusAckMode.AckA |
| | | 91 | | } |
| | | 92 | | |
| | 34 | 93 | | if (subscriberOptions.BackgroundQueueCapacity <= 0) |
| | | 94 | | { |
| | 2 | 95 | | throw new InvalidOperationException( |
| | 2 | 96 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundQueueCapacity)} must be explic |
| | 2 | 97 | | $"when {nameof(AzureServiceBusSubscriberOptions.AckMode)} is {nameof(AzureServiceBusAckMode.AckA |
| | | 98 | | } |
| | | 99 | | |
| | 32 | 100 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriberOptions.BackgroundDrainTimeout, optionPath, name |
| | | 101 | | |
| | | 102 | | // Service Bus spends the background drain plus receiver close / renewal-task join |
| | | 103 | | // (ShutdownTimeout) at shutdown; both must fit inside the host budget. |
| | 30 | 104 | | ShutdownBudgetValidator.Validate( |
| | 30 | 105 | | "Azure Service Bus", |
| | 30 | 106 | | $"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.HostShut |
| | 30 | 107 | | transportOptions.HostShutdownTimeout, |
| | 30 | 108 | | ($"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.BackgroundDrainTimeout)}", subscriberOption |
| | 30 | 109 | | ($"{nameof(AzureServiceBusAsyncResponseOptions)}.{nameof(AzureServiceBusAsyncResponseOptions.Shutdow |
| | | 110 | | |
| | 26 | 111 | | return; |
| | | 112 | | |
| | | 113 | | default: |
| | 2 | 114 | | throw new InvalidOperationException( |
| | 2 | 115 | | $"{optionPath}.{nameof(AzureServiceBusSubscriberOptions.AckMode)} has unsupported value '{subscriber |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public static string Required(string? value, string name) |
| | 3618 | 120 | | => !string.IsNullOrWhiteSpace(value) |
| | 3618 | 121 | | ? value |
| | 3618 | 122 | | : throw new InvalidOperationException($"{nameof(AzureServiceBusAsyncResponseOptions)}.{name} must be configu |
| | | 123 | | } |