| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Channels.NATS; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using NATS.Client.Core; |
| | | 6 | | using NATS.Net; |
| | | 7 | | |
| | | 8 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// DI registration for the NATS-backed AsyncResponse channel package. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class NatsAsyncResponseChannelServiceCollectionExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers NATS Core request/reply as the response channel — behind |
| | | 17 | | /// <see cref="IAsyncResponsePublisher"/>, <see cref="IAsyncResponseSubscriber"/>, and |
| | | 18 | | /// <see cref="IRecoverableAsyncResponseSubscriber"/> — together with the durable NATS JetStream |
| | | 19 | | /// Key-Value recovery-state store, so a response that arrives after the waiter died (e.g. a |
| | | 20 | | /// redeploy) can still resume or fail the owning flow. It also registers |
| | | 21 | | /// <see cref="IRecoverableAsyncResponseBuilder"/> for fluent durable recovery flows. The same |
| | | 22 | | /// channel instance serves the engine's recovery-state scanner and active-subscriber probe, so the |
| | | 23 | | /// watchdog works against NATS automatically. |
| | | 24 | | /// <para> |
| | | 25 | | /// Requires a <c>NATS.Client.Core.INatsConnection</c> singleton registered by the host — for |
| | | 26 | | /// example via <c>builder.Services.AddNatsClient(...)</c> from |
| | | 27 | | /// <c>NATS.Extensions.Microsoft.DependencyInjection</c>. Reuse the application's existing |
| | | 28 | | /// connection; don't create a second one. The recovery store requires JetStream to be enabled on |
| | | 29 | | /// the NATS server. Publisher/subscriber resolve to one shared instance: per-interface instances |
| | | 30 | | /// would split the channel's internal state. |
| | | 31 | | /// </para> |
| | | 32 | | /// </summary> |
| | | 33 | | public static AsyncResponseRegistrationBuilder WithNatsChannel( |
| | | 34 | | this AsyncResponseRegistrationBuilder builder, |
| | | 35 | | Action<NatsAsyncResponseChannelOptions>? configure = null) |
| | | 36 | | { |
| | 373 | 37 | | var services = builder.Services; |
| | 373 | 38 | | services.AddOptions(); |
| | 373 | 39 | | if (configure is not null) |
| | 369 | 40 | | services.Configure(configure); |
| | | 41 | | |
| | | 42 | | // NATS JetStream Key-Value client adapter (lazily creates the recovery bucket on first use). |
| | 742 | 43 | | services.TryAddSingleton<INatsKvStore>(provider => new NatsKvStoreAdapter( |
| | 742 | 44 | | provider.GetRequiredService<INatsConnection>().CreateKeyValueStoreContext(), |
| | 742 | 45 | | provider.GetRequiredService<IOptions<NatsAsyncResponseChannelOptions>>().Value)); |
| | | 46 | | |
| | | 47 | | // Durable recovery store; also the watchdog's recovery-state scanner. |
| | 373 | 48 | | services.TryAddSingleton<NatsRecoveryStateStore>(); |
| | 742 | 49 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateStore>(provider => provider.GetRequiredService<NatsRe |
| | 706 | 50 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateScanner>(provider => provider.GetRequiredService<Nats |
| | | 51 | | |
| | | 52 | | // NATS Core request/reply client adapter (raw extension calls isolated in NatsRawRequester). |
| | 742 | 53 | | services.TryAddSingleton<INatsResponseChannelClient>(provider => new NatsResponseChannelClient( |
| | 742 | 54 | | new NatsRawRequester(provider.GetRequiredService<INatsConnection>()))); |
| | | 55 | | |
| | | 56 | | // NATS response channel: publisher + subscriber + the watchdog's liveness probe, all one shared instance. |
| | 373 | 57 | | services.TryAddSingleton<NatsAsyncResponseChannel>(); |
| | 731 | 58 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponsePublisher>(provider => provider.GetRequiredService<Na |
| | 685 | 59 | | services.Replace(ServiceDescriptor.Singleton<IRawAsyncResponsePublisher>(provider => provider.GetRequiredService |
| | 735 | 60 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseSubscriber>(provider => provider.GetRequiredService<N |
| | 706 | 61 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseSubscriber>(provider => provider.GetRequir |
| | 710 | 62 | | services.Replace(ServiceDescriptor.Singleton<IActiveSubscriberProbe>(provider => provider.GetRequiredService<Nat |
| | | 63 | | |
| | | 64 | | // Durable recovery capability: expose the recoverable fluent builder only when the NATS channel |
| | | 65 | | // package is the selected response channel. Plain IAsyncResponseBuilder still works and shares |
| | | 66 | | // the same implementation, but its static type does not offer recovery callbacks. |
| | 706 | 67 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseBuilder>(provider => new RecoverableAsyncR |
| | 706 | 68 | | provider.GetRequiredService<IRecoverableAsyncResponseSubscriber>(), |
| | 706 | 69 | | provider.GetService<IWorkerTransport>(), |
| | 706 | 70 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 706 | 71 | | provider.GetRequiredService<AsyncResponseContextPropagation>(), |
| | 706 | 72 | | provider.GetService<TimeProvider>(), |
| | 706 | 73 | | // The producer-side mirror of the ingress's inbound size budget (WorkerJobTooLargeException). |
| | 706 | 74 | | provider.GetService<IOptions<AsyncResponseOptions>>()))); |
| | 706 | 75 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseBuilder>(provider => provider.GetRequiredService<IRec |
| | | 76 | | |
| | | 77 | | // The resolved default waiter timeout is declared through the marker so the startup |
| | | 78 | | // validator can require the durable-flow ledger TTL to out-live a timeout-less awaited |
| | | 79 | | // step, and the flow engine can extend a parked ledger by it — without either referencing |
| | | 80 | | // channel option types. |
| | 373 | 81 | | services.AddSingleton(provider => |
| | 373 | 82 | | { |
| | 335 | 83 | | var options = provider.GetRequiredService<IOptions<NatsAsyncResponseChannelOptions>>().Value; |
| | 335 | 84 | | return new AsyncResponseChannelMarker(NatsAsyncResponseChannelOptions.ChannelName) |
| | 335 | 85 | | { |
| | 335 | 86 | | EffectiveDefaultWaitTimeout = options.DefaultTimeout ?? options.RecoveryStateExpiry |
| | 335 | 87 | | }; |
| | 373 | 88 | | }); |
| | 373 | 89 | | return builder; |
| | | 90 | | } |
| | | 91 | | } |