Skip to content

Commit c74e19b

Browse files
committed
fix a bug
1 parent ad05edc commit c74e19b

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wait-service"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
authors = ["Magic Len <len@magiclen.org>"]
55
edition = "2021"
66
repository = "https://github.com/magiclen/wait-service"

src/main.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,30 @@ async fn tcp(
8787

8888
let start = Instant::now();
8989

90-
for ip in ips {
91-
if timeout.is_zero() {
92-
while TcpStream::connect(ip).await.is_err() {
93-
sleep(SLEEP_INTERVAL).await;
90+
if timeout.is_zero() {
91+
'outer: loop {
92+
for ip in ips.iter().cloned() {
93+
if TcpStream::connect(ip).await.is_ok() {
94+
break 'outer;
95+
}
9496
}
95-
} else {
96-
while let Err(err) = time::timeout(timeout, TcpStream::connect(ip)).await? {
97-
if Instant::now() - start > timeout {
98-
return Err(err.into());
99-
} else {
100-
sleep(SLEEP_INTERVAL).await;
97+
98+
sleep(SLEEP_INTERVAL).await;
99+
}
100+
} else {
101+
'outer_timeout: loop {
102+
for ip in ips.iter().cloned() {
103+
match time::timeout(timeout, TcpStream::connect(ip)).await? {
104+
Ok(_) => break 'outer_timeout,
105+
Err(err) => {
106+
if Instant::now() - start > timeout {
107+
return Err(err.into());
108+
}
109+
}
101110
}
102111
}
112+
113+
sleep(SLEEP_INTERVAL).await;
103114
}
104115
}
105116
}

0 commit comments

Comments
 (0)