refactor(controller): use state to find available port range

This commit is contained in:
hdbg
2025-12-08 14:32:08 +01:00
parent 570cec8b47
commit 471f9129b4
3 changed files with 11 additions and 5 deletions

View File

@@ -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);
}