-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add port conflict exception #4565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
c504b13
3ec630e
de1afc7
4a69660
feeaf2a
69961ba
b91487f
b749733
8752282
817286e
d1cb28c
7ba61a7
489ce6f
49555f5
896ebf2
9229afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,20 +14,26 @@ | |
| */ | ||
| package org.hyperledger.besu.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.Inet6Address; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.NetworkInterface; | ||
| import java.net.ServerSocket; | ||
| import java.net.SocketException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import com.google.common.base.Suppliers; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class NetworkUtility { | ||
| public static final String INADDR_ANY = "0.0.0.0"; | ||
| public static final String INADDR6_ANY = "0:0:0:0:0:0:0:0"; | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(NetworkUtility.class); | ||
|
|
||
| private NetworkUtility() {} | ||
|
|
||
| private static final Supplier<Boolean> ipv6Available = | ||
|
|
@@ -98,4 +104,23 @@ public static void checkPort(final int port, final String portTypeName) { | |
| "%s port requires a value between 1 and 65535. Got %d.", portTypeName, port)); | ||
| } | ||
| } | ||
|
|
||
| public static boolean isPortAvailableForTcp(final int port) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be private? |
||
| try (final ServerSocket serverSocket = new ServerSocket()) { | ||
| serverSocket.setReuseAddress(true); | ||
| serverSocket.bind(new InetSocketAddress(port)); | ||
| return true; | ||
| } catch (IOException ex) { | ||
| LOG.trace(String.format("Failed to open port %d for TCP", port), ex); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void checkPortsAvailable(final int tcpPort) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then method name says
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the name of the method, thanks for raising that @fab-10
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the approach to cover the ports we will require to initialise all the services defined in the config. So we can have a more general solution that loops through the ports and check if they are available. |
||
| if (!isPortAvailableForTcp(tcpPort)) { | ||
| throw new InvalidConfigurationException( | ||
| String.format( | ||
| "Port %d is already in use. Check for other processes using this port.", tcpPort)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason to add the extra empty line?