refactor(peer): split bulk request dispatch
Move file-chunk and Stream Install branches into focused helpers so the new admission path remains within the workspace's strict Clippy limits without changing request behavior. Test Plan: - just clippy - git diff --check
This commit is contained in:
@@ -430,8 +430,7 @@ async fn run_server_body(
|
|||||||
|
|
||||||
if !has_child_capacity(connection_tasks.len(), MAX_ESTABLISHED_CONNECTIONS) {
|
if !has_child_capacity(connection_tasks.len(), MAX_ESTABLISHED_CONNECTIONS) {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"Closing excess peer connection from {remote_addr} at application limit {}",
|
"Closing excess peer connection from {remote_addr} at application limit {MAX_ESTABLISHED_CONNECTIONS}",
|
||||||
MAX_ESTABLISHED_CONNECTIONS,
|
|
||||||
);
|
);
|
||||||
connection.close(application::Error::UNKNOWN);
|
connection.close(application::Error::UNKNOWN);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -300,55 +300,88 @@ async fn dispatch_request(
|
|||||||
.schedule_hint(StateDomain::CallToPlay, hint, source_ip);
|
.schedule_hint(StateDomain::CallToPlay, hint, source_ip);
|
||||||
DispatchResult::close(framed_tx)
|
DispatchResult::close(framed_tx)
|
||||||
}
|
}
|
||||||
Request::GetGameFileChunk {
|
request @ Request::GetGameFileChunk { .. } => {
|
||||||
game_id,
|
dispatch_file_chunk(ctx, request, framed_tx, stream_shutdown).await
|
||||||
content_id,
|
|
||||||
relative_path,
|
|
||||||
offset,
|
|
||||||
length,
|
|
||||||
} => {
|
|
||||||
match handle_file_chunk_request(
|
|
||||||
ctx,
|
|
||||||
game_id,
|
|
||||||
content_id,
|
|
||||||
relative_path,
|
|
||||||
offset,
|
|
||||||
length,
|
|
||||||
framed_tx,
|
|
||||||
stream_shutdown,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
ChunkDispatch::Finished(writer) => DispatchResult::close(writer),
|
|
||||||
ChunkDispatch::Reset(writer) => DispatchResult::reset(writer),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Request::StreamInstall {
|
Request::StreamInstall {
|
||||||
game_id,
|
game_id,
|
||||||
content_id,
|
content_id,
|
||||||
} => {
|
} => {
|
||||||
let Some(mut stream_install_admission) = admission.try_acquire_stream_install(origin)
|
dispatch_stream_install(
|
||||||
else {
|
|
||||||
let mut tx = framed_tx.into_inner();
|
|
||||||
let _ = tx.reset(application::Error::UNKNOWN);
|
|
||||||
return DispatchResult::reset(FramedWrite::new(tx, control_codec()));
|
|
||||||
};
|
|
||||||
let stream_install_permit = stream_install_admission.take_global_permit();
|
|
||||||
let writer = handle_stream_install_request(
|
|
||||||
ctx,
|
ctx,
|
||||||
game_id,
|
game_id,
|
||||||
content_id,
|
content_id,
|
||||||
framed_tx,
|
framed_tx,
|
||||||
stream_shutdown,
|
stream_shutdown,
|
||||||
stream_install_permit,
|
admission,
|
||||||
|
origin,
|
||||||
)
|
)
|
||||||
.await;
|
.await
|
||||||
drop(stream_install_admission);
|
|
||||||
DispatchResult::close(writer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn dispatch_file_chunk(
|
||||||
|
ctx: &PeerCtx,
|
||||||
|
request: Request,
|
||||||
|
framed_tx: ResponseWriter,
|
||||||
|
stream_shutdown: &CancellationToken,
|
||||||
|
) -> DispatchResult {
|
||||||
|
let Request::GetGameFileChunk {
|
||||||
|
game_id,
|
||||||
|
content_id,
|
||||||
|
relative_path,
|
||||||
|
offset,
|
||||||
|
length,
|
||||||
|
} = request
|
||||||
|
else {
|
||||||
|
unreachable!("file-chunk dispatcher received a different request")
|
||||||
|
};
|
||||||
|
match handle_file_chunk_request(
|
||||||
|
ctx,
|
||||||
|
game_id,
|
||||||
|
content_id,
|
||||||
|
relative_path,
|
||||||
|
offset,
|
||||||
|
length,
|
||||||
|
framed_tx,
|
||||||
|
stream_shutdown,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
ChunkDispatch::Finished(writer) => DispatchResult::close(writer),
|
||||||
|
ChunkDispatch::Reset(writer) => DispatchResult::reset(writer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn dispatch_stream_install(
|
||||||
|
ctx: &PeerCtx,
|
||||||
|
game_id: String,
|
||||||
|
content_id: lanspread_db::content_manifest::ContentId,
|
||||||
|
framed_tx: ResponseWriter,
|
||||||
|
stream_shutdown: &CancellationToken,
|
||||||
|
admission: &ServerAdmission,
|
||||||
|
origin: ObservedOrigin,
|
||||||
|
) -> DispatchResult {
|
||||||
|
let Some(mut stream_install_admission) = admission.try_acquire_stream_install(origin) else {
|
||||||
|
let mut tx = framed_tx.into_inner();
|
||||||
|
let _ = tx.reset(application::Error::UNKNOWN);
|
||||||
|
return DispatchResult::reset(FramedWrite::new(tx, control_codec()));
|
||||||
|
};
|
||||||
|
let stream_install_permit = stream_install_admission.take_global_permit();
|
||||||
|
let writer = handle_stream_install_request(
|
||||||
|
ctx,
|
||||||
|
game_id,
|
||||||
|
content_id,
|
||||||
|
framed_tx,
|
||||||
|
stream_shutdown,
|
||||||
|
stream_install_permit,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
drop(stream_install_admission);
|
||||||
|
DispatchResult::close(writer)
|
||||||
|
}
|
||||||
|
|
||||||
fn reset_response_writer(framed_tx: ResponseWriter, label: &str) -> DispatchResult {
|
fn reset_response_writer(framed_tx: ResponseWriter, label: &str) -> DispatchResult {
|
||||||
let mut tx = framed_tx.into_inner();
|
let mut tx = framed_tx.into_inner();
|
||||||
if let Err(error) = tx.reset(application::Error::UNKNOWN) {
|
if let Err(error) = tx.reset(application::Error::UNKNOWN) {
|
||||||
|
|||||||
Reference in New Issue
Block a user