int findAvailablePort(int minPort, int maxPort) {
            Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
            Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equal to 'minPort'");
            Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);

            int portRange = maxPort - minPort;
            int candidatePort;
            int searchCounter = 0;
            do {
                if (searchCounter > portRange) {
                    throw new IllegalStateException(String.format(
                            "Could not find an available %s port in the range [%d, %d] after %d attempts",
                            name(), minPort, maxPort, searchCounter));
                }
                candidatePort = findRandomPort(minPort, maxPort);
                searchCounter++;
            } while (!isPortAvailable(candidatePort));

            return candidatePort;
        }
Find an available port for this {@code SocketType}, randomly selected from the range [{@code minPort},
{@code maxPort}].

@param minPort the minimum port number
@param maxPort the maximum port number
@return an available port number for this socket type
@throws IllegalStateException if no available port could be found
grpc-common-spring-boot/src/main/java/net/devh/boot/grpc/common/util/SocketUtils.java