Inject HttpClient inside lightning client instances

This commit is contained in:
nicolas.dorier 2019-04-11 01:10:29 +09:00
parent 71cf02915e
commit 60cd864226
5 changed files with 20 additions and 12 deletions

View file

@ -34,7 +34,7 @@
<EmbeddedResource Include="Currencies.txt" /> <EmbeddedResource Include="Currencies.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BTCPayServer.Lightning.All" Version="1.1.0.15" /> <PackageReference Include="BTCPayServer.Lightning.All" Version="1.1.0.16" />
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" /> <PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
<PackageReference Include="BundlerMinifier.Core" Version="2.9.406" /> <PackageReference Include="BundlerMinifier.Core" Version="2.9.406" />
<PackageReference Include="BundlerMinifier.TagHelpers" Version="2.9.406" /> <PackageReference Include="BundlerMinifier.TagHelpers" Version="2.9.406" />

View file

@ -64,6 +64,7 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<SettingsRepository>(); services.TryAddSingleton<SettingsRepository>();
services.TryAddSingleton<TorServices>(); services.TryAddSingleton<TorServices>();
services.TryAddSingleton<SocketFactory>(); services.TryAddSingleton<SocketFactory>();
services.TryAddSingleton<LightningClientFactoryService>();
services.TryAddSingleton<InvoicePaymentNotification>(); services.TryAddSingleton<InvoicePaymentNotification>();
services.TryAddSingleton<BTCPayServerOptions>(o => o.GetRequiredService<IOptions<BTCPayServerOptions>>().Value); services.TryAddSingleton<BTCPayServerOptions>(o => o.GetRequiredService<IOptions<BTCPayServerOptions>>().Value);
services.TryAddSingleton<InvoiceRepository>(o => services.TryAddSingleton<InvoiceRepository>(o =>

View file

@ -19,13 +19,16 @@ namespace BTCPayServer.Payments.Lightning
public static int LIGHTNING_TIMEOUT = 5000; public static int LIGHTNING_TIMEOUT = 5000;
NBXplorerDashboard _Dashboard; NBXplorerDashboard _Dashboard;
private readonly LightningClientFactoryService _lightningClientFactory;
private readonly SocketFactory _socketFactory; private readonly SocketFactory _socketFactory;
public LightningLikePaymentHandler( public LightningLikePaymentHandler(
NBXplorerDashboard dashboard, NBXplorerDashboard dashboard,
LightningClientFactoryService lightningClientFactory,
SocketFactory socketFactory) SocketFactory socketFactory)
{ {
_Dashboard = dashboard; _Dashboard = dashboard;
_lightningClientFactory = lightningClientFactory;
_socketFactory = socketFactory; _socketFactory = socketFactory;
} }
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, BTCPayNetwork network, object preparePaymentObject) public override async Task<IPaymentMethodDetails> 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 test = GetNodeInfo(paymentMethod.PreferOnion, supportedPaymentMethod, network);
var invoice = paymentMethod.ParentEntity; var invoice = paymentMethod.ParentEntity;
var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); 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; var expiry = invoice.ExpirationTime - DateTimeOffset.UtcNow;
if (expiry < TimeSpan.Zero) if (expiry < TimeSpan.Zero)
expiry = TimeSpan.FromSeconds(1); expiry = TimeSpan.FromSeconds(1);
@ -76,7 +79,7 @@ namespace BTCPayServer.Payments.Lightning
using (var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT)) using (var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT))
{ {
var client = supportedPaymentMethod.CreateClient(network); var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network);
LightningNodeInformation info = null; LightningNodeInformation info = null;
try try
{ {

View file

@ -13,6 +13,8 @@ using BTCPayServer.Lightning;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading.Channels; using System.Threading.Channels;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System.Net.Http;
using BTCPayServer.Services;
namespace BTCPayServer.Payments.Lightning namespace BTCPayServer.Payments.Lightning
{ {
@ -24,17 +26,20 @@ namespace BTCPayServer.Payments.Lightning
BTCPayNetworkProvider _NetworkProvider; BTCPayNetworkProvider _NetworkProvider;
Channel<string> _CheckInvoices = Channel.CreateUnbounded<string>(); Channel<string> _CheckInvoices = Channel.CreateUnbounded<string>();
Task _CheckingInvoice; Task _CheckingInvoice;
IHttpClientFactory _HttpClientFactory;
Dictionary<(string, string), LightningInstanceListener> _InstanceListeners = new Dictionary<(string, string), LightningInstanceListener>(); Dictionary<(string, string), LightningInstanceListener> _InstanceListeners = new Dictionary<(string, string), LightningInstanceListener>();
public LightningListener(EventAggregator aggregator, public LightningListener(EventAggregator aggregator,
InvoiceRepository invoiceRepository, InvoiceRepository invoiceRepository,
IMemoryCache memoryCache, IMemoryCache memoryCache,
BTCPayNetworkProvider networkProvider) BTCPayNetworkProvider networkProvider,
IHttpClientFactory httpClientFactory)
{ {
_Aggregator = aggregator; _Aggregator = aggregator;
_InvoiceRepository = invoiceRepository; _InvoiceRepository = invoiceRepository;
_memoryCache = memoryCache; _memoryCache = memoryCache;
_NetworkProvider = networkProvider; _NetworkProvider = networkProvider;
_HttpClientFactory = httpClientFactory;
} }
async Task CheckingInvoice(CancellationToken cancellation) async Task CheckingInvoice(CancellationToken cancellation)
@ -50,7 +55,7 @@ namespace BTCPayServer.Payments.Lightning
if (!_InstanceListeners.TryGetValue(instanceListenerKey, out var instanceListener) || if (!_InstanceListeners.TryGetValue(instanceListenerKey, out var instanceListener) ||
!instanceListener.IsListening) !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); var status = await instanceListener.PollPayment(listenedInvoice, cancellation);
if (status is null || if (status is null ||
status is LightningInvoiceStatus.Paid || status is LightningInvoiceStatus.Paid ||
@ -201,16 +206,19 @@ namespace BTCPayServer.Payments.Lightning
private readonly InvoiceRepository invoiceRepository; private readonly InvoiceRepository invoiceRepository;
private readonly EventAggregator _eventAggregator; private readonly EventAggregator _eventAggregator;
private readonly BTCPayNetwork network; private readonly BTCPayNetwork network;
private readonly LightningClientFactoryService _lightningClientFactory;
public LightningInstanceListener(InvoiceRepository invoiceRepository, public LightningInstanceListener(InvoiceRepository invoiceRepository,
EventAggregator eventAggregator, EventAggregator eventAggregator,
LightningSupportedPaymentMethod supportedPaymentMethod, LightningSupportedPaymentMethod supportedPaymentMethod,
LightningClientFactoryService lightningClientFactory,
BTCPayNetwork network) BTCPayNetwork network)
{ {
this.supportedPaymentMethod = supportedPaymentMethod; this.supportedPaymentMethod = supportedPaymentMethod;
this.invoiceRepository = invoiceRepository; this.invoiceRepository = invoiceRepository;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
this.network = network; this.network = network;
_lightningClientFactory = lightningClientFactory;
} }
internal bool AddListenedInvoice(ListenedInvoice invoice) internal bool AddListenedInvoice(ListenedInvoice invoice)
{ {
@ -219,7 +227,7 @@ namespace BTCPayServer.Payments.Lightning
internal async Task<LightningInvoiceStatus?> PollPayment(ListenedInvoice listenedInvoice, CancellationToken cancellation) internal async Task<LightningInvoiceStatus?> 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); LightningInvoice lightningInvoice = await client.GetInvoice(listenedInvoice.PaymentMethodDetails.InvoiceId);
if (lightningInvoice?.Status is LightningInvoiceStatus.Paid && if (lightningInvoice?.Status is LightningInvoiceStatus.Paid &&
await AddPayment(lightningInvoice, listenedInvoice.InvoiceId)) await AddPayment(lightningInvoice, listenedInvoice.InvoiceId))
@ -245,7 +253,7 @@ namespace BTCPayServer.Payments.Lightning
Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningUrl().BaseUri}"); Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningUrl().BaseUri}");
try try
{ {
var lightningClient = supportedPaymentMethod.CreateClient(network); var lightningClient = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), network);
using (var session = await lightningClient.Listen(cancellation)) using (var session = await lightningClient.Listen(cancellation))
{ {
// Just in case the payment arrived after our last poll but before we listened. // 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) if (_ListenedInvoices.IsEmpty)
Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): No more invoice to listen on {supportedPaymentMethod.GetLightningUrl().BaseUri}, releasing the connection."); Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): No more invoice to listen on {supportedPaymentMethod.GetLightningUrl().BaseUri}, releasing the connection.");
} }
public DateTimeOffset? LastFullPoll { get; set; } public DateTimeOffset? LastFullPoll { get; set; }
internal async Task PollAllListenedInvoices(CancellationToken cancellation) internal async Task PollAllListenedInvoices(CancellationToken cancellation)

View file

@ -60,10 +60,5 @@ namespace BTCPayServer.Payments.Lightning
LightningChargeUrl = null; LightningChargeUrl = null;
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
} }
public ILightningClient CreateClient(BTCPayNetwork network)
{
return LightningClientFactory.CreateClient(this.GetLightningUrl(), network.NBitcoinNetwork);
}
} }
} }