improvement: try out multiple servers when joining remote rooms

next
Timo Kösters 2020-09-14 11:00:31 +02:00
parent 1f292c09f2
commit c5313b3e8f
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
5 changed files with 65 additions and 48 deletions

35
Cargo.lock generated
View File

@ -157,7 +157,7 @@ dependencies = [
"addr2line", "addr2line",
"cfg-if", "cfg-if",
"libc", "libc",
"miniz_oxide 0.4.1", "miniz_oxide 0.4.2",
"object", "object",
"rustc-demangle", "rustc-demangle",
] ]
@ -1054,11 +1054,12 @@ dependencies = [
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.4.1" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d7559a8a40d0f97e1edea3220f698f78b1c5ab67532e49f68fde3910323b722" checksum = "c60c0dfe32c10b43a144bad8fc83538c52f58302c92300ea7ec7bf7b38d5a7b9"
dependencies = [ dependencies = [
"adler", "adler",
"autocfg",
] ]
[[package]] [[package]]
@ -1634,7 +1635,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.0.1" version = "0.0.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"ruma-api", "ruma-api",
"ruma-appservice-api", "ruma-appservice-api",
@ -1650,7 +1651,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api" name = "ruma-api"
version = "0.17.0-alpha.1" version = "0.17.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"http", "http",
"percent-encoding", "percent-encoding",
@ -1665,7 +1666,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api-macros" name = "ruma-api-macros"
version = "0.17.0-alpha.1" version = "0.17.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -1676,7 +1677,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.2.0-alpha.1" version = "0.2.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"ruma-api", "ruma-api",
"ruma-common", "ruma-common",
@ -1689,7 +1690,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.10.0-alpha.1" version = "0.10.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"assign", "assign",
"http", "http",
@ -1708,7 +1709,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -1722,7 +1723,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.22.0-alpha.1" version = "0.22.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -1737,7 +1738,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events-macros" name = "ruma-events-macros"
version = "0.22.0-alpha.1" version = "0.22.0-alpha.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -1748,7 +1749,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.0.3" version = "0.0.3"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -1763,7 +1764,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers" name = "ruma-identifiers"
version = "0.17.4" version = "0.17.4"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"rand", "rand",
"ruma-identifiers-macros", "ruma-identifiers-macros",
@ -1775,7 +1776,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-macros" name = "ruma-identifiers-macros"
version = "0.17.4" version = "0.17.4"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1786,7 +1787,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.1.1" version = "0.1.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"serde", "serde",
"strum", "strum",
@ -1795,7 +1796,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde" name = "ruma-serde"
version = "0.2.3" version = "0.2.3"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"form_urlencoded", "form_urlencoded",
"itoa", "itoa",
@ -1807,7 +1808,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.6.0-dev.1" version = "0.6.0-dev.1"
source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#8d763abaecb13f4799a31ecf1e0da77d2bc956a6" source = "git+https://github.com/timokoesters/ruma?branch=timo-fed-fixes#ee66e30cbd58aecbbfde1d7008d7d6457deef87b"
dependencies = [ dependencies = [
"base64", "base64",
"ring", "ring",

View File

@ -63,7 +63,7 @@ pub async fn get_alias_helper(
if room_alias.server_name() != db.globals.server_name() { if room_alias.server_name() != db.globals.server_name() {
let response = server_server::send_request( let response = server_server::send_request(
&db, &db,
room_alias.server_name().to_string(), room_alias.server_name(),
federation::query::get_room_information::v1::Request { room_alias }, federation::query::get_room_information::v1::Request { room_alias },
) )
.await?; .await?;
@ -79,5 +79,5 @@ pub async fn get_alias_helper(
"Room with alias not found.", "Room with alias not found.",
))?; ))?;
Ok(get_alias::Response::new(room_id, vec![db.globals.server_name().to_string()]).into()) Ok(get_alias::Response::new(room_id, vec![db.globals.server_name().to_owned()]).into())
} }

View File

@ -19,7 +19,7 @@ use ruma::{
room::{avatar, canonical_alias, guest_access, history_visibility, name, topic}, room::{avatar, canonical_alias, guest_access, history_visibility, name, topic},
EventType, EventType,
}, },
Raw, Raw, ServerName,
}; };
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
@ -65,9 +65,9 @@ pub async fn get_public_rooms_route(
) -> ConduitResult<get_public_rooms::Response> { ) -> ConduitResult<get_public_rooms::Response> {
let response = get_public_rooms_filtered_helper( let response = get_public_rooms_filtered_helper(
&db, &db,
body.body.server.as_deref(), body.server.as_deref(),
body.body.limit, body.limit,
body.body.since.as_deref(), body.since.as_deref(),
None, // This is not used None, // This is not used
None, // This is not used None, // This is not used
) )
@ -119,7 +119,7 @@ pub async fn get_room_visibility_route(
pub async fn get_public_rooms_filtered_helper( pub async fn get_public_rooms_filtered_helper(
db: &Database, db: &Database,
server: Option<&str>, server: Option<&ServerName>,
limit: Option<js_int::UInt>, limit: Option<js_int::UInt>,
since: Option<&str>, since: Option<&str>,
_filter: Option<IncomingFilter>, _filter: Option<IncomingFilter>,
@ -131,7 +131,7 @@ pub async fn get_public_rooms_filtered_helper(
{ {
let response = server_server::send_request( let response = server_server::send_request(
&db, &db,
other_server.to_owned(), other_server,
federation::directory::get_public_rooms::v1::Request { federation::directory::get_public_rooms::v1::Request {
limit, limit,
since: since.as_deref(), since: since.as_deref(),

View File

@ -19,7 +19,7 @@ use ruma::{
}, },
events::pdu::Pdu, events::pdu::Pdu,
events::{room::member, EventType}, events::{room::member, EventType},
EventId, Raw, RoomId, RoomVersionId, UserId, EventId, Raw, RoomId, RoomVersionId, ServerName, UserId,
}; };
use state_res::StateEvent; use state_res::StateEvent;
use std::{ use std::{
@ -41,6 +41,7 @@ pub async fn join_room_by_id_route(
&db, &db,
body.sender_id.as_ref(), body.sender_id.as_ref(),
&body.room_id, &body.room_id,
&[body.room_id.server_name().to_owned()],
body.third_party_signed.as_ref(), body.third_party_signed.as_ref(),
) )
.await .await
@ -54,13 +55,12 @@ pub async fn join_room_by_id_or_alias_route(
db: State<'_, Database>, db: State<'_, Database>,
body: Ruma<join_room_by_id_or_alias::Request<'_>>, body: Ruma<join_room_by_id_or_alias::Request<'_>>,
) -> ConduitResult<join_room_by_id_or_alias::Response> { ) -> ConduitResult<join_room_by_id_or_alias::Response> {
let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) { let (servers, room_id) = match RoomId::try_from(body.room_id_or_alias.clone()) {
Ok(room_id) => room_id, Ok(room_id) => (vec![room_id.server_name().to_owned()], room_id),
Err(room_alias) => { Err(room_alias) => {
client_server::get_alias_helper(&db, &room_alias) let response = client_server::get_alias_helper(&db, &room_alias).await?;
.await?
.0 (response.0.servers, response.0.room_id)
.room_id
} }
}; };
@ -69,6 +69,7 @@ pub async fn join_room_by_id_or_alias_route(
&db, &db,
body.sender_id.as_ref(), body.sender_id.as_ref(),
&room_id, &room_id,
&servers,
body.third_party_signed.as_ref(), body.third_party_signed.as_ref(),
) )
.await? .await?
@ -415,22 +416,37 @@ async fn join_room_by_id_helper(
db: &Database, db: &Database,
sender_id: Option<&UserId>, sender_id: Option<&UserId>,
room_id: &RoomId, room_id: &RoomId,
servers: &[Box<ServerName>],
_third_party_signed: Option<&IncomingThirdPartySigned>, _third_party_signed: Option<&IncomingThirdPartySigned>,
) -> ConduitResult<join_room_by_id::Response> { ) -> ConduitResult<join_room_by_id::Response> {
let sender_id = sender_id.expect("user is authenticated"); let sender_id = sender_id.expect("user is authenticated");
// Ask a remote server if we don't have this room // Ask a remote server if we don't have this room
if !db.rooms.exists(&room_id)? && room_id.server_name() != db.globals.server_name() { if !db.rooms.exists(&room_id)? && room_id.server_name() != db.globals.server_name() {
let mut make_join_response_and_server = Err(Error::BadServerResponse(
"No server available to assist in joining.",
));
for remote_server in servers {
let make_join_response = server_server::send_request( let make_join_response = server_server::send_request(
&db, &db,
room_id.server_name().to_string(), remote_server,
federation::membership::create_join_event_template::v1::Request { federation::membership::create_join_event_template::v1::Request {
room_id, room_id,
user_id: sender_id, user_id: sender_id,
ver: &[RoomVersionId::Version5, RoomVersionId::Version6], ver: &[RoomVersionId::Version5, RoomVersionId::Version6],
}, },
) )
.await?; .await;
make_join_response_and_server = make_join_response.map(|r| (r, remote_server));
if make_join_response_and_server.is_ok() {
break;
}
}
let (make_join_response, remote_server) = make_join_response_and_server?;
let mut join_event_stub_value = let mut join_event_stub_value =
serde_json::from_str::<serde_json::Value>(make_join_response.event.json().get()) serde_json::from_str::<serde_json::Value>(make_join_response.event.json().get())
@ -475,7 +491,7 @@ async fn join_room_by_id_helper(
let send_join_response = server_server::send_request( let send_join_response = server_server::send_request(
&db, &db,
room_id.server_name().to_string(), remote_server,
federation::membership::create_join_event::v2::Request { federation::membership::create_join_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id: &event_id,

View File

@ -13,7 +13,7 @@ use ruma::{
}, },
OutgoingRequest, OutgoingRequest,
}, },
EventId, EventId, ServerName,
}; };
use serde_json::json; use serde_json::json;
use std::{ use std::{
@ -44,16 +44,16 @@ pub async fn request_well_known(db: &crate::Database, destination: &str) -> Opti
pub async fn send_request<T: OutgoingRequest>( pub async fn send_request<T: OutgoingRequest>(
db: &crate::Database, db: &crate::Database,
destination: String, destination: &ServerName,
request: T, request: T,
) -> Result<T::IncomingResponse> ) -> Result<T::IncomingResponse>
where where
T: Debug, T: Debug,
{ {
let actual_destination = "https://".to_owned() let actual_destination = "https://".to_owned()
+ &request_well_known(db, &destination) + &request_well_known(db, &destination.as_str())
.await .await
.unwrap_or(destination.clone() + ":8448"); .unwrap_or(destination.as_str().to_owned() + ":8448");
let mut http_request = request let mut http_request = request
.try_into_http_request(&actual_destination, Some("")) .try_into_http_request(&actual_destination, Some(""))
@ -82,7 +82,7 @@ where
"origin".to_owned(), "origin".to_owned(),
db.globals.server_name().as_str().into(), db.globals.server_name().as_str().into(),
); );
request_map.insert("destination".to_owned(), destination.into()); request_map.insert("destination".to_owned(), destination.as_str().into());
let mut request_json = request_map.into(); let mut request_json = request_map.into();
ruma::signatures::sign_json( ruma::signatures::sign_json(