Skip to content
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

fix(cargo-shuttle): prompt for new port if port is taken #1270

Merged
merged 11 commits into from Oct 2, 2023
23 changes: 16 additions & 7 deletions cargo-shuttle/src/lib.rs
Expand Up @@ -112,12 +112,21 @@ impl Shuttle {
self.run(args, provided_path_to_init).await
}

fn find_available_port(run_args: &mut RunArgs) {
fn find_available_port(run_args: &mut RunArgs, services_len: usize) {
let mut available_port = 8010;
BadgerBloke marked this conversation as resolved.
Show resolved Hide resolved
for port in (available_port..=std::u16::MAX).step_by(10) {
if portpicker::is_free_tcp(port) && portpicker::is_free_tcp(port + 1) {
available_port = port;
break;
for port in (available_port..=std::u16::MAX).step_by(services_len.max(10)) {
if portpicker::is_free_tcp(port) {
BadgerBloke marked this conversation as resolved.
Show resolved Hide resolved
let mut is_inner_ports_free = true;
for inner_port in port..(port + services_len as u16) {
if !portpicker::is_free_tcp(inner_port) {
is_inner_ports_free = false;
BadgerBloke marked this conversation as resolved.
Show resolved Hide resolved
}
}

if is_inner_ports_free {
available_port = port;
break;
}
}
}

Expand Down Expand Up @@ -1042,7 +1051,7 @@ impl Shuttle {
)> = Vec::new();

if !portpicker::is_free_tcp(run_args.port) {
Shuttle::find_available_port(&mut run_args);
Shuttle::find_available_port(&mut run_args, services.len());
}
BadgerBloke marked this conversation as resolved.
Show resolved Hide resolved

let mut signal_received = false;
Expand Down Expand Up @@ -1198,7 +1207,7 @@ impl Shuttle {
)> = Vec::new();

if !portpicker::is_free_tcp(run_args.port) {
Shuttle::find_available_port(&mut run_args);
Shuttle::find_available_port(&mut run_args, services.len());
}

let mut signal_received = false;
Expand Down