1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/service/sending/mod.rs

758 lines
28 KiB
Rust
Raw Normal View History

2022-10-08 13:02:52 +02:00
mod data;
pub use data::Data;
2020-12-19 16:00:11 +01:00
use std::{
collections::{BTreeMap, HashMap, HashSet},
2021-02-11 13:16:14 +01:00
fmt::Debug,
2020-12-19 16:00:11 +01:00
sync::Arc,
2022-10-08 13:03:07 +02:00
time::{Duration, Instant},
2020-12-19 16:00:11 +01:00
};
2020-09-15 16:13:54 +02:00
use crate::{
2022-10-05 20:34:31 +02:00
api::{appservice_server, server_server},
services,
utils::calculate_hash,
2022-10-08 13:03:07 +02:00
Config, Error, PduEvent, Result,
};
use federation::transactions::send_transaction_message;
2022-01-20 11:51:31 +01:00
use futures_util::{stream::FuturesUnordered, StreamExt};
2022-10-05 20:41:05 +02:00
use base64::{engine::general_purpose, Engine as _};
use ruma::{
api::{
appservice::{self, Registration},
federation::{
self,
transactions::edu::{
DeviceListUpdateContent, Edu, PresenceContent, PresenceUpdate, ReceiptContent,
ReceiptData, ReceiptMap,
},
},
OutgoingRequest,
},
device_id,
2022-10-09 17:25:06 +02:00
events::{
push_rules::PushRulesEvent, receipt::ReceiptType, AnySyncEphemeralRoomEvent,
GlobalAccountDataEventType,
},
push, uint, MilliSecondsSinceUnixEpoch, OwnedServerName, OwnedUserId, ServerName, UInt, UserId,
};
2021-07-14 07:07:08 +00:00
use tokio::{
select,
sync::{mpsc, Mutex, Semaphore},
2021-07-14 07:07:08 +00:00
};
2023-03-13 08:27:59 +01:00
use tracing::{debug, error, warn};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum OutgoingKind {
Appservice(String),
2022-10-09 17:25:06 +02:00
Push(OwnedUserId, String), // user and pushkey
Normal(OwnedServerName),
}
impl OutgoingKind {
#[tracing::instrument(skip(self))]
pub fn get_prefix(&self) -> Vec<u8> {
let mut prefix = match self {
OutgoingKind::Appservice(server) => {
let mut p = b"+".to_vec();
p.extend_from_slice(server.as_bytes());
p
}
OutgoingKind::Push(user, pushkey) => {
let mut p = b"$".to_vec();
2022-10-08 13:02:52 +02:00
p.extend_from_slice(user.as_bytes());
p.push(0xff);
2022-10-08 13:02:52 +02:00
p.extend_from_slice(pushkey.as_bytes());
p
}
OutgoingKind::Normal(server) => {
let mut p = Vec::new();
p.extend_from_slice(server.as_bytes());
p
}
};
prefix.push(0xff);
prefix
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SendingEventType {
2022-10-08 13:02:52 +02:00
Pdu(Vec<u8>), // pduid
Edu(Vec<u8>), // pdu json
}
2022-09-07 13:25:51 +02:00
pub struct Service {
2022-10-08 13:02:52 +02:00
db: &'static dyn Data,
2020-09-15 16:13:54 +02:00
/// The state for a given state hash.
2020-12-19 16:00:11 +01:00
pub(super) maximum_requests: Arc<Semaphore>,
2022-10-08 13:02:52 +02:00
pub sender: mpsc::UnboundedSender<(OutgoingKind, SendingEventType, Vec<u8>)>,
receiver: Mutex<mpsc::UnboundedReceiver<(OutgoingKind, SendingEventType, Vec<u8>)>>,
2020-09-15 16:13:54 +02:00
}
2021-04-24 18:01:05 +02:00
enum TransactionStatus {
Running,
Failed(u32, Instant), // number of times failed, time of last failure
Retrying(u32), // number of times failed
}
2022-09-07 13:25:51 +02:00
impl Service {
2022-10-08 13:02:52 +02:00
pub fn build(db: &'static dyn Data, config: &Config) -> Arc<Self> {
let (sender, receiver) = mpsc::unbounded_channel();
Arc::new(Self {
2022-10-08 13:03:07 +02:00
db,
sender,
receiver: Mutex::new(receiver),
2022-10-08 13:03:07 +02:00
maximum_requests: Arc::new(Semaphore::new(config.max_concurrent_requests as usize)),
})
}
2022-10-08 13:02:52 +02:00
pub fn start_handler(self: &Arc<Self>) {
2022-10-10 14:09:11 +02:00
let self2 = Arc::clone(self);
2020-09-15 16:13:54 +02:00
tokio::spawn(async move {
self2.handler().await.unwrap();
2022-10-08 13:02:52 +02:00
});
}
async fn handler(&self) -> Result<()> {
let mut receiver = self.receiver.lock().await;
2022-10-08 13:02:52 +02:00
let mut futures = FuturesUnordered::new();
2021-07-14 07:07:08 +00:00
2022-10-08 13:02:52 +02:00
let mut current_transaction_status = HashMap::<OutgoingKind, TransactionStatus>::new();
2021-02-26 13:24:07 +01:00
2022-10-08 13:02:52 +02:00
// Retry requests we could not finish yet
let mut initial_transactions = HashMap::<OutgoingKind, Vec<SendingEventType>>::new();
2022-10-08 13:03:07 +02:00
for (key, outgoing_kind, event) in self.db.active_requests().filter_map(|r| r.ok()) {
2022-10-08 13:02:52 +02:00
let entry = initial_transactions
.entry(outgoing_kind.clone())
.or_default();
2022-10-08 13:02:52 +02:00
if entry.len() > 30 {
warn!(
"Dropping some current events: {:?} {:?} {:?}",
key, outgoing_kind, event
);
self.db.delete_active_request(key)?;
continue;
}
2022-10-08 13:02:52 +02:00
entry.push(event);
}
for (outgoing_kind, events) in initial_transactions {
2022-10-08 13:03:07 +02:00
current_transaction_status.insert(outgoing_kind.clone(), TransactionStatus::Running);
2022-10-08 13:02:52 +02:00
futures.push(Self::handle_events(outgoing_kind.clone(), events));
}
loop {
select! {
Some(response) = futures.next() => {
match response {
Ok(outgoing_kind) => {
self.db.delete_all_active_requests_for(&outgoing_kind)?;
// Find events that have been added since starting the last request
let new_events = self.db.queued_requests(&outgoing_kind).filter_map(|r| r.ok()).take(30).collect::<Vec<_>>();
if !new_events.is_empty() {
// Insert pdus we found
self.db.mark_as_active(&new_events)?;
futures.push(
Self::handle_events(
outgoing_kind.clone(),
new_events.into_iter().map(|(event, _)| event).collect(),
)
);
} else {
current_transaction_status.remove(&outgoing_kind);
2020-09-15 16:13:54 +02:00
}
}
2022-10-08 13:02:52 +02:00
Err((outgoing_kind, _)) => {
current_transaction_status.entry(outgoing_kind).and_modify(|e| *e = match e {
TransactionStatus::Running => TransactionStatus::Failed(1, Instant::now()),
TransactionStatus::Retrying(n) => TransactionStatus::Failed(*n+1, Instant::now()),
TransactionStatus::Failed(_, _) => {
error!("Request that was not even running failed?!");
return
},
});
}
};
},
Some((outgoing_kind, event, key)) = receiver.recv() => {
if let Ok(Some(events)) = self.select_events(
&outgoing_kind,
vec![(event, key)],
&mut current_transaction_status,
) {
futures.push(Self::handle_events(outgoing_kind, events));
}
2020-09-15 16:13:54 +02:00
}
}
2022-10-08 13:02:52 +02:00
}
2020-09-15 16:13:54 +02:00
}
2022-10-08 13:02:52 +02:00
#[tracing::instrument(skip(self, outgoing_kind, new_events, current_transaction_status))]
fn select_events(
2022-10-08 13:02:52 +02:00
&self,
outgoing_kind: &OutgoingKind,
2021-06-08 18:10:00 +02:00
new_events: Vec<(SendingEventType, Vec<u8>)>, // Events we want to send: event and full key
2022-10-08 13:02:52 +02:00
current_transaction_status: &mut HashMap<OutgoingKind, TransactionStatus>,
2021-06-08 18:10:00 +02:00
) -> Result<Option<Vec<SendingEventType>>> {
let mut retry = false;
let mut allow = true;
2022-10-08 13:02:52 +02:00
let entry = current_transaction_status.entry(outgoing_kind.clone());
entry
.and_modify(|e| match e {
TransactionStatus::Running | TransactionStatus::Retrying(_) => {
allow = false; // already running
}
TransactionStatus::Failed(tries, time) => {
// Fail if a request has failed recently (exponential backoff)
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
}
if time.elapsed() < min_elapsed_duration {
allow = false;
} else {
retry = true;
*e = TransactionStatus::Retrying(*tries);
}
}
})
.or_insert(TransactionStatus::Running);
if !allow {
2021-06-08 18:10:00 +02:00
return Ok(None);
}
let mut events = Vec::new();
if retry {
// We retry the previous transaction
2022-10-08 13:03:07 +02:00
for (_, e) in self
.db
.active_requests_for(outgoing_kind)
.filter_map(|r| r.ok())
{
2022-10-08 13:02:52 +02:00
events.push(e);
}
} else {
2022-10-08 13:02:52 +02:00
self.db.mark_as_active(&new_events)?;
for (e, _) in new_events {
events.push(e);
}
if let OutgoingKind::Normal(server_name) = outgoing_kind {
2022-10-08 13:02:52 +02:00
if let Ok((select_edus, last_count)) = self.select_edus(server_name) {
events.extend(select_edus.into_iter().map(SendingEventType::Edu));
2022-10-08 13:02:52 +02:00
self.db.set_latest_educount(server_name, last_count)?;
}
}
}
2021-06-08 18:10:00 +02:00
Ok(Some(events))
}
2022-10-08 13:02:52 +02:00
#[tracing::instrument(skip(self, server_name))]
pub fn select_edus(&self, server_name: &ServerName) -> Result<(Vec<Vec<u8>>, u64)> {
// u64: count of last edu
2022-10-08 13:02:52 +02:00
let since = self.db.get_latest_educount(server_name)?;
let mut events = Vec::new();
let mut max_edu_count = since;
let mut device_list_changes = HashSet::new();
2022-10-08 13:02:52 +02:00
'outer: for room_id in services().rooms.state_cache.server_rooms(server_name) {
let room_id = room_id?;
// Look for device list updates in this room
device_list_changes.extend(
2022-10-05 20:34:31 +02:00
services()
.users
2022-10-10 14:09:11 +02:00
.keys_changed(room_id.as_ref(), since, None)
.filter_map(|r| r.ok())
2022-09-07 13:25:51 +02:00
.filter(|user_id| user_id.server_name() == services().globals.server_name()),
);
if services().globals.allow_outcoming_presence() {
// Look for presence updates in this room
let mut presence_updates = Vec::new();
for presence_data in services()
.rooms
.edus
.presence
.presence_since(&room_id, since)
{
let (user_id, count, presence_event) = presence_data?;
if count > max_edu_count {
max_edu_count = count;
}
if user_id.server_name() != services().globals.server_name() {
continue;
}
presence_updates.push(PresenceUpdate {
user_id,
presence: presence_event.content.presence,
currently_active: presence_event.content.currently_active.unwrap_or(false),
last_active_ago: presence_event.content.last_active_ago.unwrap_or(uint!(0)),
status_msg: presence_event.content.status_msg,
});
}
let presence_content = Edu::Presence(PresenceContent::new(presence_updates));
events.push(
serde_json::to_vec(&presence_content).expect("PresenceEvent can be serialized"),
);
}
// Look for read receipts in this room
2022-10-08 13:03:07 +02:00
for r in services()
.rooms
.edus
.read_receipt
.readreceipts_since(&room_id, since)
{
let (user_id, count, read_receipt) = r?;
if count > max_edu_count {
max_edu_count = count;
}
2022-09-07 13:25:51 +02:00
if user_id.server_name() != services().globals.server_name() {
continue;
}
let event: AnySyncEphemeralRoomEvent =
serde_json::from_str(read_receipt.json().get())
.map_err(|_| Error::bad_database("Invalid edu event in read_receipts."))?;
let federation_event = match event {
AnySyncEphemeralRoomEvent::Receipt(r) => {
let mut read = BTreeMap::new();
let (event_id, mut receipt) = r
.content
.0
.into_iter()
.next()
.expect("we only use one event per read receipt");
let receipt = receipt
.remove(&ReceiptType::Read)
.expect("our read receipts always set this")
.remove(&user_id)
.expect("our read receipts always have the user here");
read.insert(
user_id,
ReceiptData {
data: receipt.clone(),
event_ids: vec![event_id.clone()],
},
);
let receipt_map = ReceiptMap { read };
let mut receipts = BTreeMap::new();
receipts.insert(room_id.clone(), receipt_map);
Edu::Receipt(ReceiptContent { receipts })
}
_ => {
Error::bad_database("Invalid event type in read_receipts");
continue;
}
};
events.push(serde_json::to_vec(&federation_event).expect("json can be serialized"));
if events.len() >= 20 {
break 'outer;
}
}
}
for user_id in device_list_changes {
// Empty prev id forces synapse to resync: https://github.com/matrix-org/synapse/blob/98aec1cc9da2bd6b8e34ffb282c85abf9b8b42ca/synapse/handlers/device.py#L767
// Because synapse resyncs, we can just insert dummy data
let edu = Edu::DeviceListUpdate(DeviceListUpdateContent {
user_id,
2021-11-26 20:36:40 +01:00
device_id: device_id!("dummy").to_owned(),
device_display_name: Some("Dummy".to_owned()),
stream_id: uint!(1),
prev_id: Vec::new(),
deleted: None,
keys: None,
});
events.push(serde_json::to_vec(&edu).expect("json can be serialized"));
}
Ok((events, max_edu_count))
}
2022-10-08 13:02:52 +02:00
#[tracing::instrument(skip(self, pdu_id, user, pushkey))]
pub fn send_push_pdu(&self, pdu_id: &[u8], user: &UserId, pushkey: String) -> Result<()> {
let outgoing_kind = OutgoingKind::Push(user.to_owned(), pushkey);
let event = SendingEventType::Pdu(pdu_id.to_owned());
let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?;
2022-10-08 13:03:07 +02:00
self.sender
.send((outgoing_kind, event, keys.into_iter().next().unwrap()))
.unwrap();
Ok(())
}
#[tracing::instrument(skip(self, servers, pdu_id))]
2022-10-09 17:25:06 +02:00
pub fn send_pdu<I: Iterator<Item = OwnedServerName>>(
&self,
servers: I,
pdu_id: &[u8],
) -> Result<()> {
2022-10-08 13:03:07 +02:00
let requests = servers
.into_iter()
.map(|server| {
(
OutgoingKind::Normal(server),
SendingEventType::Pdu(pdu_id.to_owned()),
)
})
.collect::<Vec<_>>();
let keys = self.db.queue_requests(
&requests
.iter()
.map(|(o, e)| (o, e.clone()))
.collect::<Vec<_>>(),
)?;
2022-10-08 13:02:52 +02:00
for ((outgoing_kind, event), key) in requests.into_iter().zip(keys) {
2022-10-08 13:03:07 +02:00
self.sender
.send((outgoing_kind.to_owned(), event, key))
.unwrap();
2022-10-08 13:02:52 +02:00
}
Ok(())
}
#[tracing::instrument(skip(self, server, serialized))]
pub fn send_reliable_edu(
&self,
server: &ServerName,
serialized: Vec<u8>,
id: u64,
) -> Result<()> {
2022-10-08 13:02:52 +02:00
let outgoing_kind = OutgoingKind::Normal(server.to_owned());
let event = SendingEventType::Edu(serialized);
let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?;
2022-10-08 13:03:07 +02:00
self.sender
.send((outgoing_kind, event, keys.into_iter().next().unwrap()))
.unwrap();
2020-09-15 16:13:54 +02:00
Ok(())
}
2021-02-28 12:41:03 +01:00
#[tracing::instrument(skip(self))]
2022-10-08 13:02:52 +02:00
pub fn send_pdu_appservice(&self, appservice_id: String, pdu_id: Vec<u8>) -> Result<()> {
let outgoing_kind = OutgoingKind::Appservice(appservice_id);
let event = SendingEventType::Pdu(pdu_id);
let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?;
2022-10-08 13:03:07 +02:00
self.sender
.send((outgoing_kind, event, keys.into_iter().next().unwrap()))
.unwrap();
Ok(())
}
/// Cleanup event data
/// Used for instance after we remove an appservice registration
///
#[tracing::instrument(skip(self))]
2022-10-08 13:02:52 +02:00
pub fn cleanup_events(&self, appservice_id: String) -> Result<()> {
2022-10-08 13:03:07 +02:00
self.db
.delete_all_requests_for(&OutgoingKind::Appservice(appservice_id))?;
Ok(())
}
2022-09-07 13:25:51 +02:00
#[tracing::instrument(skip(events, kind))]
async fn handle_events(
kind: OutgoingKind,
events: Vec<SendingEventType>,
) -> Result<OutgoingKind, (OutgoingKind, Error)> {
2021-03-16 18:00:26 +01:00
match &kind {
OutgoingKind::Appservice(id) => {
let mut pdu_jsons = Vec::new();
for event in &events {
match event {
SendingEventType::Pdu(pdu_id) => {
2022-10-08 13:02:52 +02:00
pdu_jsons.push(services().rooms.timeline
.get_pdu_from_id(pdu_id)
.map_err(|e| (kind.clone(), e))?
.ok_or_else(|| {
(
kind.clone(),
Error::bad_database(
2021-07-29 20:17:47 +02:00
"[Appservice] Event in servernameevent_data not found in db.",
),
)
})?
.to_room_event())
}
SendingEventType::Edu(_) => {
// Appservices don't need EDUs (?)
}
}
}
2022-09-07 13:25:51 +02:00
let permit = services().sending.maximum_requests.acquire().await;
let response = match appservice_server::send_request(
2022-10-05 20:34:31 +02:00
services()
.appservice
2022-10-10 14:09:11 +02:00
.get_registration(id)
.await
.ok_or_else(|| {
(
kind.clone(),
Error::bad_database(
"[Appservice] Could not load registration from db.",
),
)
})?,
appservice::event::push_events::v1::Request {
2022-12-14 13:09:10 +01:00
events: pdu_jsons,
txn_id: (&*general_purpose::URL_SAFE_NO_PAD.encode(calculate_hash(
&events
.iter()
.map(|e| match e {
SendingEventType::Edu(b) | SendingEventType::Pdu(b) => &**b,
})
.collect::<Vec<_>>(),
)))
2022-01-17 14:39:37 +01:00
.into(),
},
)
.await
{
Ok(_) => Ok(kind.clone()),
Err(e) => Err((kind.clone(), e)),
};
2021-02-26 13:24:07 +01:00
drop(permit);
2021-02-26 13:24:07 +01:00
response
}
2022-10-08 13:02:52 +02:00
OutgoingKind::Push(userid, pushkey) => {
let mut pdus = Vec::new();
for event in &events {
match event {
SendingEventType::Pdu(pdu_id) => {
pdus.push(
2022-09-07 13:25:51 +02:00
services().rooms
2022-10-08 13:02:52 +02:00
.timeline
.get_pdu_from_id(pdu_id)
.map_err(|e| (kind.clone(), e))?
.ok_or_else(|| {
(
kind.clone(),
Error::bad_database(
2021-07-29 20:17:47 +02:00
"[Push] Event in servernamevent_datas not found in db.",
),
)
})?,
);
}
SendingEventType::Edu(_) => {
// Push gateways don't need EDUs (?)
}
}
}
2021-03-16 18:00:26 +01:00
for pdu in pdus {
// Redacted events are not notification targets (we don't send push for them)
if let Some(unsigned) = &pdu.unsigned {
if let Ok(unsigned) =
serde_json::from_str::<serde_json::Value>(unsigned.get())
{
if unsigned.get("redacted_because").is_some() {
continue;
}
}
}
2022-09-07 13:25:51 +02:00
let pusher = match services()
.pusher
2022-10-10 14:09:11 +02:00
.get_pusher(userid, pushkey)
2022-10-08 13:02:52 +02:00
.map_err(|e| (OutgoingKind::Push(userid.clone(), pushkey.clone()), e))?
{
Some(pusher) => pusher,
None => continue,
};
2022-09-07 13:25:51 +02:00
let rules_for_user = services()
.account_data
2022-04-06 21:31:29 +02:00
.get(
None,
2022-10-10 14:09:11 +02:00
userid,
2022-04-06 21:31:29 +02:00
GlobalAccountDataEventType::PushRules.to_string().into(),
)
.unwrap_or_default()
2022-10-08 13:02:52 +02:00
.and_then(|event| serde_json::from_str::<PushRulesEvent>(event.get()).ok())
.map(|ev: PushRulesEvent| ev.content.global)
2022-10-10 14:09:11 +02:00
.unwrap_or_else(|| push::Ruleset::server_default(userid));
2022-09-07 13:25:51 +02:00
let unread: UInt = services()
.rooms
2022-10-08 13:02:52 +02:00
.user
2022-10-10 14:09:11 +02:00
.notification_count(userid, &pdu.room_id)
.map_err(|e| (kind.clone(), e))?
.try_into()
.expect("notification count can't go that high");
2022-09-07 13:25:51 +02:00
let permit = services().sending.maximum_requests.acquire().await;
2022-10-05 20:34:31 +02:00
let _response = services()
.pusher
2022-10-10 14:09:11 +02:00
.send_push_notice(userid, unread, &pusher, rules_for_user, &pdu)
2022-10-05 20:34:31 +02:00
.await
.map(|_response| kind.clone())
.map_err(|e| (kind.clone(), e));
drop(permit);
}
2022-10-08 13:02:52 +02:00
Ok(OutgoingKind::Push(userid.clone(), pushkey.clone()))
}
OutgoingKind::Normal(server) => {
let mut edu_jsons = Vec::new();
let mut pdu_jsons = Vec::new();
for event in &events {
match event {
SendingEventType::Pdu(pdu_id) => {
// TODO: check room version and remove event_id if needed
2021-07-30 18:05:26 +02:00
let raw = PduEvent::convert_to_outgoing_federation_event(
2022-09-07 13:25:51 +02:00
services().rooms
2022-10-08 13:02:52 +02:00
.timeline
.get_pdu_json_from_id(pdu_id)
2021-07-30 18:05:26 +02:00
.map_err(|e| (OutgoingKind::Normal(server.clone()), e))?
.ok_or_else(|| {
error!("event not found: {server} {pdu_id:?}");
2021-07-30 18:05:26 +02:00
(
OutgoingKind::Normal(server.clone()),
Error::bad_database(
"[Normal] Event in servernamevent_datas not found in db.",
),
)
})?,
);
pdu_jsons.push(raw);
}
SendingEventType::Edu(edu) => {
2021-07-30 18:05:26 +02:00
if let Ok(raw) = serde_json::from_slice(edu) {
edu_jsons.push(raw);
}
}
}
}
2022-09-07 13:25:51 +02:00
let permit = services().sending.maximum_requests.acquire().await;
let response = server_server::send_request(
2022-10-10 14:09:11 +02:00
server,
send_transaction_message::v1::Request {
2022-12-14 13:09:10 +01:00
origin: services().globals.server_name().to_owned(),
pdus: pdu_jsons,
edus: edu_jsons,
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
transaction_id: (&*general_purpose::URL_SAFE_NO_PAD.encode(
2022-10-05 20:33:55 +02:00
calculate_hash(
&events
.iter()
.map(|e| match e {
SendingEventType::Edu(b) | SendingEventType::Pdu(b) => &**b,
})
.collect::<Vec<_>>(),
),
2022-01-17 14:39:37 +01:00
))
.into(),
},
)
.await
.map(|response| {
2021-03-26 11:10:45 +01:00
for pdu in response.pdus {
if pdu.1.is_err() {
warn!("Failed to send to {}: {:?}", server, pdu);
}
}
kind.clone()
})
.map_err(|e| (kind, e));
2021-02-26 13:24:07 +01:00
drop(permit);
2021-02-26 13:24:07 +01:00
response
}
}
}
2020-12-19 16:00:11 +01:00
2022-09-07 13:25:51 +02:00
#[tracing::instrument(skip(self, destination, request))]
pub async fn send_federation_request<T>(
2020-12-19 16:00:11 +01:00
&self,
destination: &ServerName,
2020-12-19 16:00:11 +01:00
request: T,
) -> Result<T::IncomingResponse>
where
T: OutgoingRequest + Debug,
2020-12-19 16:00:11 +01:00
{
2023-03-13 08:27:59 +01:00
debug!("Waiting for permit");
2020-12-19 16:00:11 +01:00
let permit = self.maximum_requests.acquire().await;
2023-03-13 08:27:59 +01:00
debug!("Got permit");
let response = tokio::time::timeout(
Duration::from_secs(2 * 60),
server_server::send_request(destination, request),
)
.await
.map_err(|_| {
warn!("Timeout waiting for server response of {destination}");
Error::BadServerResponse("Timeout waiting for server response")
})?;
2020-12-19 16:00:11 +01:00
drop(permit);
response
}
/// Sends a request to an appservice
///
/// Only returns None if there is no url specified in the appservice registration file
2022-09-07 13:25:51 +02:00
#[tracing::instrument(skip(self, registration, request))]
pub async fn send_appservice_request<T>(
2020-12-19 16:00:11 +01:00
&self,
registration: Registration,
2020-12-19 16:00:11 +01:00
request: T,
) -> Result<Option<T::IncomingResponse>>
2020-12-19 16:00:11 +01:00
where
T: OutgoingRequest + Debug,
2020-12-19 16:00:11 +01:00
{
let permit = self.maximum_requests.acquire().await;
2022-09-07 13:25:51 +02:00
let response = appservice_server::send_request(registration, request).await;
2020-12-19 16:00:11 +01:00
drop(permit);
response
}
2020-09-15 16:13:54 +02:00
}