| | | 1 | | namespace AsyncResponse.Channels.PostgreSQL; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Options for the PostgreSQL-backed async-response channel. |
| | | 5 | | /// <para> |
| | | 6 | | /// Active waiters are woken with PostgreSQL <c>LISTEN/NOTIFY</c>. Response envelopes are stored in a |
| | | 7 | | /// table and notifications carry only the message id, avoiding PostgreSQL's small NOTIFY payload |
| | | 8 | | /// limit. Durable <see cref="RecoveryState"/> entries live in a separate table so late responses can |
| | | 9 | | /// resume or fail flows after the original waiter process dies. |
| | | 10 | | /// </para> |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class PostgreSqlAsyncResponseChannelOptions : DurableAsyncResponseChannelOptions |
| | | 13 | | { |
| | | 14 | | /// <summary>The channel name reported to the startup validator.</summary> |
| | | 15 | | public const string ChannelName = "PostgreSQL"; |
| | | 16 | | |
| | | 17 | | /// <summary>Database schema that contains the channel tables. Default: <c>public</c>.</summary> |
| | 3882 | 18 | | public string SchemaName { get; set; } = "public"; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Table storing durable recovery registrations. Each waiter registration is one row keyed by |
| | | 22 | | /// correlation id and registration id. |
| | | 23 | | /// </summary> |
| | 5619 | 24 | | public string RecoveryStateTable { get; set; } = "asyncresponse_recovery_state"; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Table storing response envelopes until they expire. Notifications carry message ids; waiters |
| | | 28 | | /// load the envelope from this table. |
| | | 29 | | /// </summary> |
| | 9062 | 30 | | public string MessageTable { get; set; } = "asyncresponse_channel_messages"; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Table storing short-lived live-subscriber heartbeats for watchdog liveness and the publish |
| | | 34 | | /// fast path. |
| | | 35 | | /// </summary> |
| | 5231 | 36 | | public string SubscriberTable { get; set; } = "asyncresponse_channel_subscribers"; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// PostgreSQL notification channel used to wake local listener loops. Must be a simple |
| | | 40 | | /// PostgreSQL identifier. Default: <c>asyncresponse_channel_notify</c>. |
| | | 41 | | /// </summary> |
| | 4899 | 42 | | public string NotificationChannel { get; set; } = "asyncresponse_channel_notify"; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates the schema, tables, and indexes on first use. Disable when migrations provision them |
| | | 46 | | /// out of band. |
| | | 47 | | /// </summary> |
| | 1004 | 48 | | public bool AutoCreateSchema { get; set; } = true; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// How long response-envelope rows are retained for active waiter delivery and missed-notify |
| | | 52 | | /// recovery. Expired rows are pruned opportunistically during channel operations. |
| | | 53 | | /// </summary> |
| | 4247 | 54 | | public TimeSpan MessageRetention { get; set; } = TimeSpan.FromHours(1); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// How long a publisher waits for a live waiter to acknowledge loading a response envelope |
| | | 58 | | /// before treating the response as lost-subscriber delivery. Default: 5 seconds. |
| | | 59 | | /// </summary> |
| | 3406 | 60 | | public TimeSpan DeliveryConfirmationTimeout { get; set; } = TimeSpan.FromSeconds(5); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Poll interval used while a publisher waits for delivery acknowledgement. Default: 50 ms. |
| | | 64 | | /// </summary> |
| | 3137 | 65 | | public TimeSpan DeliveryConfirmationPollInterval { get; set; } = TimeSpan.FromMilliseconds(50); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Fallback poll interval used by the listener loop to catch messages if a notification is |
| | | 69 | | /// missed during reconnect. Default: 250 ms. |
| | | 70 | | /// </summary> |
| | 10517 | 71 | | public TimeSpan ListenerPollInterval { get; set; } = TimeSpan.FromMilliseconds(250); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Minimum interval between full safety-net sweeps. A full sweep queries the store once per |
| | | 75 | | /// subscribed correlation id, so its idle cost is W queries per <see cref="ListenerPollInterval"/> |
| | | 76 | | /// tick with W in-flight waiters. On this provider the sweep only covers wake notifications |
| | | 77 | | /// lost in failure windows (NOTIFY carries normal delivery), so this bounds idle |
| | | 78 | | /// database load without touching normal delivery latency — it stretches only the worst-case |
| | | 79 | | /// recovery of a LOST wake. Default: 5 seconds (an unbounded null swept every waiter on every |
| | | 80 | | /// 250 ms tick — W sequential queries per tick of pure idle load). Set null to sweep on every |
| | | 81 | | /// poll tick. |
| | | 82 | | /// </summary> |
| | 6784 | 83 | | public TimeSpan? FullSweepInterval { get; set; } = TimeSpan.FromSeconds(5); |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Number of pending response messages loaded per subscribed correlation id per listener pass. |
| | | 87 | | /// Default: 64. |
| | | 88 | | /// </summary> |
| | 3426 | 89 | | public int PendingMessageBatchSize { get; set; } = 64; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Minimum interval between incremental reconciliation passes over retained message history. |
| | | 93 | | /// Normal scans keep their forward cursor; reconciliation catches late commits behind it, |
| | | 94 | | /// including rows already acknowledged by another process. One history page is read per |
| | | 95 | | /// dispatch pass so a long history cannot monopolize delivery to other correlations. |
| | | 96 | | /// Default: 5 seconds; large histories take additional poll intervals to reconcile. |
| | | 97 | | /// </summary> |
| | 1815 | 98 | | public TimeSpan HistoryReconciliationInterval { get; set; } = TimeSpan.FromSeconds(5); |
| | | 99 | | |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// How often a live waiter refreshes its subscriber heartbeat row. Default: 10 seconds. |
| | | 103 | | /// </summary> |
| | 5463 | 104 | | public TimeSpan SubscriberHeartbeatInterval { get; set; } = TimeSpan.FromSeconds(10); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// How long a subscriber heartbeat remains live without refresh. Keep this above |
| | | 108 | | /// <see cref="SubscriberHeartbeatInterval"/>. Default: 30 seconds. |
| | | 109 | | /// </summary> |
| | 3227 | 110 | | public TimeSpan SubscriberHeartbeatTimeout { get; set; } = TimeSpan.FromSeconds(30); |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Minimum interval between opportunistic prunes of expired channel rows. Pruning is housekeeping |
| | | 114 | | /// only (read queries filter on expiry), so throttling it keeps publishes off a full-table delete |
| | | 115 | | /// on every call. Set to <see cref="TimeSpan.Zero"/> to prune on every operation. Default: 30 seconds. |
| | | 116 | | /// </summary> |
| | 3417 | 117 | | public TimeSpan PruneInterval { get; set; } = TimeSpan.FromSeconds(30); |
| | | 118 | | |
| | | 119 | | /// <summary>Maximum attempts for a response-row insert. Set to 1 to disable publish retries. Default: 3.</summary> |
| | 1992 | 120 | | public int PublishMaxAttempts { get; set; } = 3; |
| | | 121 | | |
| | | 122 | | /// <summary>Initial delay before retrying a failed response-row insert. Default: 50 ms.</summary> |
| | 2858 | 123 | | public TimeSpan PublishRetryBaseDelay { get; set; } = TimeSpan.FromMilliseconds(50); |
| | | 124 | | |
| | | 125 | | /// <summary>Maximum delay between response-row insert retries. Default: 1 second.</summary> |
| | 2860 | 126 | | public TimeSpan PublishRetryMaxDelay { get; set; } = TimeSpan.FromSeconds(1); |
| | | 127 | | |
| | | 128 | | /// <summary>Validates the option values and throws on misconfiguration.</summary> |
| | | 129 | | public void Validate() |
| | | 130 | | { |
| | | 131 | | // Shared channel knobs (RecoveryStateExpiry, DefaultTimeout, DisposalDrainTimeout) go |
| | | 132 | | // through the ONE base guard set — a bespoke duplicate here silently missed every knob |
| | | 133 | | // added to the base later (DisposalDrainTimeout was validated nowhere on this provider). |
| | 924 | 134 | | ValidateShared(nameof(PostgreSqlAsyncResponseChannelOptions)); |
| | | 135 | | |
| | 918 | 136 | | PostgreSqlChannelSql.ValidateIdentifier(SchemaName, nameof(SchemaName)); |
| | 912 | 137 | | PostgreSqlChannelSql.ValidateIdentifier(RecoveryStateTable, nameof(RecoveryStateTable)); |
| | 912 | 138 | | PostgreSqlChannelSql.ValidateIdentifier(MessageTable, nameof(MessageTable)); |
| | 908 | 139 | | PostgreSqlChannelSql.ValidateIdentifier(SubscriberTable, nameof(SubscriberTable)); |
| | 908 | 140 | | PostgreSqlChannelSql.ValidateIdentifier(NotificationChannel, nameof(NotificationChannel)); |
| | 908 | 141 | | PostgreSqlChannelSql.ValidateNamePlan(this); |
| | | 142 | | |
| | 902 | 143 | | EnsurePersistedTtl(MessageRetention, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(MessageRetention)); |
| | 898 | 144 | | EnsurePersistedTtl(DeliveryConfirmationTimeout, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(DeliveryCo |
| | 894 | 145 | | if (MessageRetention <= DeliveryConfirmationTimeout) |
| | 2 | 146 | | throw new InvalidOperationException( |
| | 2 | 147 | | $"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(MessageRetention)} must exceed " + |
| | 2 | 148 | | $"{nameof(DeliveryConfirmationTimeout)}: a message row pruned inside the confirmation window is " + |
| | 2 | 149 | | "indistinguishable from an acknowledged one, so the response would be reported delivered and " + |
| | 2 | 150 | | "lost-response recovery silently skipped."); |
| | 892 | 151 | | EnsureTimerBacked(DeliveryConfirmationPollInterval, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(Delive |
| | 888 | 152 | | EnsureTimerBacked(ListenerPollInterval, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(ListenerPollInterv |
| | 884 | 153 | | EnsureTimerBacked(HistoryReconciliationInterval, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(HistoryRe |
| | 882 | 154 | | if (FullSweepInterval is { } fullSweepInterval) |
| | 818 | 155 | | EnsureTimerBacked(fullSweepInterval, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(FullSweepInterval |
| | 882 | 156 | | EnsureTimerBacked(SubscriberHeartbeatInterval, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(SubscriberH |
| | 882 | 157 | | EnsurePersistedTtl(SubscriberHeartbeatTimeout, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(SubscriberH |
| | | 158 | | |
| | 880 | 159 | | if (MaxRemoteStackTraceLength < 0) |
| | 2 | 160 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(MaxRemoteStack |
| | | 161 | | |
| | 878 | 162 | | if (PendingMessageBatchSize <= 0) |
| | 2 | 163 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(PendingMessage |
| | | 164 | | |
| | 876 | 165 | | if (SubscriberHeartbeatInterval >= SubscriberHeartbeatTimeout) |
| | 2 | 166 | | throw new InvalidOperationException( |
| | 2 | 167 | | $"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(SubscriberHeartbeatInterval)} must be less tha |
| | 2 | 168 | | $"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(SubscriberHeartbeatTimeout)}."); |
| | | 169 | | |
| | 874 | 170 | | if (PruneInterval < TimeSpan.Zero) |
| | 2 | 171 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(PruneInterval) |
| | | 172 | | |
| | 872 | 173 | | if (PublishMaxAttempts <= 0) |
| | 2 | 174 | | throw new InvalidOperationException($"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(PublishMaxAtte |
| | | 175 | | |
| | 870 | 176 | | EnsureTimerBacked(PublishRetryBaseDelay, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(PublishRetryBaseD |
| | 870 | 177 | | EnsureTimerBacked(PublishRetryMaxDelay, nameof(PostgreSqlAsyncResponseChannelOptions), nameof(PublishRetryMaxDel |
| | 868 | 178 | | if (PublishRetryBaseDelay > PublishRetryMaxDelay) |
| | 2 | 179 | | throw new InvalidOperationException( |
| | 2 | 180 | | $"{nameof(PostgreSqlAsyncResponseChannelOptions)}.{nameof(PublishRetryBaseDelay)} cannot exceed {nameof( |
| | 866 | 181 | | } |
| | | 182 | | |
| | | 183 | | } |