| | | 1 | | namespace AsyncResponse.Transports.SqlServer; |
| | | 2 | | |
| | | 3 | | internal static class SqlServerTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(SqlServerAsyncResponseTransportOptions options) |
| | | 6 | | { |
| | 894 | 7 | | Required(options.ConnectionString, nameof(options.ConnectionString)); |
| | 892 | 8 | | ValidateIdentifier(options.SchemaName, nameof(options.SchemaName)); |
| | 888 | 9 | | ValidateIdentifier(options.MessageTable, nameof(options.MessageTable)); |
| | 884 | 10 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 884 | 11 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 884 | 12 | | Required(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | 884 | 13 | | Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 882 | 14 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 15 | | |
| | | 16 | | // The three logical queues share one table, distinguished only by the queue column, so |
| | | 17 | | // names that the DATABASE considers equal are as dangerous as identical ones. SQL Server |
| | | 18 | | // pads the shorter operand of an `=` comparison with spaces before comparing — even under |
| | | 19 | | // a binary collation — so 'worker' and 'worker ' match each other's rows and both |
| | | 20 | | // subscribers would consume both queues. Ordinal distinctness alone does not see that. |
| | 882 | 21 | | ValidateQueueName(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 876 | 22 | | ValidateQueueName(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 872 | 23 | | ValidateQueueName(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | | 24 | | |
| | 868 | 25 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue) |
| | 868 | 26 | | || StringComparer.Ordinal.Equals(options.WorkerQueue, options.DeadLetterQueue) |
| | 868 | 27 | | || StringComparer.Ordinal.Equals(options.ResponseQueue, options.DeadLetterQueue)) |
| | | 28 | | { |
| | 2 | 29 | | throw new InvalidOperationException( |
| | 2 | 30 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.WorkerQueue)}, " + |
| | 2 | 31 | | $"{nameof(options.ResponseQueue)}, and {nameof(options.DeadLetterQueue)} must be distinct; they share on |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // Timer-armed knobs get the .NET timer ceiling (LockTimeout also drives the in-process |
| | | 35 | | // lease-renewal Task.Delay at a third of its value); DeadLetterRetention is a database-side |
| | | 36 | | // "now - retention" prune cutoff and gets the persistence bound instead. |
| | 866 | 37 | | if (options.DeadLetterRetention is { } deadLetterRetention) |
| | 24 | 38 | | AsyncResponseChannelOptions.EnsurePersistedTtl(deadLetterRetention, nameof(SqlServerAsyncResponseTransportOp |
| | | 39 | | |
| | 864 | 40 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.LockTimeout, nameof(SqlServerAsyncResponseTransportOptions |
| | 860 | 41 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryBaseDelay, nameof(SqlServerAsyncResponseTransp |
| | 860 | 42 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryMaxDelay, nameof(SqlServerAsyncResponseTranspo |
| | 860 | 43 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryBaseDelay, nameof(SqlServerAsyncResponseTra |
| | 860 | 44 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryMaxDelay, nameof(SqlServerAsyncResponseTran |
| | | 45 | | |
| | 860 | 46 | | if (options.PublishMaxAttempts <= 0) |
| | 2 | 47 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Publi |
| | 858 | 48 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 2 | 49 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Publi |
| | 856 | 50 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 2 | 51 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Subsc |
| | 854 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 55 | | public static void ValidateSubscriber( |
| | | 56 | | SqlServerAsyncResponseTransportOptions transportOptions, |
| | | 57 | | SqlServerSubscriberOptions subscriber, |
| | | 58 | | string role) |
| | | 59 | | { |
| | 868 | 60 | | ValidateSubscriber(subscriber, role); |
| | | 61 | | |
| | 864 | 62 | | if (subscriber.AckMode is not SqlServerAckMode.AckAfterEnqueue) |
| | 820 | 63 | | return; |
| | | 64 | | |
| | | 65 | | // SQL Server subscribers spend only the background drain at shutdown; the polling loop |
| | | 66 | | // stops with the host token and holds no connection that needs a bounded close. |
| | 44 | 67 | | ShutdownBudgetValidator.Validate( |
| | 44 | 68 | | "SQL Server", |
| | 44 | 69 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 44 | 70 | | transportOptions.HostShutdownTimeout, |
| | 44 | 71 | | ($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.Ba |
| | 40 | 72 | | } |
| | | 73 | | |
| | | 74 | | public static void ValidateSubscriber(SqlServerSubscriberOptions subscriber, string role) |
| | | 75 | | { |
| | 886 | 76 | | if (subscriber.BatchSize <= 0) |
| | 2 | 77 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.BatchSize)} ({ |
| | 884 | 78 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 2 | 79 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAtt |
| | | 80 | | |
| | | 81 | | // RedeliveryDelay is a database-side "now + delay" visibility stamp (persistence bound); |
| | | 82 | | // EmptyPollDelay arms the idle-poll Task.Delay (timer ceiling). |
| | 882 | 83 | | AsyncResponseChannelOptions.EnsurePersistedTtl(subscriber.RedeliveryDelay, nameof(SqlServerSubscriberOptions), $ |
| | 880 | 84 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.EmptyPollDelay, nameof(SqlServerSubscriberOptions), $"{ |
| | | 85 | | |
| | 878 | 86 | | switch (subscriber.AckMode) |
| | | 87 | | { |
| | | 88 | | case SqlServerAckMode.AckAfterHandlerCompletes: |
| | 822 | 89 | | return; |
| | | 90 | | case SqlServerAckMode.AckAfterEnqueue: |
| | 54 | 91 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 6 | 92 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.Backgr |
| | 48 | 93 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 2 | 94 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.Backgr |
| | 46 | 95 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.BackgroundDrainTimeout, nameof(SqlServerSubscri |
| | 46 | 96 | | return; |
| | | 97 | | default: |
| | 2 | 98 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.AckMode)} |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// A queue name is a database KEY, not free text: it is stored in <c>nvarchar(200)</c> and |
| | | 104 | | /// matched with <c>=</c>, whose space padding makes trailing blanks invisible to the database |
| | | 105 | | /// while the library still treats the names as distinct. Leading blanks are rejected on the |
| | | 106 | | /// same principle — they survive comparison but make two names indistinguishable in logs. |
| | | 107 | | /// </summary> |
| | | 108 | | public static void ValidateQueueName(string value, string name) |
| | | 109 | | { |
| | 2658 | 110 | | if (value.Length > MaxQueueNameLength) |
| | | 111 | | { |
| | 6 | 112 | | throw new InvalidOperationException( |
| | 6 | 113 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} is {value.Length} characters; the queue column |
| | 6 | 114 | | $"nvarchar({MaxQueueNameLength}), so a longer name is truncated or rejected at the first publish."); |
| | | 115 | | } |
| | | 116 | | |
| | 2652 | 117 | | if (value[0] == ' ' || value[^1] == ' ') |
| | | 118 | | { |
| | 18 | 119 | | throw new InvalidOperationException( |
| | 18 | 120 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} '{value}' begins or ends with a space. SQL Ser |
| | 18 | 121 | | "the shorter operand of an equality comparison, so a trailing space is invisible to the queue lookup — t |
| | 18 | 122 | | "differing only in trailing spaces would consume each other's messages. Remove the surrounding spaces.") |
| | | 123 | | } |
| | 2634 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary>The queue column's width: <c>queue nvarchar(200)</c> in the transport's DDL.</summary> |
| | | 127 | | public const int MaxQueueNameLength = 200; |
| | | 128 | | |
| | | 129 | | public static string Required(string? value, string name) |
| | 5353 | 130 | | => !string.IsNullOrWhiteSpace(value) |
| | 5353 | 131 | | ? value |
| | 5353 | 132 | | : throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} must be conf |
| | | 133 | | |
| | | 134 | | public static void ValidateIdentifier(string? value, string name) |
| | | 135 | | { |
| | 1780 | 136 | | if (string.IsNullOrWhiteSpace(value)) |
| | 2 | 137 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} must be config |
| | 1778 | 138 | | if (!IsIdentifier(value)) |
| | 4 | 139 | | throw new InvalidOperationException( |
| | 4 | 140 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} '{value}' must be a simple SQL Server identifi |
| | | 141 | | // sysname caps identifiers at 128; an over-limit name fails at DDL time with a raw |
| | | 142 | | // "identifier too long" error instead of an actionable configuration error. |
| | 1774 | 143 | | if (value.Length > 128) |
| | 2 | 144 | | throw new InvalidOperationException( |
| | 2 | 145 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} '{value}' is {value.Length} characters; SQL Se |
| | 1772 | 146 | | } |
| | | 147 | | |
| | | 148 | | private static bool IsIdentifier(string value) |
| | | 149 | | { |
| | 1778 | 150 | | if (value.Length == 0 || !(char.IsAsciiLetter(value[0]) || value[0] == '_')) |
| | 2 | 151 | | return false; |
| | 86576 | 152 | | foreach (var c in value) |
| | | 153 | | { |
| | 41513 | 154 | | if (!(char.IsAsciiLetterOrDigit(c) || c == '_')) |
| | 2 | 155 | | return false; |
| | | 156 | | } |
| | 1774 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | } |