| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Transports.Redis; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// DI registration for the Redis Streams AsyncResponse transport. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class RedisAsyncResponseTransportServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Registers Redis Streams as the worker transport and response ingress in one call: |
| | | 14 | | /// <list type="bullet"> |
| | | 15 | | /// <item><description>worker jobs are XADDed to the configured worker stream;</description></item> |
| | | 16 | | /// <item><description>a hosted consumer group subscriber reads worker entries and executes jobs;</description></ite |
| | | 17 | | /// <item><description>a hosted consumer group subscriber reads response entries and feeds them into <see cref="IAsy |
| | | 18 | | /// </list> |
| | | 19 | | /// Redis Streams is a transport, not the waiter/recovery channel. Pair it with |
| | | 20 | | /// <c>.WithRedisChannel()</c> when late responses must survive redeploys, or with |
| | | 21 | | /// <c>.WithInMemoryChannel()</c> for simple single-process waits. |
| | | 22 | | /// <para> |
| | | 23 | | /// Requires a <c>StackExchange.Redis.IConnectionMultiplexer</c> singleton registered by the |
| | | 24 | | /// host. Reuse the same multiplexer as the Redis channel package when both are installed. |
| | | 25 | | /// </para> |
| | | 26 | | /// </summary> |
| | | 27 | | public static AsyncResponseRegistrationBuilder WithRedisTransport( |
| | | 28 | | this AsyncResponseRegistrationBuilder builder, |
| | | 29 | | Action<RedisAsyncResponseTransportOptions> configure) |
| | | 30 | | { |
| | 3 | 31 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 32 | | |
| | 3 | 33 | | var services = builder.Services; |
| | 3 | 34 | | services.AddOptions(); |
| | 3 | 35 | | services.Configure(configure); |
| | | 36 | | |
| | 3 | 37 | | services.TryAddSingleton<RedisWorkerTransport>(); |
| | 3 | 38 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => |
| | 3 | 39 | | provider.GetRequiredService<RedisWorkerTransport>())); |
| | 3 | 40 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, RedisReplyTargetProvider>()); |
| | 3 | 41 | | services.AddSingleton(provider => |
| | 3 | 42 | | { |
| | 3 | 43 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 3 | 44 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 3 | 45 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<RedisAsyncResponseTransportO |
| | 3 | 46 | | return new AsyncResponseTransportMarker("Redis") |
| | 3 | 47 | | { |
| | 3 | 48 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == RedisAckMode.AckAfterEnqueue, |
| | 3 | 49 | | WorkerAckModePath = $"{nameof(RedisAsyncResponseTransportOptions)}.{nameof(options.WorkerSubscriber)}.{n |
| | 3 | 50 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == RedisAckMode.AckAfterEnqueue, |
| | 3 | 51 | | ResponseAckModePath = $"{nameof(RedisAsyncResponseTransportOptions)}.{nameof(options.ResponseSubscriber) |
| | 3 | 52 | | }; |
| | 3 | 53 | | }); |
| | | 54 | | |
| | 3 | 55 | | services.AddHostedService<RedisWorkerSubscriber>(); |
| | 3 | 56 | | services.AddHostedService<RedisResponseIngressSubscriber>(); |
| | | 57 | | |
| | 3 | 58 | | return builder; |
| | | 59 | | } |
| | | 60 | | } |