diff --git a/src/pdns-dhcp/Kea/IKeaFactory.cs b/src/pdns-dhcp/Kea/IKeaFactory.cs index 55f1eb5..4f9953f 100644 --- a/src/pdns-dhcp/Kea/IKeaFactory.cs +++ b/src/pdns-dhcp/Kea/IKeaFactory.cs @@ -1,10 +1,8 @@ using pdns_dhcp.Options; -using Stl.Interception; - namespace pdns_dhcp.Kea; -public interface IKeaFactory : IRequiresFullProxy +public interface IKeaFactory { KeaDhcp4LeaseHandler CreateHandler4(); diff --git a/src/pdns-dhcp/Kea/KeaDhcpLease.cs b/src/pdns-dhcp/Kea/KeaDhcpLease.cs index e18c98d..4254440 100644 --- a/src/pdns-dhcp/Kea/KeaDhcpLease.cs +++ b/src/pdns-dhcp/Kea/KeaDhcpLease.cs @@ -3,7 +3,7 @@ using System.Globalization; using DotNext.Buffers; -using Microsoft.Toolkit.HighPerformance.Buffers; +using CommunityToolkit.HighPerformance.Buffers; namespace pdns_dhcp.Kea; diff --git a/src/pdns-dhcp/Kea/KeaFactoryServices.cs b/src/pdns-dhcp/Kea/KeaFactoryServices.cs new file mode 100644 index 0000000..aa50cf3 --- /dev/null +++ b/src/pdns-dhcp/Kea/KeaFactoryServices.cs @@ -0,0 +1,39 @@ +using Microsoft.Extensions.DependencyInjection; + +using pdns_dhcp.Options; + +namespace pdns_dhcp.Kea; + +public static class KeaFactoryServices +{ + public static IServiceCollection AddKeaFactory(this IServiceCollection services) + { + services.AddTransient(); + return services; + } + + private class KeaFactory(IServiceProvider services) : IKeaFactory + { + private ObjectFactory? _cachedCreateHandler4; + private ObjectFactory? _cachedCreateHandler6; + private ObjectFactory? _cachedCreateWatcher; + + KeaDhcp4LeaseHandler IKeaFactory.CreateHandler4() + { + _cachedCreateHandler4 ??= ActivatorUtilities.CreateFactory([]); + return _cachedCreateHandler4(services, null); + } + + KeaDhcp6LeaseHandler IKeaFactory.CreateHandler6() + { + _cachedCreateHandler6 ??= ActivatorUtilities.CreateFactory([]); + return _cachedCreateHandler6(services, null); + } + + KeaDhcpLeaseWatcher IKeaFactory.CreateWatcher(IKeaDhcpLeaseHandler handler, KeaDhcpServerOptions options) + { + _cachedCreateWatcher ??= ActivatorUtilities.CreateFactory([typeof(IKeaDhcpLeaseHandler), typeof(KeaDhcpServerOptions)]); + return _cachedCreateWatcher(services, [handler, options]); + } + } +} diff --git a/src/pdns-dhcp/PowerDns/PowerDnsHandler.cs b/src/pdns-dhcp/PowerDns/PowerDnsHandler.cs index 09be29e..e2ee4c6 100644 --- a/src/pdns-dhcp/PowerDns/PowerDnsHandler.cs +++ b/src/pdns-dhcp/PowerDns/PowerDnsHandler.cs @@ -1,11 +1,13 @@ using System.Buffers; +using System.IO.Pipelines; using System.Net.Sockets; using System.Text.Json; +using CommunityToolkit.HighPerformance; +using CommunityToolkit.HighPerformance.Buffers; + using Microsoft.AspNetCore.Connections; using Microsoft.Extensions.Logging; -using Microsoft.Toolkit.HighPerformance; -using Microsoft.Toolkit.HighPerformance.Buffers; using pdns_dhcp.Dns; @@ -31,8 +33,7 @@ public class PowerDnsHandler : ConnectionHandler using var writer = connection.Transport.Output.AsStream(); while (!connection.ConnectionClosed.IsCancellationRequested) { - var read = await input.ReadAsync(connection.ConnectionClosed).ConfigureAwait(false); - if (read.IsCanceled) + if (await ReadAsync(input, connection.ConnectionClosed) is not { IsCanceled: false } read) { return; } @@ -61,6 +62,18 @@ public class PowerDnsHandler : ConnectionHandler input.AdvanceTo(read.Buffer.End); } + static async ValueTask ReadAsync(PipeReader reader, CancellationToken cancellationToken) + { + try + { + return await reader.ReadAsync(cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + return null; + } + } + static bool ConsumeJson(ArrayPoolBufferWriter inflight, ArrayPoolBufferWriter json, ref JsonReaderState state) { bool final = false; diff --git a/src/pdns-dhcp/Program.cs b/src/pdns-dhcp/Program.cs index 91fb2c1..4d12cff 100644 --- a/src/pdns-dhcp/Program.cs +++ b/src/pdns-dhcp/Program.cs @@ -17,8 +17,6 @@ using pdns_dhcp.Options; using pdns_dhcp.PowerDns; using pdns_dhcp.Services; -using Stl.Interception; - var builder = WebApplication.CreateBuilder(args); builder.Services.AddSystemd(); @@ -31,8 +29,8 @@ builder.Services.AddHostedService(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddTypedFactory(); -builder.Services.AddTypedFactory(); +builder.Services.AddDhcpWatcherFactory(); +builder.Services.AddKeaFactory(); builder.Services.Configure(options => { diff --git a/src/pdns-dhcp/Services/DhcpWatcherFactoryServices.cs b/src/pdns-dhcp/Services/DhcpWatcherFactoryServices.cs new file mode 100644 index 0000000..2ecc258 --- /dev/null +++ b/src/pdns-dhcp/Services/DhcpWatcherFactoryServices.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.DependencyInjection; + +using pdns_dhcp.Kea; +using pdns_dhcp.Options; + +namespace pdns_dhcp.Services; + +public static class DhcpWatcherFactoryServices +{ + public static IServiceCollection AddDhcpWatcherFactory(this IServiceCollection services) + { + services.AddTransient(); + return services; + } + + private class DhcpWatcherFactory(IServiceProvider services) : IDhcpWatcherFactory + { + private ObjectFactory? _cachedKeaService; + + KeaService IDhcpWatcherFactory.KeaService(KeaDhcpOptions options) + { + _cachedKeaService ??= ActivatorUtilities.CreateFactory([typeof(KeaDhcpOptions)]); + return _cachedKeaService(services, [options]); + } + } +} diff --git a/src/pdns-dhcp/Services/IDhcpWatcherFactory.cs b/src/pdns-dhcp/Services/IDhcpWatcherFactory.cs index cdb644c..4ff6612 100644 --- a/src/pdns-dhcp/Services/IDhcpWatcherFactory.cs +++ b/src/pdns-dhcp/Services/IDhcpWatcherFactory.cs @@ -1,11 +1,9 @@ using pdns_dhcp.Kea; using pdns_dhcp.Options; -using Stl.Interception; - namespace pdns_dhcp.Services; -public interface IDhcpWatcherFactory : IRequiresFullProxy +public interface IDhcpWatcherFactory { KeaService KeaService(KeaDhcpOptions options); } diff --git a/src/pdns-dhcp/System/IO/Paths.cs b/src/pdns-dhcp/System/IO/Paths.cs index 9ea36be..ebb2b50 100644 --- a/src/pdns-dhcp/System/IO/Paths.cs +++ b/src/pdns-dhcp/System/IO/Paths.cs @@ -14,7 +14,7 @@ using System.Buffers; -using Microsoft.Toolkit.HighPerformance.Buffers; +using CommunityToolkit.HighPerformance.Buffers; namespace System.IO; diff --git a/src/pdns-dhcp/pdns-dhcp.csproj b/src/pdns-dhcp/pdns-dhcp.csproj index 9b8e02d..438e7a8 100644 --- a/src/pdns-dhcp/pdns-dhcp.csproj +++ b/src/pdns-dhcp/pdns-dhcp.csproj @@ -14,13 +14,12 @@ + - -