-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
169 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Sockets; | ||
using System.Runtime.ExceptionServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nethermind.Network; | ||
|
||
public static class NetworkHelper | ||
{ | ||
private static PortInUseException MapOrRethrow(Exception exception, int[]? ports = null, string[]? urls = null) | ||
{ | ||
if (exception is AggregateException) | ||
exception = exception.InnerException!; | ||
|
||
switch (exception) | ||
{ | ||
case SocketException { SocketErrorCode: SocketError.AddressAlreadyInUse or SocketError.AccessDenied }: | ||
return ports != null ? new(exception, ports) : new(exception, urls!); | ||
case IOException { Source: "Grpc.Core" } when exception.Message.Contains("Failed to bind port"): | ||
return ports != null ? new(exception, ports) : new(exception, urls!); | ||
default: | ||
ExceptionDispatchInfo.Throw(exception); | ||
throw exception; // Make compiler happy, should never execute | ||
} | ||
} | ||
|
||
public static void HandlePortTakenError(Action action, params int[] ports) | ||
{ | ||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw MapOrRethrow(exception, ports: ports); | ||
} | ||
} | ||
|
||
public static T HandlePortTakenError<T>(Func<T> action, params int[] ports) | ||
{ | ||
try | ||
{ | ||
return action(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw MapOrRethrow(exception, ports: ports); | ||
} | ||
} | ||
|
||
public static async Task HandlePortTakenError(Func<Task> action, params string[] urls) | ||
{ | ||
try | ||
{ | ||
await action(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw MapOrRethrow(exception, urls: urls); | ||
} | ||
} | ||
|
||
public static async Task<T> HandlePortTakenError<T>(Func<Task<T>> action, params int[] ports) | ||
{ | ||
try | ||
{ | ||
return await action(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw MapOrRethrow(exception, ports: ports); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Nethermind.Config; | ||
|
||
namespace Nethermind.Network; | ||
|
||
public class PortInUseException : IOException | ||
{ | ||
public PortInUseException(Exception exception, params int[] ports) : base( | ||
$"{GetReason(ports)} " + | ||
"If you intend to run 2 or more nodes on one machine, ensure you have changed all configured ports under: " + | ||
$"{"\n\t" + string.Join("\n\t", ConfigExtensions.GetPortOptionNames())}", | ||
exception | ||
) | ||
{ } | ||
|
||
public PortInUseException(Exception exception, params string[] urls) : this(exception, GetPorts(urls)) { } | ||
|
||
private static int[] GetPorts(string[] urls) => urls.Select(u => new Uri(u).Port).ToArray(); | ||
|
||
private static string GetReason(params int[] ports) | ||
{ | ||
return ports.Length switch | ||
{ | ||
0 => "One of the configured ports is in use.", | ||
1 => $"Port {ports[0]} is in use.", | ||
_ => $"One or more of the ports {string.Join(',', ports)} are in use." | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters