refactor(controller): use state to find available port range
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
use miette::Result;
|
||||
use rand::{Rng, distr::Alphanumeric};
|
||||
|
||||
use crate::state::StateManager;
|
||||
const DEFAULT_POSTGRES_PORT: u16 = 5432;
|
||||
const PORT_SEARCH_RANGE: u16 = 100;
|
||||
|
||||
pub fn find_available_port() -> Result<u16> {
|
||||
pub fn find_available_port(state: &StateManager) -> Result<u16> {
|
||||
use std::net::TcpListener;
|
||||
|
||||
for port in DEFAULT_POSTGRES_PORT..(DEFAULT_POSTGRES_PORT + PORT_SEARCH_RANGE) {
|
||||
let starting_port = state
|
||||
.get_highest_used_port()
|
||||
.unwrap_or(DEFAULT_POSTGRES_PORT);
|
||||
|
||||
for port in starting_port..(starting_port + PORT_SEARCH_RANGE) {
|
||||
if TcpListener::bind(("127.0.0.1", port)).is_ok() {
|
||||
return Ok(port);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user