| | | 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, |
| | | 27 | | TimeProvider? timeProvider = null) |
| | 416 | 28 | | : base( |
| | 416 | 29 | | scopeFactory, |
| | 416 | 30 | | sql, |
| | 416 | 31 | | recoveryStateStore, |
| | 416 | 32 | | options.Value, |
| | 416 | 33 | | propagation, |
| | 416 | 34 | | logger, |
| | 416 | 35 | | channelTypeName: nameof(PostgreSqlAsyncResponseChannel), |
| | 416 | 36 | | providerName: "PostgreSQL", |
| | 416 | 37 | | activityTag: "postgresql", |
| | 416 | 38 | | subscriberRecordNoun: "row", |
| | 416 | 39 | | localDispatchRetryHint: "listener retry will pick it up", |
| | 416 | 40 | | timeProvider) |
| | | 41 | | { |
| | 416 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 2121 | 45 | | protected override string ChannelName(string correlationId) => $"{_options.NotificationChannel}:{correlationId}"; |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | 8680 | 48 | | protected override TimeSpan CurrentPollInterval() => _options.ListenerPollInterval; |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | protected override Task? StartWakeListener(CancellationToken cancellationToken) |
| | 726 | 52 | | => Task.Run(() => ListenLoopAsync(cancellationToken)); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | protected override IAsyncResponseWaiter<T> CreateWaiter<T>(Task<T> responseTask, Func<ValueTask> cleanupAsync) |
| | 409 | 56 | | => new PostgreSqlAsyncResponseWaiter<T>(responseTask, cleanupAsync); |
| | | 57 | | |
| | | 58 | | private async Task ListenLoopAsync(CancellationToken cancellationToken) |
| | | 59 | | { |
| | 363 | 60 | | var failures = 0; |
| | 363 | 61 | | while (!cancellationToken.IsCancellationRequested) |
| | | 62 | | { |
| | | 63 | | try |
| | | 64 | | { |
| | 362 | 65 | | await _store.ExecuteListenAsync(payload => |
| | 362 | 66 | | { |
| | 510 | 67 | | SignalDispatcher(string.IsNullOrEmpty(payload) ? null : payload); |
| | 510 | 68 | | return Task.CompletedTask; |
| | 362 | 69 | | }, cancellationToken).ConfigureAwait(false); |
| | 0 | 70 | | failures = 0; |
| | 0 | 71 | | } |
| | 358 | 72 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 73 | | { |
| | 358 | 74 | | return; |
| | | 75 | | } |
| | 4 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 4 | 78 | | failures++; |
| | 4 | 79 | | var delay = AsyncResponseRetry.Backoff(failures, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5) |
| | 4 | 80 | | _logger.LogWarning(ex, "PostgreSQL LISTEN loop failed; retrying in {Delay}.", delay); |
| | 4 | 81 | | await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| | | 82 | | } |
| | | 83 | | } |
| | 359 | 84 | | } |
| | | 85 | | } |