Skip to content

Commit bc0c76d

Browse files
committed
fix: duplicating clients in use
1 parent 9d64d55 commit bc0c76d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

crates/kftray-portforward/src/client.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::{
77
Context,
88
Result,
99
};
10-
use futures::future::join_all;
10+
use futures::future::select_ok;
1111
use hyper_openssl::client::legacy::HttpsConnector;
1212
use hyper_util::client::legacy::connect::HttpConnector;
1313
use hyper_util::rt::TokioExecutor;
@@ -188,13 +188,15 @@ async fn create_client_with_config(config: &Config) -> Option<Client> {
188188
config_with_invalid_certs_false,
189189
);
190190

191-
for (description, strategy) in strategies {
192-
if let Some(client) = try_create_client(description, strategy).await {
193-
return Some(client);
194-
}
195-
}
191+
let futures: Vec<_> = strategies
192+
.into_iter()
193+
.map(|(description, strategy)| Box::pin(try_create_client(description, strategy)))
194+
.collect();
196195

197-
None
196+
match select_ok(futures).await {
197+
Ok((client, _)) => Some(client),
198+
Err(_) => None,
199+
}
198200
}
199201

200202
fn create_strategies<'a>(
@@ -260,21 +262,21 @@ fn create_strategies<'a>(
260262
]
261263
}
262264

263-
async fn try_create_client(description: &str, strategy: StrategyFuture<'_>) -> Option<Client> {
265+
async fn try_create_client(description: &str, strategy: StrategyFuture<'_>) -> Result<Client, ()> {
264266
info!("Attempting to create client with {}", description);
265267
match strategy.await {
266268
Ok(client) => {
267269
if test_client(&client).await.is_ok() {
268270
info!("Successfully created client with {}", description);
269-
Some(client)
271+
Ok(client)
270272
} else {
271273
warn!("{} failed to connect.", description);
272-
None
274+
Err(())
273275
}
274276
}
275277
Err(e) => {
276278
warn!("Failed to create {}: {}", description, e);
277-
None
279+
Err(())
278280
}
279281
}
280282
}

0 commit comments

Comments
 (0)