| | | 1 | | namespace AsyncResponse.Transports.SqlServer; |
| | | 2 | | |
| | | 3 | | internal static class SqlServerTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(SqlServerAsyncResponseTransportOptions options) |
| | | 6 | | { |
| | 3 | 7 | | Required(options.ConnectionString, nameof(options.ConnectionString)); |
| | 3 | 8 | | ValidateIdentifier(options.SchemaName, nameof(options.SchemaName)); |
| | 3 | 9 | | ValidateIdentifier(options.MessageTable, nameof(options.MessageTable)); |
| | 3 | 10 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 3 | 11 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 3 | 12 | | Required(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | 3 | 13 | | Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 3 | 14 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 15 | | |
| | | 16 | | // All three logical queues share one table, distinguished only by the queue column. Equal |
| | | 17 | | // names would make subscribers consume each other's rows (or re-consume dead letters). |
| | 3 | 18 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue) |
| | 3 | 19 | | || StringComparer.Ordinal.Equals(options.WorkerQueue, options.DeadLetterQueue) |
| | 3 | 20 | | || StringComparer.Ordinal.Equals(options.ResponseQueue, options.DeadLetterQueue)) |
| | | 21 | | { |
| | 3 | 22 | | throw new InvalidOperationException( |
| | 3 | 23 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.WorkerQueue)}, " + |
| | 3 | 24 | | $"{nameof(options.ResponseQueue)}, and {nameof(options.DeadLetterQueue)} must be distinct; they share on |
| | | 25 | | } |
| | | 26 | | |
| | 3 | 27 | | if (options.DeadLetterRetention is { } deadLetterRetention && deadLetterRetention <= TimeSpan.Zero) |
| | 3 | 28 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.DeadL |
| | | 29 | | |
| | 3 | 30 | | Positive(options.LockTimeout, nameof(options.LockTimeout)); |
| | 3 | 31 | | Positive(options.PublishRetryBaseDelay, nameof(options.PublishRetryBaseDelay)); |
| | 3 | 32 | | Positive(options.PublishRetryMaxDelay, nameof(options.PublishRetryMaxDelay)); |
| | 3 | 33 | | Positive(options.SubscriberRetryBaseDelay, nameof(options.SubscriberRetryBaseDelay)); |
| | 3 | 34 | | Positive(options.SubscriberRetryMaxDelay, nameof(options.SubscriberRetryMaxDelay)); |
| | | 35 | | |
| | 3 | 36 | | if (options.PublishMaxAttempts <= 0) |
| | 3 | 37 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Publi |
| | 3 | 38 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 3 | 39 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Publi |
| | 3 | 40 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 3 | 41 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(options.Subsc |
| | 3 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 45 | | public static void ValidateSubscriber( |
| | | 46 | | SqlServerAsyncResponseTransportOptions transportOptions, |
| | | 47 | | SqlServerSubscriberOptions subscriber, |
| | | 48 | | string role) |
| | | 49 | | { |
| | 3 | 50 | | ValidateSubscriber(subscriber, role); |
| | | 51 | | |
| | 3 | 52 | | if (subscriber.AckMode is not SqlServerAckMode.AckAfterEnqueue) |
| | 3 | 53 | | return; |
| | | 54 | | |
| | | 55 | | // SQL Server subscribers spend only the background drain at shutdown; the polling loop |
| | | 56 | | // stops with the host token and holds no connection that needs a bounded close. |
| | 3 | 57 | | ShutdownBudgetValidator.Validate( |
| | 3 | 58 | | "SQL Server", |
| | 3 | 59 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 3 | 60 | | transportOptions.HostShutdownTimeout, |
| | 3 | 61 | | ($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.Ba |
| | 3 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static void ValidateSubscriber(SqlServerSubscriberOptions subscriber, string role) |
| | | 65 | | { |
| | 3 | 66 | | if (subscriber.BatchSize <= 0) |
| | 3 | 67 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.BatchSize)} ({ |
| | 3 | 68 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 3 | 69 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAtt |
| | | 70 | | |
| | 3 | 71 | | Positive(subscriber.RedeliveryDelay, $"{nameof(subscriber.RedeliveryDelay)} ({role})"); |
| | 3 | 72 | | Positive(subscriber.EmptyPollDelay, $"{nameof(subscriber.EmptyPollDelay)} ({role})"); |
| | | 73 | | |
| | 3 | 74 | | switch (subscriber.AckMode) |
| | | 75 | | { |
| | | 76 | | case SqlServerAckMode.AckAfterHandlerCompletes: |
| | 3 | 77 | | return; |
| | | 78 | | case SqlServerAckMode.AckAfterEnqueue: |
| | 3 | 79 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 3 | 80 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.Backgr |
| | 3 | 81 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 3 | 82 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.Backgr |
| | 3 | 83 | | Positive(subscriber.BackgroundDrainTimeout, $"{nameof(subscriber.BackgroundDrainTimeout)} ({role})"); |
| | 3 | 84 | | return; |
| | | 85 | | default: |
| | 3 | 86 | | throw new InvalidOperationException($"{nameof(SqlServerSubscriberOptions)}.{nameof(subscriber.AckMode)} |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public static string Required(string? value, string name) |
| | 3 | 91 | | => !string.IsNullOrWhiteSpace(value) |
| | 3 | 92 | | ? value |
| | 3 | 93 | | : throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} must be conf |
| | | 94 | | |
| | | 95 | | public static void ValidateIdentifier(string? value, string name) |
| | | 96 | | { |
| | 3 | 97 | | if (string.IsNullOrWhiteSpace(value)) |
| | 3 | 98 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} must be config |
| | 3 | 99 | | if (!IsIdentifier(value)) |
| | 3 | 100 | | throw new InvalidOperationException( |
| | 3 | 101 | | $"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} '{value}' must be a simple SQL Server identifi |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | private static void Positive(TimeSpan value, string name) |
| | | 105 | | { |
| | 3 | 106 | | if (value <= TimeSpan.Zero) |
| | 3 | 107 | | throw new InvalidOperationException($"{nameof(SqlServerAsyncResponseTransportOptions)}.{name} must be positi |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static bool IsIdentifier(string value) |
| | | 111 | | { |
| | 3 | 112 | | if (value.Length == 0 || !(char.IsAsciiLetter(value[0]) || value[0] == '_')) |
| | 3 | 113 | | return false; |
| | 3 | 114 | | foreach (var c in value) |
| | | 115 | | { |
| | 3 | 116 | | if (!(char.IsAsciiLetterOrDigit(c) || c == '_')) |
| | 3 | 117 | | return false; |
| | | 118 | | } |
| | 3 | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | } |