Skip to content

Commit fe41af6

Browse files
authored
Rollup merge of #104146 - Ayush1325:remote-test-server, r=jyn514
Retry binding TCP Socket in remote-test-server This allows retrying binding TCP Socket multiple times. This is useful when using emulators as network might not be available in the beginning. This was orignally implemented in #100316 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2 parents 206928c + 06a77af commit fe41af6

File tree

1 file changed

+13
-1
lines changed
  • src/tools/remote-test-server/src

1 file changed

+13
-1
lines changed

src/tools/remote-test-server/src/main.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ macro_rules! t {
3939
}
4040

4141
static TEST: AtomicUsize = AtomicUsize::new(0);
42+
const RETRY_INTERVAL: u64 = 1;
43+
const NUMBER_OF_RETRIES: usize = 5;
4244

4345
#[derive(Copy, Clone)]
4446
struct Config {
@@ -115,7 +117,7 @@ fn main() {
115117
let config = Config::parse_args();
116118
println!("starting test server");
117119

118-
let listener = t!(TcpListener::bind(config.bind));
120+
let listener = bind_socket(config.bind);
119121
let (work, tmp): (PathBuf, PathBuf) = if cfg!(target_os = "android") {
120122
("/data/local/tmp/work".into(), "/data/local/tmp/work/tmp".into())
121123
} else {
@@ -159,6 +161,16 @@ fn main() {
159161
}
160162
}
161163

164+
fn bind_socket(addr: SocketAddr) -> TcpListener {
165+
for _ in 0..(NUMBER_OF_RETRIES - 1) {
166+
if let Ok(x) = TcpListener::bind(addr) {
167+
return x;
168+
}
169+
std::thread::sleep(std::time::Duration::from_secs(RETRY_INTERVAL));
170+
}
171+
TcpListener::bind(addr).unwrap()
172+
}
173+
162174
fn handle_push(socket: TcpStream, work: &Path, config: Config) {
163175
let mut reader = BufReader::new(socket);
164176
let dst = recv(&work, &mut reader);

0 commit comments

Comments
 (0)