| | | 1 | | namespace AsyncResponse.Transports.PostgreSQL; |
| | | 2 | | |
| | | 3 | | internal static class PostgreSqlTransportOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void ValidateCommon(PostgreSqlAsyncResponseTransportOptions options) |
| | | 6 | | { |
| | 3 | 7 | | ValidateIdentifier(options.SchemaName, nameof(options.SchemaName)); |
| | 3 | 8 | | ValidateIdentifier(options.MessageTable, nameof(options.MessageTable)); |
| | 3 | 9 | | ValidateIdentifier(options.NotificationChannel, nameof(options.NotificationChannel)); |
| | 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(PostgreSqlAsyncResponseTransportOptions)}.{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(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Dead |
| | | 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)); |
| | 3 | 35 | | Positive(options.ShutdownTimeout, nameof(options.ShutdownTimeout)); |
| | | 36 | | |
| | 3 | 37 | | if (options.PublishMaxAttempts <= 0) |
| | 3 | 38 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Publ |
| | 3 | 39 | | if (options.PublishRetryBaseDelay > options.PublishRetryMaxDelay) |
| | 3 | 40 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Publ |
| | 3 | 41 | | if (options.SubscriberRetryBaseDelay > options.SubscriberRetryMaxDelay) |
| | 3 | 42 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(options.Subs |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary>Validates the supplied subscriber options together with the transport-wide shutdown budget.</summary> |
| | | 46 | | public static void ValidateSubscriber( |
| | | 47 | | PostgreSqlAsyncResponseTransportOptions transportOptions, |
| | | 48 | | PostgreSqlSubscriberOptions subscriber, |
| | | 49 | | string role) |
| | | 50 | | { |
| | 3 | 51 | | ValidateSubscriber(subscriber, role); |
| | | 52 | | |
| | 3 | 53 | | if (subscriber.AckMode is not PostgreSqlAckMode.AckAfterEnqueue) |
| | 3 | 54 | | return; |
| | | 55 | | |
| | | 56 | | // PostgreSQL spends the background drain plus the LISTEN-task join (ShutdownTimeout) |
| | | 57 | | // at shutdown; both must fit inside the host budget or ACKed work is truncated. |
| | 3 | 58 | | ShutdownBudgetValidator.Validate( |
| | 3 | 59 | | "PostgreSQL", |
| | 3 | 60 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(transportOptions.HostShutdownTimeout)}", |
| | 3 | 61 | | transportOptions.HostShutdownTimeout, |
| | 3 | 62 | | ($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.BackgroundDrainTimeout)} ({role})", subscriber.B |
| | 3 | 63 | | ($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{nameof(transportOptions.ShutdownTimeout)}", transportO |
| | 3 | 64 | | } |
| | | 65 | | |
| | | 66 | | public static void ValidateSubscriber(PostgreSqlSubscriberOptions subscriber, string role) |
| | | 67 | | { |
| | 3 | 68 | | if (subscriber.BatchSize <= 0) |
| | 3 | 69 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.BatchSize)} ( |
| | 3 | 70 | | if (subscriber.MaxDeliveryAttempts < 0) |
| | 3 | 71 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.MaxDeliveryAt |
| | | 72 | | |
| | 3 | 73 | | Positive(subscriber.RedeliveryDelay, $"{nameof(subscriber.RedeliveryDelay)} ({role})"); |
| | 3 | 74 | | Positive(subscriber.EmptyPollDelay, $"{nameof(subscriber.EmptyPollDelay)} ({role})"); |
| | | 75 | | |
| | 3 | 76 | | switch (subscriber.AckMode) |
| | | 77 | | { |
| | | 78 | | case PostgreSqlAckMode.AckAfterHandlerCompletes: |
| | 3 | 79 | | return; |
| | | 80 | | case PostgreSqlAckMode.AckAfterEnqueue: |
| | 3 | 81 | | if (subscriber.BackgroundWorkerCount <= 0) |
| | 3 | 82 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.Backg |
| | 3 | 83 | | if (subscriber.BackgroundQueueCapacity <= 0) |
| | 3 | 84 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.Backg |
| | 3 | 85 | | Positive(subscriber.BackgroundDrainTimeout, $"{nameof(subscriber.BackgroundDrainTimeout)} ({role})"); |
| | 3 | 86 | | return; |
| | | 87 | | default: |
| | 3 | 88 | | throw new InvalidOperationException($"{nameof(PostgreSqlSubscriberOptions)}.{nameof(subscriber.AckMode)} |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public static string Required(string? value, string name) |
| | 3 | 93 | | => !string.IsNullOrWhiteSpace(value) |
| | 3 | 94 | | ? value |
| | 3 | 95 | | : throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} must be con |
| | | 96 | | |
| | | 97 | | public static void ValidateIdentifier(string? value, string name) |
| | | 98 | | { |
| | 3 | 99 | | if (string.IsNullOrWhiteSpace(value)) |
| | 3 | 100 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} must be confi |
| | 3 | 101 | | if (!IsIdentifier(value)) |
| | 3 | 102 | | throw new InvalidOperationException( |
| | 3 | 103 | | $"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} '{value}' must be a simple PostgreSQL identif |
| | 3 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static void Positive(TimeSpan value, string name) |
| | | 107 | | { |
| | 3 | 108 | | if (value <= TimeSpan.Zero) |
| | 3 | 109 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseTransportOptions)}.{name} must be posit |
| | 3 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static bool IsIdentifier(string value) |
| | | 113 | | { |
| | 3 | 114 | | if (value.Length == 0 || !(char.IsAsciiLetter(value[0]) || value[0] == '_')) |
| | 3 | 115 | | return false; |
| | 3 | 116 | | foreach (var c in value) |
| | | 117 | | { |
| | 3 | 118 | | if (!(char.IsAsciiLetterOrDigit(c) || c == '_')) |
| | 3 | 119 | | return false; |
| | | 120 | | } |
| | 3 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |