| | | 1 | | // Binds the shared database-channel machinery (src/Channels/Shared/DbChannelShared.cs, compiled |
| | | 2 | | // into this project) to this provider's concrete seam types. See the note atop the shared file. |
| | | 3 | | global using DbChannelStore = AsyncResponse.Channels.PostgreSQL.PostgreSqlChannelSql; |
| | | 4 | | global using DbChannelMessage = AsyncResponse.Channels.PostgreSQL.PostgreSqlChannelMessage; |
| | | 5 | | global using DbChannelOptions = AsyncResponse.Channels.PostgreSQL.PostgreSqlAsyncResponseChannelOptions; |
| | | 6 | | |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace AsyncResponse.Channels.PostgreSQL; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// PostgreSQL-backed response channel using <c>LISTEN/NOTIFY</c> for active waiter wakeups and |
| | | 15 | | /// PostgreSQL tables for durable recovery state. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class PostgreSqlAsyncResponseChannel : DbAsyncResponseChannelBase |
| | | 18 | | { |
| | | 19 | | /// <summary>Creates a PostgreSQL-backed async-response channel.</summary> |
| | | 20 | | public PostgreSqlAsyncResponseChannel( |
| | | 21 | | IServiceScopeFactory scopeFactory, |
| | | 22 | | PostgreSqlChannelSql sql, |
| | | 23 | | IRecoveryStateStore recoveryStateStore, |
| | | 24 | | IOptions<PostgreSqlAsyncResponseChannelOptions> options, |
| | | 25 | | AsyncResponseContextPropagation propagation, |
| | | 26 | | ILogger<PostgreSqlAsyncResponseChannel> logger) |
| | 3 | 27 | | : base( |
| | 3 | 28 | | scopeFactory, |
| | 3 | 29 | | sql, |
| | 3 | 30 | | recoveryStateStore, |
| | 3 | 31 | | options.Value, |
| | 3 | 32 | | propagation, |
| | 3 | 33 | | logger, |
| | 3 | 34 | | channelTypeName: nameof(PostgreSqlAsyncResponseChannel), |
| | 3 | 35 | | providerName: "PostgreSQL", |
| | 3 | 36 | | activityTag: "postgresql", |
| | 3 | 37 | | subscriberRecordNoun: "row", |
| | 3 | 38 | | localDispatchRetryHint: "listener retry will pick it up") |
| | | 39 | | { |
| | 3 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 3 | 43 | | protected override string ChannelName(string correlationId) => $"{_options.NotificationChannel}:{correlationId}"; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 3 | 46 | | protected override TimeSpan CurrentPollInterval() => _options.ListenerPollInterval; |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | protected override Task? StartWakeListener(CancellationToken cancellationToken) |
| | 3 | 50 | | => Task.Run(() => ListenLoopAsync(cancellationToken)); |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | protected override IAsyncResponseWaiter<T> CreateWaiter<T>(Task<T> responseTask, Func<ValueTask> cleanupAsync) |
| | 1 | 54 | | => new PostgreSqlAsyncResponseWaiter<T>(responseTask, cleanupAsync); |
| | | 55 | | |
| | | 56 | | private async Task ListenLoopAsync(CancellationToken cancellationToken) |
| | | 57 | | { |
| | 3 | 58 | | var failures = 0; |
| | 3 | 59 | | while (!cancellationToken.IsCancellationRequested) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | await _store.ExecuteListenAsync(payload => |
| | 3 | 64 | | { |
| | 1 | 65 | | SignalDispatcher(string.IsNullOrEmpty(payload) ? null : payload); |
| | 1 | 66 | | return Task.CompletedTask; |
| | 3 | 67 | | }, cancellationToken).ConfigureAwait(false); |
| | 1 | 68 | | failures = 0; |
| | 1 | 69 | | } |
| | 3 | 70 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 71 | | { |
| | 1 | 72 | | return; |
| | | 73 | | } |
| | 3 | 74 | | catch (Exception ex) |
| | | 75 | | { |
| | 3 | 76 | | failures++; |
| | 3 | 77 | | var delay = AsyncResponseRetry.Backoff(failures, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5) |
| | 3 | 78 | | _logger.LogWarning(ex, "PostgreSQL LISTEN loop failed; retrying in {Delay}.", delay); |
| | 3 | 79 | | await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| | | 80 | | } |
| | | 81 | | } |
| | 1 | 82 | | } |
| | | 83 | | } |