From 60cd8642265c9cf778646f521254c7ee1086d70f Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 11 Apr 2019 01:10:29 +0900 Subject: [PATCH] Inject HttpClient inside lightning client instances --- BTCPayServer/BTCPayServer.csproj | 2 +- BTCPayServer/Hosting/BTCPayServerServices.cs | 1 + .../Lightning/LightningLikePaymentHandler.cs | 7 +++++-- .../Payments/Lightning/LightningListener.cs | 17 +++++++++++++---- .../LightningSupportedPaymentMethod.cs | 5 ----- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index f4d1b61cf..70a06e8cf 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -34,7 +34,7 @@ - + diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 27f5a7658..419f6dbb6 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -64,6 +64,7 @@ namespace BTCPayServer.Hosting services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(o => o.GetRequiredService>().Value); services.TryAddSingleton(o => diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index 098a46829..8791b9430 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -19,13 +19,16 @@ namespace BTCPayServer.Payments.Lightning public static int LIGHTNING_TIMEOUT = 5000; NBXplorerDashboard _Dashboard; + private readonly LightningClientFactoryService _lightningClientFactory; private readonly SocketFactory _socketFactory; public LightningLikePaymentHandler( NBXplorerDashboard dashboard, + LightningClientFactoryService lightningClientFactory, SocketFactory socketFactory) { _Dashboard = dashboard; + _lightningClientFactory = lightningClientFactory; _socketFactory = socketFactory; } public override async Task CreatePaymentMethodDetails(LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, BTCPayNetwork network, object preparePaymentObject) @@ -34,7 +37,7 @@ namespace BTCPayServer.Payments.Lightning var test = GetNodeInfo(paymentMethod.PreferOnion, supportedPaymentMethod, network); var invoice = paymentMethod.ParentEntity; var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); - var client = supportedPaymentMethod.CreateClient(network); + var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network); var expiry = invoice.ExpirationTime - DateTimeOffset.UtcNow; if (expiry < TimeSpan.Zero) expiry = TimeSpan.FromSeconds(1); @@ -76,7 +79,7 @@ namespace BTCPayServer.Payments.Lightning using (var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT)) { - var client = supportedPaymentMethod.CreateClient(network); + var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network); LightningNodeInformation info = null; try { diff --git a/BTCPayServer/Payments/Lightning/LightningListener.cs b/BTCPayServer/Payments/Lightning/LightningListener.cs index 3bede145e..2f704a8c7 100644 --- a/BTCPayServer/Payments/Lightning/LightningListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningListener.cs @@ -13,6 +13,8 @@ using BTCPayServer.Lightning; using System.Collections.Concurrent; using System.Threading.Channels; using Microsoft.Extensions.Caching.Memory; +using System.Net.Http; +using BTCPayServer.Services; namespace BTCPayServer.Payments.Lightning { @@ -24,17 +26,20 @@ namespace BTCPayServer.Payments.Lightning BTCPayNetworkProvider _NetworkProvider; Channel _CheckInvoices = Channel.CreateUnbounded(); Task _CheckingInvoice; + IHttpClientFactory _HttpClientFactory; Dictionary<(string, string), LightningInstanceListener> _InstanceListeners = new Dictionary<(string, string), LightningInstanceListener>(); public LightningListener(EventAggregator aggregator, InvoiceRepository invoiceRepository, IMemoryCache memoryCache, - BTCPayNetworkProvider networkProvider) + BTCPayNetworkProvider networkProvider, + IHttpClientFactory httpClientFactory) { _Aggregator = aggregator; _InvoiceRepository = invoiceRepository; _memoryCache = memoryCache; _NetworkProvider = networkProvider; + _HttpClientFactory = httpClientFactory; } async Task CheckingInvoice(CancellationToken cancellation) @@ -50,7 +55,7 @@ namespace BTCPayServer.Payments.Lightning if (!_InstanceListeners.TryGetValue(instanceListenerKey, out var instanceListener) || !instanceListener.IsListening) { - instanceListener = instanceListener ?? new LightningInstanceListener(_InvoiceRepository, _Aggregator, listenedInvoice.SupportedPaymentMethod, listenedInvoice.Network); + instanceListener = instanceListener ?? new LightningInstanceListener(_InvoiceRepository, _Aggregator, listenedInvoice.SupportedPaymentMethod, _HttpClientFactory, listenedInvoice.Network); var status = await instanceListener.PollPayment(listenedInvoice, cancellation); if (status is null || status is LightningInvoiceStatus.Paid || @@ -201,16 +206,19 @@ namespace BTCPayServer.Payments.Lightning private readonly InvoiceRepository invoiceRepository; private readonly EventAggregator _eventAggregator; private readonly BTCPayNetwork network; + private readonly LightningClientFactoryService _lightningClientFactory; public LightningInstanceListener(InvoiceRepository invoiceRepository, EventAggregator eventAggregator, LightningSupportedPaymentMethod supportedPaymentMethod, + LightningClientFactoryService lightningClientFactory, BTCPayNetwork network) { this.supportedPaymentMethod = supportedPaymentMethod; this.invoiceRepository = invoiceRepository; _eventAggregator = eventAggregator; this.network = network; + _lightningClientFactory = lightningClientFactory; } internal bool AddListenedInvoice(ListenedInvoice invoice) { @@ -219,7 +227,7 @@ namespace BTCPayServer.Payments.Lightning internal async Task PollPayment(ListenedInvoice listenedInvoice, CancellationToken cancellation) { - var client = supportedPaymentMethod.CreateClient(network); + var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network); LightningInvoice lightningInvoice = await client.GetInvoice(listenedInvoice.PaymentMethodDetails.InvoiceId); if (lightningInvoice?.Status is LightningInvoiceStatus.Paid && await AddPayment(lightningInvoice, listenedInvoice.InvoiceId)) @@ -245,7 +253,7 @@ namespace BTCPayServer.Payments.Lightning Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningUrl().BaseUri}"); try { - var lightningClient = supportedPaymentMethod.CreateClient(network); + var lightningClient = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network); using (var session = await lightningClient.Listen(cancellation)) { // Just in case the payment arrived after our last poll but before we listened. @@ -290,6 +298,7 @@ namespace BTCPayServer.Payments.Lightning if (_ListenedInvoices.IsEmpty) Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): No more invoice to listen on {supportedPaymentMethod.GetLightningUrl().BaseUri}, releasing the connection."); } + public DateTimeOffset? LastFullPoll { get; set; } internal async Task PollAllListenedInvoices(CancellationToken cancellation) diff --git a/BTCPayServer/Payments/Lightning/LightningSupportedPaymentMethod.cs b/BTCPayServer/Payments/Lightning/LightningSupportedPaymentMethod.cs index ff3c78656..625666942 100644 --- a/BTCPayServer/Payments/Lightning/LightningSupportedPaymentMethod.cs +++ b/BTCPayServer/Payments/Lightning/LightningSupportedPaymentMethod.cs @@ -60,10 +60,5 @@ namespace BTCPayServer.Payments.Lightning LightningChargeUrl = null; #pragma warning restore CS0618 // Type or member is obsolete } - - public ILightningClient CreateClient(BTCPayNetwork network) - { - return LightningClientFactory.CreateClient(this.GetLightningUrl(), network.NBitcoinNetwork); - } } }