| | | 1 | | using AsyncResponse.Internal; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.PostgreSQL; |
| | | 4 | | |
| | | 5 | | internal static class PostgreSqlTransportOptionsValidator |
| | | 6 | | { |
| | | 7 | | public static void ValidateCommon(PostgreSqlAsyncResponseTransportOptions options) |
| | | 8 | | { |
| | 872 | 9 | | ValidateIdentifier(options.SchemaName, nameof(options.SchemaName)); |
| | 868 | 10 | | ValidateIdentifier(options.MessageTable, nameof(options.MessageTable)); |
| | 866 | 11 | | ValidateIdentifier(options.NotificationChannel, nameof(options.NotificationChannel)); |
| | 864 | 12 | | Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 864 | 13 | | Required(options.ResponseQueue, nameof(options.ResponseQueue)); |
| | 864 | 14 | | Required(options.DeadLetterQueue, nameof(options.DeadLetterQueue)); |
| | 864 | 15 | | Required(options.CorrelationIdHeader, nameof(options.CorrelationIdHeader)); |
| | 862 | 16 | | Required(options.DefaultReplyTargetName, nameof(options.DefaultReplyTargetName)); |
| | | 17 | | |
| | | 18 | | // All three logical queues share one table, distinguished only by the queue column. Equal |
| | | 19 | | // names would make subscribers consume each other's rows (or re-consume dead letters). |
| | 862 | 20 | | if (StringComparer.Ordinal.Equals(options.WorkerQueue, options.ResponseQueue) |
| | 862 | 21 | | || StringComparer.Ordinal.Equals(options.WorkerQueue, options.DeadLetterQueue) |
| | 862 | 22 | | || StringComparer.Ordinal.Equals(options.ResponseQueue, options.DeadLetterQueue)) |
| | | 23 | | { |
| | 2 | 24 | | throw new InvalidOperationException( |
| | 2 | 25 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.WorkerQueue)}, " + |
| | 2 | 26 | | $"{nameof(options.ResponseQueue)}, and {nameof(options.DeadLetterQueue)} must be distinct; they share on |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | // The queue table's derived index names reserve suffix space, but a table whose name ends |
| | | 30 | | // exactly where a reserved stem truncates can still derive its own name, silently skipping |
| | | 31 | | // index creation (indexes and tables share PostgreSQL's relation namespace). |
| | 860 | 32 | | (string Role, string Name)[] namePlan = |
| | 860 | 33 | | [ |
| | 860 | 34 | | ($"{nameof(options.MessageTable)} table", options.MessageTable), |
| | 860 | 35 | | ("dequeue index (derived from MessageTable)", PostgreSqlTransportStore.IndexName(options.MessageTable, "read |
| | 860 | 36 | | ("created index (derived from MessageTable)", PostgreSqlTransportStore.IndexName(options.MessageTable, "crea |
| | 860 | 37 | | ]; |
| | 860 | 38 | | RelationalNamePlan.RequireDistinct( |
| | 860 | 39 | | namePlan, |
| | 860 | 40 | | nameof(PostgreSqlAsyncResponseTransportOptions), |
| | 860 | 41 | | $"; rename {nameof(options.MessageTable)} so the derived index names stay distinct."); |
| | | 42 | | |
| | | 43 | | // Timer-armed knobs get the .NET timer ceiling (LockTimeout also drives the in-process |
| | | 44 | | // lease-renewal Task.Delay at a third of its value); DeadLetterRetention and RedeliveryDelay are |
| | | 45 | | // database-side "now + value" stamps and get the persistence bound instead. |
| | 856 | 46 | | if (options.DeadLetterRetention is { } deadLetterRetention) |
| | 26 | 47 | | AsyncResponseChannelOptions.EnsurePersistedTtl(deadLetterRetention, nameof(PostgreSqlAsyncResponseTransportO |
| | | 48 | | |
| | 852 | 49 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.LockTimeout, nameof(PostgreSqlAsyncResponseTransportOption |
| | 848 | 50 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryBaseDelay, nameof(PostgreSqlAsyncResponseTrans |
| | 848 | 51 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.PublishRetryMaxDelay, nameof(PostgreSqlAsyncResponseTransp |
| | 848 | 52 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryBaseDelay, nameof(PostgreSqlAsyncResponseTr |
| | 848 | 53 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.SubscriberRetryMaxDelay, nameof(PostgreSqlAsyncResponseTra |
| | 848 | 54 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.ShutdownTimeout, nameof(PostgreSqlAsyncResponseTransportOp |
| | | 55 | | |
| | 848 | 56 | | if (options.PublishMaxAttempts <= 0) |
| | 2 | 57 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Publ |
| | 846 | 58 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 2 | 59 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Publ |
| | 844 | 60 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 2 | 61 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Subs |
| | 842 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 65 | | public static void ValidateSubscriber( |
| | | 66 | | PostgreSqlAsyncResponseTransportOptions transportOptions, |
| | | 67 | | PostgreSqlSubscriberOptions subscriber, |
| | | 68 | | string role) |
| | | 69 | | { |
| | 878 | 70 | | ValidateSubscriber(subscriber, role); |
| | | 71 | | |
| | 874 | 72 | | if (subscriber.AckMode is not PostgreSqlAckMode.AckAfterEnqueue) |
| | 824 | 73 | | return; |
| | | 74 | | |
| | | 75 | | // PostgreSQL spends the background drain plus the LISTEN-task join (ShutdownTimeout) |
| | | 76 | | // at shutdown; both must fit inside the host budget or ACKed work is truncated. |
| | 50 | 77 | | ShutdownBudgetValidator.Validate( |
| | 50 | 78 | | "PostgreSQL", |
| | 50 | 79 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 50 | 80 | | transportOptions.HostShutdownTimeout, |
| | 50 | 81 | | ($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.B |
| | 50 | 82 | | ($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(transportOptions.ShutdownTimeout)}", transportO |
| | 46 | 83 | | } |
| | | 84 | | |
| | | 85 | | public static void ValidateSubscriber(PostgreSqlSubscriberOptions subscriber, string role) |
| | | 86 | | { |
| | 894 | 87 | | if (subscriber.BatchSize <= 0) |
| | 2 | 88 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.BatchSize)} ( |
| | 892 | 89 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 2 | 90 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAt |
| | | 91 | | |
| | | 92 | | // RedeliveryDelay is a database-side "now + delay" visibility stamp (persistence bound); |
| | | 93 | | // EmptyPollDelay arms the idle-poll Task.Delay (timer ceiling). |
| | 890 | 94 | | AsyncResponseChannelOptions.EnsurePersistedTtl(subscriber.RedeliveryDelay, nameof(PostgreSqlSubscriberOptions), |
| | 888 | 95 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.EmptyPollDelay, nameof(PostgreSqlSubscriberOptions), $" |
| | | 96 | | |
| | 888 | 97 | | switch (subscriber.AckMode) |
| | | 98 | | { |
| | | 99 | | case PostgreSqlAckMode.AckAfterHandlerCompletes: |
| | 826 | 100 | | return; |
| | | 101 | | case PostgreSqlAckMode.AckAfterEnqueue: |
| | 60 | 102 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 6 | 103 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.Backg |
| | 54 | 104 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 2 | 105 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.Backg |
| | 52 | 106 | | AsyncResponseChannelOptions.EnsureTimerBacked(subscriber.BackgroundDrainTimeout, nameof(PostgreSqlSubscr |
| | 52 | 107 | | return; |
| | | 108 | | default: |
| | 2 | 109 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.AckMode)} |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public static string Required(string? value, string name) |
| | 4353 | 114 | | => !string.IsNullOrWhiteSpace(value) |
| | 4353 | 115 | | ? value |
| | 4353 | 116 | | : throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} must be con |
| | | 117 | | |
| | | 118 | | public static void ValidateIdentifier(string? value, string name) |
| | | 119 | | { |
| | 2606 | 120 | | if (string.IsNullOrWhiteSpace(value)) |
| | 2 | 121 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} must be confi |
| | 2604 | 122 | | if (!IsIdentifier(value)) |
| | 4 | 123 | | throw new InvalidOperationException( |
| | 4 | 124 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} '{value}' must be a simple PostgreSQL identif |
| | | 125 | | // PostgreSQL TRUNCATES over-limit identifiers silently (a NOTICE, not an error), so an |
| | | 126 | | // over-limit configured name would create/address an object under a different name. |
| | 2600 | 127 | | if (value.Length > 63) |
| | 2 | 128 | | throw new InvalidOperationException( |
| | 2 | 129 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} '{value}' is {value.Length} characters; Postg |
| | 2598 | 130 | | } |
| | | 131 | | |
| | | 132 | | private static bool IsIdentifier(string value) |
| | | 133 | | { |
| | 2604 | 134 | | if (value.Length == 0 || !(char.IsAsciiLetter(value[0]) || value[0] == '_')) |
| | 2 | 135 | | return false; |
| | 127438 | 136 | | foreach (var c in value) |
| | | 137 | | { |
| | 61118 | 138 | | if (!(char.IsAsciiLetterOrDigit(c) || c == '_')) |
| | 2 | 139 | | return false; |
| | | 140 | | } |
| | 2600 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | } |