refactor send_request in api/server_server

Seriously, what is going on with the control flow in this codebase?
This commit is contained in:
Charles Hall 2024-06-23 18:20:21 -07:00
parent e13db834ed
commit cb036593ea
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -258,14 +258,17 @@ where
let response = let response =
services().globals.federation_client().execute(reqwest_request).await; services().globals.federation_client().execute(reqwest_request).await;
match response { let mut response = response.inspect_err(|error| {
Ok(mut response) => { if log_error {
warn!(%error, "Could not send request");
}
})?;
// reqwest::Response -> http::Response conversion // reqwest::Response -> http::Response conversion
let status = response.status(); let status = response.status();
debug!(status = u16::from(status), "Received response"); debug!(status = u16::from(status), "Received response");
let mut http_response_builder = http::Response::builder() let mut http_response_builder =
.status(status) http::Response::builder().status(status).version(response.version());
.version(response.version());
mem::swap( mem::swap(
response.headers_mut(), response.headers_mut(),
http_response_builder http_response_builder
@ -284,10 +287,8 @@ where
if status != 200 { if status != 200 {
warn!( warn!(
status = u16::from(status), status = u16::from(status),
response = dbg_truncate_str( response =
String::from_utf8_lossy(&body).as_ref(), dbg_truncate_str(String::from_utf8_lossy(&body).as_ref(), 100)
100,
)
.into_owned(), .into_owned(),
"Received error over federation", "Received error over federation",
); );
@ -297,49 +298,27 @@ where
.body(body) .body(body)
.expect("reqwest body is valid http body"); .expect("reqwest body is valid http body");
if status == 200 { if status != 200 {
return Err(Error::Federation(
destination.to_owned(),
RumaError::from_http_response(http_response),
));
}
debug!("Parsing response bytes"); debug!("Parsing response bytes");
let response = let response = T::IncomingResponse::try_from_http_response(http_response);
T::IncomingResponse::try_from_http_response(http_response);
if response.is_ok() && write_destination_to_cache { if response.is_ok() && write_destination_to_cache {
METRICS.record_lookup( METRICS.record_lookup(Lookup::FederationDestination, FoundIn::Remote);
Lookup::FederationDestination, services().globals.actual_destination_cache.write().await.insert(
FoundIn::Remote,
);
services()
.globals
.actual_destination_cache
.write()
.await
.insert(
OwnedServerName::from(destination), OwnedServerName::from(destination),
(actual_destination, host), (actual_destination, host),
); );
} }
response.map_err(|e| { response.map_err(|e| {
warn!(error = %e, "Invalid 200 response",); warn!(error = %e, "Invalid 200 response");
Error::BadServerResponse( Error::BadServerResponse("Server returned bad 200 response.")
"Server returned bad 200 response.",
)
}) })
} else {
Err(Error::Federation(
destination.to_owned(),
RumaError::from_http_response(http_response),
))
}
}
Err(e) => {
if log_error {
warn!(
error = %e,
"Could not send request",
);
}
Err(e.into())
}
}
} }
fn get_ip_with_port(destination_str: &str) -> Option<FedDest> { fn get_ip_with_port(destination_str: &str) -> Option<FedDest> {