Skip to content

Commit 5223352

Browse files
committed
fix(cli): bind directly from pre-allocated tcplistener
1 parent 363028d commit 5223352

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

meta-cli/src/com/server.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@ use lazy_static::lazy_static;
1010
use reqwest::StatusCode;
1111
use serde::Deserialize;
1212
use serde_json::json;
13-
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
13+
use std::{
14+
io::{Error, ErrorKind},
15+
net::{Ipv4Addr, SocketAddrV4, TcpListener},
16+
sync::Arc,
17+
};
18+
19+
pub struct PortManager {
20+
pub tcp_listener: Arc<TcpListener>,
21+
}
1422

15-
pub fn random_free_port() -> u16 {
16-
let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0);
17-
TcpListener::bind(addr)
18-
.unwrap()
19-
.local_addr()
20-
.unwrap()
21-
.port()
23+
impl PortManager {
24+
pub fn new() -> Self {
25+
let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0);
26+
Self {
27+
tcp_listener: Arc::new(TcpListener::bind(addr).unwrap()),
28+
}
29+
}
2230
}
2331

2432
lazy_static! {
25-
#[derive(Debug)]
26-
pub static ref SERVER_PORT: u16 = random_free_port();
33+
pub static ref PORT_MAN: Arc<PortManager> = Arc::new(PortManager::new());
34+
}
35+
36+
pub fn get_instance_port() -> u16 {
37+
PORT_MAN.tcp_listener.local_addr().unwrap().port()
2738
}
2839

2940
#[derive(Debug, Deserialize)]
@@ -102,15 +113,23 @@ async fn response(req_body: String) -> impl Responder {
102113
}
103114

104115
pub async fn spawn_server() -> std::io::Result<()> {
105-
let port = SERVER_PORT.to_owned();
116+
let port = get_instance_port();
117+
118+
let tcp_listener = PORT_MAN
119+
.tcp_listener
120+
.try_clone()
121+
.map_err(|e| Error::new(ErrorKind::AddrNotAvailable, e.to_string()))?;
122+
106123
eprintln!("Server is running at {:?}", port);
107124
HttpServer::new(|| {
108125
App::new()
109126
.service(config)
110127
.service(command)
111128
.service(response)
112129
})
113-
.bind(("127.0.0.1", port))?
130+
.listen(tcp_listener)?
114131
.run()
115-
.await
132+
.await?;
133+
134+
Ok(())
116135
}

meta-cli/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use clap::Parser;
2222
use cli::upgrade::upgrade_check;
2323
use cli::Action;
2424
use cli::Args;
25-
use com::server::{spawn_server, SERVER_PORT};
25+
use com::server::{get_instance_port, spawn_server};
2626
use futures::try_join;
2727
use futures::TryFutureExt;
2828
use log::{error, warn};
@@ -33,7 +33,8 @@ shadow!(build);
3333
fn main() -> Result<()> {
3434
setup_panic_hook();
3535
logger::init();
36-
std::env::set_var("META_CLI_SERVER_PORT", SERVER_PORT.to_string());
36+
37+
std::env::set_var("META_CLI_SERVER_PORT", get_instance_port().to_string());
3738

3839
let _ = actix::System::with_tokio_rt(|| {
3940
tokio::runtime::Builder::new_multi_thread()

0 commit comments

Comments
 (0)