Support trimming

This commit is contained in:
Jöran Malek 2024-01-07 01:33:49 +01:00
parent 6dbcb38ce2
commit 526beac55e
9 changed files with 89 additions and 18 deletions

View file

@ -1,10 +1,8 @@
using pdns_dhcp.Options; using pdns_dhcp.Options;
using Stl.Interception;
namespace pdns_dhcp.Kea; namespace pdns_dhcp.Kea;
public interface IKeaFactory : IRequiresFullProxy public interface IKeaFactory
{ {
KeaDhcp4LeaseHandler CreateHandler4(); KeaDhcp4LeaseHandler CreateHandler4();

View file

@ -3,7 +3,7 @@ using System.Globalization;
using DotNext.Buffers; using DotNext.Buffers;
using Microsoft.Toolkit.HighPerformance.Buffers; using CommunityToolkit.HighPerformance.Buffers;
namespace pdns_dhcp.Kea; namespace pdns_dhcp.Kea;

View file

@ -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<IKeaFactory, KeaFactory>();
return services;
}
private class KeaFactory(IServiceProvider services) : IKeaFactory
{
private ObjectFactory<KeaDhcp4LeaseHandler>? _cachedCreateHandler4;
private ObjectFactory<KeaDhcp6LeaseHandler>? _cachedCreateHandler6;
private ObjectFactory<KeaDhcpLeaseWatcher>? _cachedCreateWatcher;
KeaDhcp4LeaseHandler IKeaFactory.CreateHandler4()
{
_cachedCreateHandler4 ??= ActivatorUtilities.CreateFactory<KeaDhcp4LeaseHandler>([]);
return _cachedCreateHandler4(services, null);
}
KeaDhcp6LeaseHandler IKeaFactory.CreateHandler6()
{
_cachedCreateHandler6 ??= ActivatorUtilities.CreateFactory<KeaDhcp6LeaseHandler>([]);
return _cachedCreateHandler6(services, null);
}
KeaDhcpLeaseWatcher IKeaFactory.CreateWatcher(IKeaDhcpLeaseHandler handler, KeaDhcpServerOptions options)
{
_cachedCreateWatcher ??= ActivatorUtilities.CreateFactory<KeaDhcpLeaseWatcher>([typeof(IKeaDhcpLeaseHandler), typeof(KeaDhcpServerOptions)]);
return _cachedCreateWatcher(services, [handler, options]);
}
}
}

View file

@ -1,11 +1,13 @@
using System.Buffers; using System.Buffers;
using System.IO.Pipelines;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text.Json; using System.Text.Json;
using CommunityToolkit.HighPerformance;
using CommunityToolkit.HighPerformance.Buffers;
using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Toolkit.HighPerformance;
using Microsoft.Toolkit.HighPerformance.Buffers;
using pdns_dhcp.Dns; using pdns_dhcp.Dns;
@ -31,8 +33,7 @@ public class PowerDnsHandler : ConnectionHandler
using var writer = connection.Transport.Output.AsStream(); using var writer = connection.Transport.Output.AsStream();
while (!connection.ConnectionClosed.IsCancellationRequested) while (!connection.ConnectionClosed.IsCancellationRequested)
{ {
var read = await input.ReadAsync(connection.ConnectionClosed).ConfigureAwait(false); if (await ReadAsync(input, connection.ConnectionClosed) is not { IsCanceled: false } read)
if (read.IsCanceled)
{ {
return; return;
} }
@ -61,6 +62,18 @@ public class PowerDnsHandler : ConnectionHandler
input.AdvanceTo(read.Buffer.End); input.AdvanceTo(read.Buffer.End);
} }
static async ValueTask<ReadResult?> ReadAsync(PipeReader reader, CancellationToken cancellationToken)
{
try
{
return await reader.ReadAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
return null;
}
}
static bool ConsumeJson(ArrayPoolBufferWriter<byte> inflight, ArrayPoolBufferWriter<byte> json, ref JsonReaderState state) static bool ConsumeJson(ArrayPoolBufferWriter<byte> inflight, ArrayPoolBufferWriter<byte> json, ref JsonReaderState state)
{ {
bool final = false; bool final = false;

View file

@ -17,8 +17,6 @@ using pdns_dhcp.Options;
using pdns_dhcp.PowerDns; using pdns_dhcp.PowerDns;
using pdns_dhcp.Services; using pdns_dhcp.Services;
using Stl.Interception;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSystemd(); builder.Services.AddSystemd();
@ -31,8 +29,8 @@ builder.Services.AddHostedService<DhcpQueueWorker>();
builder.Services.AddSingleton<DhcpLeaseQueue>(); builder.Services.AddSingleton<DhcpLeaseQueue>();
builder.Services.AddSingleton<DnsRepository>(); builder.Services.AddSingleton<DnsRepository>();
builder.Services.AddTypedFactory<IDhcpWatcherFactory>(); builder.Services.AddDhcpWatcherFactory();
builder.Services.AddTypedFactory<IKeaFactory>(); builder.Services.AddKeaFactory();
builder.Services.Configure<SocketTransportOptions>(options => builder.Services.Configure<SocketTransportOptions>(options =>
{ {

View file

@ -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<IDhcpWatcherFactory, DhcpWatcherFactory>();
return services;
}
private class DhcpWatcherFactory(IServiceProvider services) : IDhcpWatcherFactory
{
private ObjectFactory<KeaService>? _cachedKeaService;
KeaService IDhcpWatcherFactory.KeaService(KeaDhcpOptions options)
{
_cachedKeaService ??= ActivatorUtilities.CreateFactory<KeaService>([typeof(KeaDhcpOptions)]);
return _cachedKeaService(services, [options]);
}
}
}

View file

@ -1,11 +1,9 @@
using pdns_dhcp.Kea; using pdns_dhcp.Kea;
using pdns_dhcp.Options; using pdns_dhcp.Options;
using Stl.Interception;
namespace pdns_dhcp.Services; namespace pdns_dhcp.Services;
public interface IDhcpWatcherFactory : IRequiresFullProxy public interface IDhcpWatcherFactory
{ {
KeaService KeaService(KeaDhcpOptions options); KeaService KeaService(KeaDhcpOptions options);
} }

View file

@ -14,7 +14,7 @@
using System.Buffers; using System.Buffers;
using Microsoft.Toolkit.HighPerformance.Buffers; using CommunityToolkit.HighPerformance.Buffers;
namespace System.IO; namespace System.IO;

View file

@ -14,13 +14,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="DotNext.Threading" Version="4.15.2" /> <PackageReference Include="DotNext.Threading" Version="4.15.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" PrivateAssets="all" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
<PackageReference Include="Sep" Version="0.3.0" /> <PackageReference Include="Sep" Version="0.3.0" />
<PackageReference Include="Stl.Generators" Version="6.8.11" PrivateAssets="all" />
<PackageReference Include="Stl.Interception" Version="6.8.11" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" /> <PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
</ItemGroup> </ItemGroup>