Skip to content

Commit db2d7cf

Browse files
authored
Auto detect network name server changes (#66)
* Using `NetworkChange` event to react on changing network adapters * Periodically re-run `NameServer.ResolveNameServers` to check if the configuration changed
1 parent b5166f0 commit db2d7cf

File tree

13 files changed

+300
-151
lines changed

13 files changed

+300
-151
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# DnsClient.NET
22

3-
[![Build Status](https://dev.azure.com/michaco/DnsClient/_apis/build/status/MichaCo.DnsClient.NET?branchName=dev)](https://dev.azure.com/michaco/DnsClient/_build/latest?definitionId=1&branchName=dev)
4-
[![NuGet](https://img.shields.io/nuget/v/DnsClient.svg?style=flat&label=Stable)](https://www.nuget.org/packages/DnsClient)
5-
[![MyGet](https://img.shields.io/myget/dnsclient/vpre/DnsClient.svg?style=flat&label=Pre-release)](https://www.myget.org/feed/dnsclient/package/nuget/DnsClient)
3+
[![Build Status](https://dev.azure.com/michaco/DnsClient/_apis/build/status/MichaCo.DnsClient.NET?branchName=dev&label=Build)](https://dev.azure.com/michaco/DnsClient/_build/latest?definitionId=1&branchName=dev)
4+
[![Code Coverage](https://img.shields.io/azure-devops/coverage/michaco/DnsClient/1?label=Coverage&style=flat&color=informational)](https://dev.azure.com/michaco/DnsClient/_build/latest?definitionId=1&branchName=dev)
5+
[![NuGet](https://img.shields.io/nuget/v/DnsClient?color=brightgreen&label=NuGet%20Stable)](https://www.nuget.org/packages/DnsClient)
6+
[![NuGet](https://img.shields.io/nuget/vpre/DnsClient?color=yellow&label=NuGet%20Latest)](https://www.nuget.org/packages/DnsClient)
67

78
DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups.
89

azure-pipelines-ci.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,26 @@ jobs:
4848

4949
steps:
5050
- task: DotNetCoreCLI@2
51+
displayName: 'dotnet build'
5152
inputs:
5253
command: 'build'
5354
projects: |
5455
src/**/*.csproj
5556
test/DnsClient.Tests/*.csproj
5657
arguments: '-c Release'
57-
name: 'Build'
58-
displayName: 'dotnet build'
58+
name: 'Build'
5959
- task: DotNetCoreCLI@2
60+
displayName: "dotnet test"
6061
inputs:
6162
command: 'test'
6263
projects: 'test/DnsClient.Tests/*.csproj'
63-
publishTestResults: true
64-
arguments: '-c Release --no-build --no-restore'
65-
64+
publishTestResults: true
65+
arguments: '-c Release --no-build --no-restore --collect "Code coverage"'
6666
- script: 'dotnet pack src\DnsClient\DnsClient.csproj -c Release --no-build --no-restore --version-suffix $(versionSuffix) -v normal -o $(Build.ArtifactStagingDirectory)'
6767
name: 'PackBeta'
68-
displayName: 'dotnet pack for beta'
68+
displayName: 'dotnet pack'
6969
- task: PublishBuildArtifacts@1
70+
displayName: 'publish'
7071
inputs:
7172
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
7273
artifactName: 'beta'

samples/MiniDig/PerfCommand.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace DigApp
1212
public class PerfCommand : DnsCommand
1313
{
1414
private int _clients;
15+
private int _tasks;
1516
private string _query;
1617
private int _runtime;
1718
private long _reportExcecutions = 0;
@@ -26,6 +27,8 @@ public class PerfCommand : DnsCommand
2627

2728
public CommandOption ClientsArg { get; private set; }
2829

30+
public CommandOption TasksArg { get; private set; }
31+
2932
public CommandArgument QueryArg { get; private set; }
3033

3134
public CommandOption RuntimeArg { get; private set; }
@@ -40,6 +43,7 @@ protected override void Configure()
4043
{
4144
QueryArg = App.Argument("query", "the domain query to run.", false);
4245
ClientsArg = App.Option("-c | --clients", "Number of clients to run", CommandOptionType.SingleValue);
46+
TasksArg = App.Option("--tasks", "Number of tasks each client runs simultanously (8).", CommandOptionType.SingleValue);
4347
RuntimeArg = App.Option("-r | --run", "Time in seconds to run", CommandOptionType.SingleValue);
4448
SyncArg = App.Option("--sync", "Run synchronous api", CommandOptionType.NoValue);
4549
base.Configure();
@@ -48,6 +52,17 @@ protected override void Configure()
4852
protected override async Task<int> Execute()
4953
{
5054
_clients = ClientsArg.HasValue() ? int.Parse(ClientsArg.Value()) : 10;
55+
if (_clients <= 0)
56+
{
57+
throw new ArgumentOutOfRangeException(nameof(ClientsArg));
58+
}
59+
60+
_tasks = TasksArg.HasValue() ? int.Parse(TasksArg.Value()) : 8;
61+
if (_clients <= 0)
62+
{
63+
throw new ArgumentOutOfRangeException(nameof(TasksArg));
64+
}
65+
5166
_runtime = RuntimeArg.HasValue() ? int.Parse(RuntimeArg.Value()) <= 1 ? 5 : int.Parse(RuntimeArg.Value()) : 5;
5267
_query = string.IsNullOrWhiteSpace(QueryArg.Value) ? string.Empty : QueryArg.Value;
5368
_runSync = SyncArg.HasValue();
@@ -57,7 +72,7 @@ protected override async Task<int> Execute()
5772
_lookup = GetDnsLookup(_settings);
5873
_running = true;
5974

60-
Console.WriteLine($"; <<>> Starting perf run with {_clients} clients running for {_runtime} seconds <<>>");
75+
Console.WriteLine($"; <<>> Starting perf run with {_clients} (x{_tasks}) clients running for {_runtime} seconds <<>>");
6176
Console.WriteLine($"; ({_settings.NameServers.Count} Servers, caching:{_settings.UseCache}, minttl:{_settings.MinimumCacheTimeout?.TotalMilliseconds}, maxttl:{_settings.MaximumCacheTimeout?.TotalMilliseconds})");
6277
_spinner = new Spiner();
6378
_spinner.Start();
@@ -76,7 +91,7 @@ protected override async Task<int> Execute()
7691

7792
for (var clientIndex = 0; clientIndex < _clients; clientIndex++)
7893
{
79-
tasks.Add(ExcecuteRun());
94+
tasks.Add(ExcecuteRun(numTasks: _tasks));
8095
}
8196

8297
tasks.Add(CollectPrint());
@@ -91,7 +106,7 @@ protected override async Task<int> Execute()
91106
Console.WriteLine(string.Join("-", Enumerable.Repeat("-", 50)));
92107
Console.WriteLine($";; results:\t\t");
93108
Console.WriteLine(string.Join("-", Enumerable.Repeat("-", 50)));
94-
Console.WriteLine($";; run for {elapsedSeconds}sec {_clients} clients.");
109+
Console.WriteLine($";; run for {elapsedSeconds}sec {_clients} (x{_tasks}) clients.");
95110

96111
var successPercent = _errors == 0 ? 100 : _success == 0 ? 0 : (100 - ((double)_errors / (_success) * 100));
97112
Console.WriteLine($";; {_errors:N0} errors {_success:N0} ok {successPercent:N2}% success.");
@@ -115,14 +130,25 @@ private async Task CollectPrint()
115130
_running = false;
116131
}
117132

118-
private async Task ExcecuteRun()
133+
private async Task ExcecuteRun(int numTasks = 8)
119134
{
120135
//var swatch = Stopwatch.StartNew();
121136
var options = GetLookupSettings();
122137
options.EnableAuditTrail = false;
123138
var lookup = GetDnsLookup(options);
124139

125140
while (_running)
141+
{
142+
var tasks = new List<Task>();
143+
for (var i = 0; i < numTasks; i++)
144+
{
145+
tasks.Add(Job());
146+
}
147+
148+
await Task.WhenAll(tasks);
149+
}
150+
151+
async Task Job()
126152
{
127153
try
128154
{

src/DnsClient/DnsQueryOptions.cs

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -851,102 +851,6 @@ public bool Equals(LookupClientSettings other)
851851
&& Equals(MaximumCacheTimeout, other.MaximumCacheTimeout)
852852
&& base.Equals(other);
853853
}
854-
855-
// TODO: remove if LookupClient settings can be made readonly
856-
internal LookupClientSettings Copy(
857-
IReadOnlyCollection<NameServer> nameServers,
858-
TimeSpan? minimumCacheTimeout,
859-
bool? continueOnDnsError = null,
860-
bool? enableAuditTrail = null,
861-
bool? recursion = null,
862-
int? retries = null,
863-
bool? throwDnsErrors = null,
864-
TimeSpan? timeout = null,
865-
bool? useCache = null,
866-
bool? useRandomNameServer = null,
867-
bool? useTcpFallback = null,
868-
bool? useTcpOnly = null)
869-
{
870-
// auto resolved flag might get lost here. But this stuff gets deleted anyways.
871-
return new LookupClientSettings(new LookupClientOptions(nameServers?.ToArray())
872-
{
873-
MinimumCacheTimeout = minimumCacheTimeout,
874-
ContinueOnDnsError = continueOnDnsError ?? ContinueOnDnsError,
875-
EnableAuditTrail = enableAuditTrail ?? EnableAuditTrail,
876-
Recursion = recursion ?? Recursion,
877-
Retries = retries ?? Retries,
878-
ThrowDnsErrors = throwDnsErrors ?? ThrowDnsErrors,
879-
Timeout = timeout ?? Timeout,
880-
UseCache = useCache ?? UseCache,
881-
UseRandomNameServer = useRandomNameServer ?? UseRandomNameServer,
882-
UseTcpFallback = useTcpFallback ?? UseTcpFallback,
883-
UseTcpOnly = useTcpOnly ?? UseTcpOnly,
884-
MaximumCacheTimeout = MaximumCacheTimeout,
885-
ExtendedDnsBufferSize = ExtendedDnsBufferSize,
886-
RequestDnsSecRecords = RequestDnsSecRecords,
887-
ContinueOnEmptyResponse = ContinueOnEmptyResponse
888-
});
889-
}
890-
891-
// TODO: remove if LookupClient settings can be made readonly
892-
internal LookupClientSettings WithContinueOnDnsError(bool value)
893-
{
894-
return Copy(NameServers, MinimumCacheTimeout, continueOnDnsError: value);
895-
}
896-
897-
// TODO: remove if LookupClient settings can be made readonly
898-
internal LookupClientSettings WithEnableAuditTrail(bool value)
899-
{
900-
return Copy(NameServers, MinimumCacheTimeout, enableAuditTrail: value);
901-
}
902-
903-
// TODO: remove if LookupClient settings can be made readonly
904-
internal LookupClientSettings WithMinimumCacheTimeout(TimeSpan? value)
905-
{
906-
return Copy(NameServers, minimumCacheTimeout: value);
907-
}
908-
909-
// TODO: remove if LookupClient settings can be made readonly
910-
internal LookupClientSettings WithRecursion(bool value)
911-
{
912-
return Copy(NameServers, MinimumCacheTimeout, recursion: value);
913-
}
914-
915-
// TODO: remove if LookupClient settings can be made readonly
916-
internal LookupClientSettings WithRetries(int value)
917-
{
918-
return Copy(NameServers, MinimumCacheTimeout, retries: value);
919-
}
920-
921-
// TODO: remove if LookupClient settings can be made readonly
922-
internal LookupClientSettings WithThrowDnsErrors(bool value)
923-
{
924-
return Copy(NameServers, MinimumCacheTimeout, throwDnsErrors: value);
925-
}
926-
927-
// TODO: remove if LookupClient settings can be made readonly
928-
internal LookupClientSettings WithTimeout(TimeSpan value)
929-
{
930-
return Copy(NameServers, MinimumCacheTimeout, timeout: value);
931-
}
932-
933-
// TODO: remove if LookupClient settings can be made readonly
934-
internal LookupClientSettings WithUseCache(bool value)
935-
{
936-
return Copy(NameServers, MinimumCacheTimeout, useCache: value);
937-
}
938-
939-
// TODO: remove if LookupClient settings can be made readonly
940-
internal LookupClientSettings WithUseTcpFallback(bool value)
941-
{
942-
return Copy(NameServers, MinimumCacheTimeout, useTcpFallback: value);
943-
}
944-
945-
// TODO: remove if LookupClient settings can be made readonly
946-
internal LookupClientSettings WithUseTcpOnly(bool value)
947-
{
948-
return Copy(NameServers, MinimumCacheTimeout, useTcpOnly: value);
949-
}
950854
}
951855

952856
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()

src/DnsClient/ILookupClient.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Threading;
63

74
namespace DnsClient
85
{

0 commit comments

Comments
 (0)