Skip to content

Commit e9ebd06

Browse files
committed
allow 3 connectoins
1 parent 12d1ebb commit e9ebd06

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

crates/chat-cli/src/auth/portal.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rand::Rng;
1717
use tokio::net::TcpListener;
1818
use tracing::{
1919
debug,
20+
error,
2021
info,
2122
warn,
2223
};
@@ -232,12 +233,33 @@ async fn wait_for_auth_callback(
232233
) -> Result<AuthPortalCallback, AuthError> {
233234
let (tx, mut rx) = tokio::sync::mpsc::channel::<AuthPortalCallback>(1);
234235

235-
// Accept a single connection
236236
let server_handle = tokio::spawn(async move {
237-
if let Ok((stream, _)) = listener.accept().await {
238-
let io = TokioIo::new(stream);
239-
let service = AuthCallbackService { tx: tx.clone() };
240-
let _ = http1::Builder::new().serve_connection(io, service).await;
237+
const MAX_CONNECTIONS: usize = 3;
238+
let mut count = 0;
239+
240+
loop {
241+
if count >= MAX_CONNECTIONS {
242+
warn!("Reached max connections ({})", MAX_CONNECTIONS);
243+
break;
244+
}
245+
246+
match listener.accept().await {
247+
Ok((stream, _)) => {
248+
count += 1;
249+
debug!("Connection {}/{}", count, MAX_CONNECTIONS);
250+
251+
let io = TokioIo::new(stream);
252+
let service = AuthCallbackService { tx: tx.clone() };
253+
254+
tokio::spawn(async move {
255+
let _ = http1::Builder::new().serve_connection(io, service).await;
256+
});
257+
},
258+
Err(e) => {
259+
error!("Accept failed: {}", e);
260+
break;
261+
},
262+
}
241263
}
242264
});
243265

@@ -329,7 +351,7 @@ async fn handle_valid_callback(
329351
}
330352

331353
async fn handle_invalid_callback(path: &str) -> Result<Response<Full<Bytes>>, AuthError> {
332-
info!(%path, "Invalid callback path, redirecting to portal");
354+
info!(%path, "Invalid callback path: {}, redirecting to portal", path);
333355
build_redirect_response("error", Some("Invalid callback path"))
334356
}
335357

0 commit comments

Comments
 (0)