| | | 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.SqlServer.SqlServerChannelSql; |
| | | 4 | | global using DbChannelMessage = AsyncResponse.Channels.SqlServer.SqlServerChannelMessage; |
| | | 5 | | global using DbChannelOptions = AsyncResponse.Channels.SqlServer.SqlServerAsyncResponseChannelOptions; |
| | | 6 | | |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace AsyncResponse.Channels.SqlServer; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Microsoft SQL Server-backed response channel using an adaptive polling sweep for active waiter |
| | | 15 | | /// wakeups (SQL Server has no <c>LISTEN/NOTIFY</c>) and SQL Server tables for durable recovery state. |
| | | 16 | | /// Same-process deliveries bypass the sweep entirely; cross-process deliveries are picked up within |
| | | 17 | | /// <see cref="SqlServerAsyncResponseChannelOptions.ActivePollInterval"/>, and the sweep backs off to |
| | | 18 | | /// <see cref="SqlServerAsyncResponseChannelOptions.IdlePollInterval"/> while no waiters are subscribed. |
| | | 19 | | /// </summary> |
| | | 20 | | internal sealed class SqlServerAsyncResponseChannel : DbAsyncResponseChannelBase |
| | | 21 | | { |
| | | 22 | | /// <summary>Creates a SQL Server-backed async-response channel.</summary> |
| | | 23 | | public SqlServerAsyncResponseChannel( |
| | | 24 | | IServiceScopeFactory scopeFactory, |
| | | 25 | | SqlServerChannelSql sql, |
| | | 26 | | IRecoveryStateStore recoveryStateStore, |
| | | 27 | | IOptions<SqlServerAsyncResponseChannelOptions> options, |
| | | 28 | | AsyncResponseContextPropagation propagation, |
| | | 29 | | ILogger<SqlServerAsyncResponseChannel> logger) |
| | 3 | 30 | | : base( |
| | 3 | 31 | | scopeFactory, |
| | 3 | 32 | | sql, |
| | 3 | 33 | | recoveryStateStore, |
| | 3 | 34 | | options.Value, |
| | 3 | 35 | | propagation, |
| | 3 | 36 | | logger, |
| | 3 | 37 | | channelTypeName: nameof(SqlServerAsyncResponseChannel), |
| | 3 | 38 | | providerName: "SQL Server", |
| | 3 | 39 | | activityTag: "sqlserver", |
| | 3 | 40 | | subscriberRecordNoun: "row", |
| | 3 | 41 | | localDispatchRetryHint: "the sweep retry will pick it up") |
| | | 42 | | { |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 3 | 46 | | protected override string ChannelName(string correlationId) => $"{_options.SchemaName}.{_options.MessageTable}:{corr |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The adaptive sweep cadence: tight while any waiter is subscribed (cross-process deliveries |
| | | 50 | | /// must land promptly), backed off while the channel is idle so an inactive application does not |
| | | 51 | | /// keep polling the database at delivery latency. |
| | | 52 | | /// </summary> |
| | | 53 | | protected override TimeSpan CurrentPollInterval() |
| | 3 | 54 | | => _subscriptions.IsEmpty ? _options.IdlePollInterval : _options.ActivePollInterval; |
| | | 55 | | |
| | | 56 | | // No StartWakeListener override: SQL Server has no push notification, so the adaptive dispatch |
| | | 57 | | // sweep above IS the cross-process wake mechanism and the base keeps no listener task. |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | protected override IAsyncResponseWaiter<T> CreateWaiter<T>(Task<T> responseTask, Func<ValueTask> cleanupAsync) |
| | 1 | 61 | | => new SqlServerAsyncResponseWaiter<T>(responseTask, cleanupAsync); |
| | | 62 | | } |