This commit is contained in:
Jöran Malek 2023-11-28 18:15:27 +01:00
parent 9b54818d54
commit 9f548cc482
11 changed files with 52 additions and 37 deletions

View file

@ -0,0 +1,8 @@
using nietras.SeparatedValues;
namespace pdns_dhcp.Kea;
public interface IKeaDhcpLeaseHandler
{
void Handle(in SepReader.Row row);
}

View file

@ -0,0 +1,8 @@
using nietras.SeparatedValues;
namespace pdns_dhcp.Kea;
public class KeaDhcp4LeaseHandler : IKeaDhcpLeaseHandler
{
public void Handle(in SepReader.Row row) { }
}

View file

@ -1,7 +0,0 @@
using pdns_dhcp.Options;
namespace pdns_dhcp.Kea;
public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
{
}

View file

@ -0,0 +1,10 @@
using nietras.SeparatedValues;
namespace pdns_dhcp.Kea;
public class KeaDhcp6LeaseHandler : IKeaDhcpLeaseHandler
{
public void Handle(in SepReader.Row row)
{
}
}

View file

@ -1,7 +0,0 @@
using pdns_dhcp.Options;
namespace pdns_dhcp.Kea;
public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
{
}

View file

@ -1,6 +0,0 @@
namespace pdns_dhcp.Kea;
public class KeaDhcpLeaseFileReader
{
}

View file

@ -11,7 +11,8 @@ using pdns_dhcp.Options;
namespace pdns_dhcp.Kea; namespace pdns_dhcp.Kea;
public abstract class KeaDhcpLeaseWatcher : IHostedService public sealed class KeaDhcpLeaseWatcher<T> : IHostedService
where T : IKeaDhcpLeaseHandler
{ {
private static readonly FileStreamOptions LeaseFileStreamOptions = new() private static readonly FileStreamOptions LeaseFileStreamOptions = new()
{ {
@ -23,17 +24,20 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
private readonly Decoder _decoder; private readonly Decoder _decoder;
private readonly FileSystemWatcher _fsw; private readonly FileSystemWatcher _fsw;
private readonly T _handler;
private readonly string _leaseFile; private readonly string _leaseFile;
private readonly Pipe _pipe; private readonly Pipe _pipe;
private Channel<FileSystemEventArgs>? _eventChannel; private Channel<FileSystemEventArgs>? _eventChannel;
private Task? _executeTask; private Task? _executeTask;
private CancellationTokenSource? _stoppingCts; private CancellationTokenSource? _stoppingCts;
protected KeaDhcpServerOptions Options { get; } private KeaDhcpServerOptions Options { get; }
protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options) public KeaDhcpLeaseWatcher(KeaDhcpServerOptions options, T handler)
{ {
Options = options; Options = options;
_handler = handler;
var leases = options.Leases.AsSpan(); var leases = options.Leases.AsSpan();
if (leases.IsWhiteSpace()) if (leases.IsWhiteSpace())
{ {
@ -176,6 +180,7 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
{ {
CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed); CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed);
writer.Advance(read); writer.Advance(read);
await writer.FlushAsync(stoppingToken);
} }
else else
{ {
@ -218,24 +223,23 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
private void OnLeaseChanged(object sender, FileSystemEventArgs e) private void OnLeaseChanged(object sender, FileSystemEventArgs e)
{ {
var writer = _eventChannel?.Writer; if (_eventChannel?.Writer is not { } writer)
if (writer?.TryWrite(e) != false)
{ {
return; return;
} }
#pragma warning disable CA2012 // Task is awaited immediately. #pragma warning disable CA2012
if (writer.WriteAsync(e) is { IsCompleted: false } task) var task = writer.WriteAsync(e, CancellationToken.None);
{
try
{
task.GetAwaiter().GetResult();
}
catch { }
}
#pragma warning restore #pragma warning restore
if (task.IsCompleted)
{
return;
}
task.GetAwaiter().GetResult();
} }
private void OnLeaseError(object sender, ErrorEventArgs e) private void OnLeaseError(object sender, ErrorEventArgs e)
{ {
_eventChannel?.Writer.Complete(e.GetException()); _eventChannel?.Writer.Complete(e.GetException());

View file

@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using pdns_dhcp.Kea;
using pdns_dhcp.Options; using pdns_dhcp.Options;
using pdns_dhcp.PowerDns; using pdns_dhcp.PowerDns;
using pdns_dhcp.Services; using pdns_dhcp.Services;
@ -18,4 +19,7 @@ builder.Services.AddHostedService<PowerDnsBackend>();
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>(); builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
builder.Services.AddTypedFactory<IPowerDnsFactory>(); builder.Services.AddTypedFactory<IPowerDnsFactory>();
builder.Services.AddTransient<KeaDhcp4LeaseHandler>();
builder.Services.AddTransient<KeaDhcp6LeaseHandler>();
builder.Build().Run(); builder.Build().Run();

View file

@ -3,6 +3,7 @@ using System.Collections.Immutable;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using pdns_dhcp.Kea;
using pdns_dhcp.Options; using pdns_dhcp.Options;
namespace pdns_dhcp.Services; namespace pdns_dhcp.Services;

View file

@ -7,7 +7,7 @@ namespace pdns_dhcp.Services;
public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy
{ {
KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options); KeaDhcpLeaseWatcher<KeaDhcp4LeaseHandler> KeaDhcp4Watcher(KeaDhcpServerOptions options);
KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options); KeaDhcpLeaseWatcher<KeaDhcp6LeaseHandler> KeaDhcp6Watcher(KeaDhcpServerOptions options);
} }

View file

@ -5,7 +5,7 @@
"Leases": "../../ext/kea/dhcp4.leases" "Leases": "../../ext/kea/dhcp4.leases"
}, },
"DHcp6": { "DHcp6": {
"Leases": "../..ext/kea/dhcp4.leases" "Leases": "../../ext/kea/dhcp4.leases"
} }
} }
}, },