Skip to content

Commit

Permalink
feat: Add and set default SyncLockTimeoutInMs to 30sec
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkallesen committed Feb 14, 2024
1 parent 054f382 commit e31129d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/Atc.Network/Helpers/DnsLookupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Atc.Network.Helpers;
/// </summary>
public static class DnsLookupHelper
{
private const int SyncLockTimeoutInMs = 30_000;

private static readonly SemaphoreSlim SyncLock = new(1, 1);
private static string? hostname;
private static IPAddress[]? hostAddresses;
Expand Down Expand Up @@ -34,7 +36,7 @@ public static class DnsLookupHelper

try
{
await SyncLock.WaitAsync(cancellationToken);
await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

if (ipAddress.IsPrivate())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Network/Helpers/InternetWorldTimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Atc.Network.Helpers;
public static class InternetWorldTimeHelper
{
private const string WorldTimeBaseApi = "https://worldtimeapi.org/api/timezone/";
private const int SyncLockTimeout = 3_000;
private const int SyncLockTimeoutInMs = 30_000;
private static readonly SemaphoreSlim SyncLock = new(1, 1);

/// <summary>
Expand Down Expand Up @@ -40,7 +40,7 @@ public static class InternetWorldTimeHelper

try
{
await SyncLock.WaitAsync(SyncLockTimeout, cancellationToken);
await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

if (!NetworkInformationHelper.HasConnection())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Network/Helpers/MacAddressVendorLookupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class MacAddressVendorLookupHelper
private const string AtcCacheFolder = "AtcCache";
private const string AtcCacheFile = "macvendors.txt";
private const int MinimumCallDelay = 1_200;
private const int SyncLockTimeout = 3_000;
private const int SyncLockTimeoutInMs = 30_000;

private static readonly SemaphoreSlim SyncLock = new(1, 1);
private static readonly Uri MacVendorsApiUrl = new("http://api.macvendors.com/");
Expand All @@ -24,7 +24,7 @@ public static class MacAddressVendorLookupHelper

try
{
await SyncLock.WaitAsync(SyncLockTimeout, cancellationToken);
await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

macAddress = macAddress.ToUpper(GlobalizationConstants.EnglishCultureInfo);
var cacheVendorName = GetVendorFromCacheFileLines(macAddress);
Expand Down
7 changes: 4 additions & 3 deletions src/Atc.Network/Internet/IPPortScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace Atc.Network.Internet;
public class IPPortScan : IIPPortScan, IDisposable
{
private const int InternalDelayInMs = 5;
private readonly SemaphoreSlim syncLock = new(1, 1);
private const int SyncLockTimeoutInMs = 30_000;

private readonly SemaphoreSlim syncLock = new(1, 1);
private IPAddress? ipAddress;
private int timeoutInMs = 100;

Expand Down Expand Up @@ -69,7 +70,7 @@ public async Task<bool> CanConnectWithTcp(

try
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

await Task.Delay(InternalDelayInMs, cancellationToken);
var cancellationCompletionSource = new TaskCompletionSource<bool>();
Expand Down Expand Up @@ -151,7 +152,7 @@ public async Task<bool> CanConnectWithHttpOrHttps(

try
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

await Task.Delay(InternalDelayInMs, cancellationToken);
var cancellationCompletionSource = new TaskCompletionSource<bool>();
Expand Down
13 changes: 7 additions & 6 deletions src/Atc.Network/Internet/IPScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Atc.Network.Internet;
[SuppressMessage("Minor Code Smell", "S2486:Generic exceptions should not be ignored", Justification = "OK.")]
public partial class IPScanner : IIPScanner, IDisposable
{
private const int SyncLockTimeoutInMs = 30_000;

private readonly SemaphoreSlim syncLock = new(1, 1);
private readonly ConcurrentBag<IPScanResult> processedScanResults = new();
private readonly IPScannerProgressReport progressReporting = new();
Expand Down Expand Up @@ -85,7 +87,7 @@ public async Task<IPScanResults> ScanRange(
}

var ipAddresses = IPv4AddressHelper.GetAddressesInRange(startIpAddress, endIpAddress);
if (!ipAddresses.Any())
if (ipAddresses.Count == 0)
{
scanResults.ErrorMessage = "Nothing to process";
scanResults.End = DateTime.Now;
Expand All @@ -94,7 +96,7 @@ public async Task<IPScanResults> ScanRange(

try
{
await syncLock.WaitAsync(cancellationToken);
await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken);

if (Configuration.ResolveMacAddress)
{
Expand Down Expand Up @@ -191,13 +193,12 @@ private async Task DoScan(
HandleResolveMacAddress(ipScanResult, ipAddress);
}

if (Configuration.ResolveMacAddress &&
Configuration.ResolveVendorFromMacAddress)
if (Configuration is { ResolveMacAddress: true, ResolveVendorFromMacAddress: true })
{
await HandleResolveVendorFromMacAddress(ipScanResult, cancellationToken);
}

if (Configuration.PortNumbers.Any())
if (Configuration.PortNumbers.Count != 0)
{
foreach (var portNumber in Configuration.PortNumbers)
{
Expand Down Expand Up @@ -238,7 +239,7 @@ private void HandleResolveMacAddress(
IPAddress ipAddress)
{
arpEntities ??= ArpHelper.GetArpResult();
if (arpEntities.Any())
if (arpEntities.Length != 0)
{
var arpEntity = arpEntities.FirstOrDefault(x => x.IPAddress.Equals(ipAddress));
if (arpEntity is not null)
Expand Down

0 comments on commit e31129d

Please sign in to comment.