| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Channels.Redis; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// DI registration for the Redis-backed AsyncResponse channel package. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class RedisAsyncResponseServiceCollectionExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Registers Redis pub/sub as the response channel — behind <see cref="IAsyncResponsePublisher"/>, |
| | | 15 | | /// <see cref="IAsyncResponseSubscriber"/>, and <see cref="IRecoverableAsyncResponseSubscriber"/> — |
| | | 16 | | /// together with the durable Redis recovery-state store, so a response that arrives after the |
| | | 17 | | /// waiter died (e.g. a redeploy) can still resume or fail the owning flow. It also registers |
| | | 18 | | /// <see cref="IRecoverableAsyncResponseBuilder"/> for fluent durable recovery flows. The same |
| | | 19 | | /// channel instance serves the engine's recovery-state scanner and active-subscriber probe, so the |
| | | 20 | | /// watchdog works against Redis automatically. |
| | | 21 | | /// <para> |
| | | 22 | | /// Requires a <c>StackExchange.Redis.IConnectionMultiplexer</c> singleton registered by the |
| | | 23 | | /// host — reuse the application's existing multiplexer; don't create a second connection. |
| | | 24 | | /// Publisher/subscriber resolve to one shared instance: per-interface instances would split the |
| | | 25 | | /// channel's internal per-channel state. |
| | | 26 | | /// </para> |
| | | 27 | | /// </summary> |
| | | 28 | | public static AsyncResponseRegistrationBuilder WithRedisChannel( |
| | | 29 | | this AsyncResponseRegistrationBuilder builder, |
| | | 30 | | Action<RedisAsyncResponseOptions>? configure = null) |
| | | 31 | | { |
| | 438 | 32 | | var services = builder.Services; |
| | 438 | 33 | | services.AddOptions(); |
| | 438 | 34 | | if (configure is not null) |
| | | 35 | | { |
| | 372 | 36 | | services.Configure(configure); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Durable recovery store; also the watchdog's recovery-state scanner. |
| | 438 | 40 | | services.TryAddSingleton<RedisRecoveryStateStore>(); |
| | 865 | 41 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateStore>(provider => provider.GetRequiredService<RedisR |
| | 773 | 42 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateScanner>(provider => provider.GetRequiredService<Redi |
| | | 43 | | |
| | | 44 | | // Redis pub/sub response channel: publisher + subscriber + the watchdog's liveness probe, |
| | | 45 | | // all one shared instance. |
| | 438 | 46 | | services.TryAddSingleton<RedisAsyncResponseChannel>(); |
| | 851 | 47 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponsePublisher>(provider => provider.GetRequiredService<Re |
| | 766 | 48 | | services.Replace(ServiceDescriptor.Singleton<IRawAsyncResponsePublisher>(provider => provider.GetRequiredService |
| | 799 | 49 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseSubscriber>(provider => provider.GetRequiredService<R |
| | 775 | 50 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseSubscriber>(provider => provider.GetRequir |
| | 775 | 51 | | services.Replace(ServiceDescriptor.Singleton<IActiveSubscriberProbe>(provider => provider.GetRequiredService<Red |
| | | 52 | | |
| | | 53 | | // Durable recovery capability: expose the recoverable fluent builder only when the Redis |
| | | 54 | | // channel package is the selected response channel. Plain IAsyncResponseBuilder still works |
| | | 55 | | // and shares the same implementation, but its static type does not offer recovery callbacks. |
| | 771 | 56 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseBuilder>(provider => new RecoverableAsyncR |
| | 771 | 57 | | provider.GetRequiredService<IRecoverableAsyncResponseSubscriber>(), |
| | 771 | 58 | | provider.GetService<IWorkerTransport>(), |
| | 771 | 59 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 771 | 60 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 771 | 61 | | provider.GetService<TimeProvider>(), |
| | 771 | 62 | | // The producer-side mirror of the ingress's inbound size budget (WorkerJobTooLargeException). |
| | 771 | 63 | | provider.GetService<IOptions<AsyncResponseOptions>>()))); |
| | 771 | 64 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseBuilder>(provider => provider.GetRequiredService<IRec |
| | | 65 | | |
| | | 66 | | // The resolved default waiter timeout is declared through the marker so the startup |
| | | 67 | | // validator can require the durable-flow ledger TTL to out-live a timeout-less awaited |
| | | 68 | | // step, and the flow engine can extend a parked ledger by it — without either referencing |
| | | 69 | | // channel option types. |
| | 438 | 70 | | services.AddSingleton(provider => |
| | 438 | 71 | | { |
| | 335 | 72 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<RedisAsyncResponseOptions>>( |
| | 335 | 73 | | return new AsyncResponseChannelMarker("Redis") |
| | 335 | 74 | | { |
| | 335 | 75 | | EffectiveDefaultWaitTimeout = options.DefaultTimeout ?? options.RecoveryStateExpiry |
| | 335 | 76 | | }; |
| | 438 | 77 | | }); |
| | 438 | 78 | | return builder; |
| | | 79 | | } |
| | | 80 | | } |