Skip to content

Commit

Permalink
Better method of getting port
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Nov 29, 2017
1 parent 4ec5224 commit a713ca7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@

random_open_port <- function(min = 3000, max = 9000) {
random_open_port <- function(min = 3000, max = 9000, n = 20) {
# Unsafe port list from shiny::runApp()
valid_ports <- setdiff(min:max, c(3659, 4045, 6000, 6665:6669, 6697))

while (TRUE) {
port <- sample(valid_ports, 1)
# Try up to n ports
for (port in sample(valid_ports, n)) {
handle <- NULL

# Keep trying ports until we find an open one
# Check if port is open
tryCatch(
handle <- httpuv::startServer("127.0.0.1", port, list()),
error = function(e) { }
)
if (!is.null(handle)) {
httpuv::stopServer(handle)
break
return(port)
}
}

port
stop("Cannot find an available port")
}

check_external <- function(x) {
Expand Down

0 comments on commit a713ca7

Please sign in to comment.