Files
continuwuity/src/service/sending/mod.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

304 lines
8.8 KiB
Rust
Raw Normal View History

2024-05-31 00:14:22 +00:00
mod appservice;
mod data;
2024-08-03 07:16:39 +00:00
mod dest;
2024-05-31 00:14:22 +00:00
mod send;
mod sender;
2024-07-17 01:00:57 +00:00
use std::{fmt::Debug, sync::Arc};
2020-09-15 16:13:54 +02:00
use async_trait::async_trait;
use conduit::{
err,
utils::{ReadyExt, TryReadyExt},
warn, Result, Server,
};
use futures::{Stream, StreamExt};
2020-12-08 10:33:44 +01:00
use ruma::{
2024-04-24 13:01:49 -07:00
api::{appservice::Registration, OutgoingRequest},
2024-08-08 17:18:30 +00:00
RoomId, ServerName, UserId,
2020-12-08 10:33:44 +01:00
};
use tokio::sync::Mutex;
2024-08-03 07:16:39 +00:00
use self::data::Data;
pub use self::{
dest::Destination,
sender::{EDU_LIMIT, PDU_LIMIT},
};
use crate::{account_data, client, globals, presence, pusher, resolver, rooms, server_keys, users, Dep};
2024-05-09 15:59:08 -07:00
pub struct Service {
2024-07-17 01:00:57 +00:00
server: Arc<Server>,
2024-07-18 06:37:47 +00:00
services: Services,
2024-08-03 07:16:39 +00:00
pub db: Data,
2024-04-23 16:00:39 -07:00
sender: loole::Sender<Msg>,
receiver: Mutex<loole::Receiver<Msg>>,
}
2024-03-05 19:48:54 -05:00
2024-07-18 06:37:47 +00:00
struct Services {
client: Dep<client::Service>,
globals: Dep<globals::Service>,
resolver: Dep<resolver::Service>,
state: Dep<rooms::state::Service>,
state_cache: Dep<rooms::state_cache::Service>,
user: Dep<rooms::user::Service>,
users: Dep<users::Service>,
presence: Dep<presence::Service>,
read_receipt: Dep<rooms::read_receipt::Service>,
timeline: Dep<rooms::timeline::Service>,
account_data: Dep<account_data::Service>,
appservice: Dep<crate::appservice::Service>,
pusher: Dep<pusher::Service>,
server_keys: Dep<server_keys::Service>,
2024-07-18 06:37:47 +00:00
}
2024-04-23 16:00:39 -07:00
#[derive(Clone, Debug, PartialEq, Eq)]
struct Msg {
dest: Destination,
event: SendingEvent,
queue_id: Vec<u8>,
}
2024-03-02 20:55:02 -05:00
#[allow(clippy::module_name_repetitions)]
2024-04-23 16:00:39 -07:00
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2024-05-09 15:59:08 -07:00
pub enum SendingEvent {
2022-10-08 13:02:52 +02:00
Pdu(Vec<u8>), // pduid
Edu(Vec<u8>), // pdu json
Flush, // none
2021-05-12 20:04:28 +02:00
}
#[async_trait]
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
let (sender, receiver) = loole::unbounded();
Ok(Arc::new(Self {
server: args.server.clone(),
2024-07-18 06:37:47 +00:00
services: Services {
client: args.depend::<client::Service>("client"),
globals: args.depend::<globals::Service>("globals"),
resolver: args.depend::<resolver::Service>("resolver"),
state: args.depend::<rooms::state::Service>("rooms::state"),
state_cache: args.depend::<rooms::state_cache::Service>("rooms::state_cache"),
user: args.depend::<rooms::user::Service>("rooms::user"),
users: args.depend::<users::Service>("users"),
presence: args.depend::<presence::Service>("presence"),
read_receipt: args.depend::<rooms::read_receipt::Service>("rooms::read_receipt"),
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
account_data: args.depend::<account_data::Service>("account_data"),
appservice: args.depend::<crate::appservice::Service>("appservice"),
pusher: args.depend::<pusher::Service>("pusher"),
server_keys: args.depend::<server_keys::Service>("server_keys"),
2024-07-18 06:37:47 +00:00
},
2024-08-03 07:16:39 +00:00
db: Data::new(&args),
sender,
receiver: Mutex::new(receiver),
}))
}
async fn worker(self: Arc<Self>) -> Result<()> {
// trait impl can't be split between files so this just glues to mod sender
self.sender().await
}
fn interrupt(&self) {
if !self.sender.is_closed() {
self.sender.close();
}
}
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
2024-07-04 03:26:19 +00:00
impl Service {
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, pdu_id, user, pushkey), level = "debug")]
2024-05-09 15:59:08 -07:00
pub fn send_pdu_push(&self, pdu_id: &[u8], user: &UserId, pushkey: String) -> Result<()> {
2024-04-23 14:00:21 -07:00
let dest = Destination::Push(user.to_owned(), pushkey);
2024-04-23 16:00:39 -07:00
let event = SendingEvent::Pdu(pdu_id.to_owned());
2024-07-17 01:00:57 +00:00
let _cork = self.db.db.cork();
2024-08-08 17:18:30 +00:00
let keys = self.db.queue_requests(&[(&event, &dest)]);
2024-04-23 16:00:39 -07:00
self.dispatch(Msg {
dest,
event,
queue_id: keys.into_iter().next().expect("request queue key"),
})
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self), level = "debug")]
2024-05-09 15:59:08 -07:00
pub fn send_pdu_appservice(&self, appservice_id: String, pdu_id: Vec<u8>) -> Result<()> {
2024-04-23 14:00:21 -07:00
let dest = Destination::Appservice(appservice_id);
2024-04-23 16:00:39 -07:00
let event = SendingEvent::Pdu(pdu_id);
2024-07-17 01:00:57 +00:00
let _cork = self.db.db.cork();
2024-08-08 17:18:30 +00:00
let keys = self.db.queue_requests(&[(&event, &dest)]);
2024-04-23 16:00:39 -07:00
self.dispatch(Msg {
dest,
event,
queue_id: keys.into_iter().next().expect("request queue key"),
})
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, room_id, pdu_id), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn send_pdu_room(&self, room_id: &RoomId, pdu_id: &[u8]) -> Result<()> {
2024-07-18 06:37:47 +00:00
let servers = self
.services
.state_cache
.room_servers(room_id)
2024-08-08 17:18:30 +00:00
.ready_filter(|server_name| !self.services.globals.server_is_ours(server_name));
2024-08-08 17:18:30 +00:00
self.send_pdu_servers(servers, pdu_id).await
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, servers, pdu_id), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn send_pdu_servers<'a, S>(&self, servers: S, pdu_id: &[u8]) -> Result<()>
where
S: Stream<Item = &'a ServerName> + Send + 'a,
{
2024-07-17 01:00:57 +00:00
let _cork = self.db.db.cork();
2024-08-08 17:18:30 +00:00
let requests = servers
.map(|server| (Destination::Normal(server.into()), SendingEvent::Pdu(pdu_id.into())))
.collect::<Vec<_>>()
.await;
let keys = self
.db
.queue_requests(&requests.iter().map(|(o, e)| (e, o)).collect::<Vec<_>>());
2024-04-23 16:00:39 -07:00
for ((dest, event), queue_id) in requests.into_iter().zip(keys) {
self.dispatch(Msg {
dest,
event,
queue_id,
})?;
}
Ok(())
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, server, serialized), level = "debug")]
2024-05-09 15:59:08 -07:00
pub fn send_edu_server(&self, server: &ServerName, serialized: Vec<u8>) -> Result<()> {
2024-04-23 14:00:21 -07:00
let dest = Destination::Normal(server.to_owned());
2024-04-23 16:00:39 -07:00
let event = SendingEvent::Edu(serialized);
2024-07-17 01:00:57 +00:00
let _cork = self.db.db.cork();
2024-08-08 17:18:30 +00:00
let keys = self.db.queue_requests(&[(&event, &dest)]);
2024-04-23 16:00:39 -07:00
self.dispatch(Msg {
dest,
event,
queue_id: keys.into_iter().next().expect("request queue key"),
})
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, room_id, serialized), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn send_edu_room(&self, room_id: &RoomId, serialized: Vec<u8>) -> Result<()> {
2024-07-18 06:37:47 +00:00
let servers = self
.services
.state_cache
.room_servers(room_id)
2024-08-08 17:18:30 +00:00
.ready_filter(|server_name| !self.services.globals.server_is_ours(server_name));
2024-08-08 17:18:30 +00:00
self.send_edu_servers(servers, serialized).await
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, servers, serialized), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn send_edu_servers<'a, S>(&self, servers: S, serialized: Vec<u8>) -> Result<()>
where
S: Stream<Item = &'a ServerName> + Send + 'a,
{
2024-07-17 01:00:57 +00:00
let _cork = self.db.db.cork();
2024-08-08 17:18:30 +00:00
let requests = servers
.map(|server| (Destination::Normal(server.to_owned()), SendingEvent::Edu(serialized.clone())))
.collect::<Vec<_>>()
.await;
let keys = self
.db
.queue_requests(&requests.iter().map(|(o, e)| (e, o)).collect::<Vec<_>>());
2024-04-23 16:00:39 -07:00
for ((dest, event), queue_id) in requests.into_iter().zip(keys) {
self.dispatch(Msg {
dest,
event,
queue_id,
})?;
}
Ok(())
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, room_id), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn flush_room(&self, room_id: &RoomId) -> Result<()> {
2024-07-18 06:37:47 +00:00
let servers = self
.services
.state_cache
.room_servers(room_id)
2024-08-08 17:18:30 +00:00
.ready_filter(|server_name| !self.services.globals.server_is_ours(server_name));
2024-08-08 17:18:30 +00:00
self.flush_servers(servers).await
}
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self, servers), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn flush_servers<'a, S>(&self, servers: S) -> Result<()>
where
S: Stream<Item = &'a ServerName> + Send + 'a,
{
servers
.map(ToOwned::to_owned)
.map(Destination::Normal)
.map(Ok)
.ready_try_for_each(|dest| {
self.dispatch(Msg {
2024-08-08 17:18:30 +00:00
dest,
event: SendingEvent::Flush,
queue_id: Vec::<u8>::new(),
})
2024-08-08 17:18:30 +00:00
})
.await
}
/// Sends a request to a federation server
2024-07-18 06:37:47 +00:00
#[tracing::instrument(skip_all, name = "request")]
2024-05-09 15:59:08 -07:00
pub async fn send_federation_request<T>(&self, dest: &ServerName, request: T) -> Result<T::IncomingResponse>
where
2024-06-09 06:10:21 +00:00
T: OutgoingRequest + Debug + Send,
{
2024-07-18 06:37:47 +00:00
let client = &self.services.client.federation;
self.send(client, dest, request).await
}
/// Like send_federation_request() but with a very large timeout
#[tracing::instrument(skip_all, name = "synapse")]
pub async fn send_synapse_request<T>(&self, dest: &ServerName, request: T) -> Result<T::IncomingResponse>
where
T: OutgoingRequest + Debug + Send,
{
let client = &self.services.client.synapse;
self.send(client, dest, request).await
}
/// Sends a request to an appservice
///
/// Only returns None if there is no url specified in the appservice
/// registration file
2024-05-09 15:59:08 -07:00
pub async fn send_appservice_request<T>(
&self, registration: Registration, request: T,
) -> Result<Option<T::IncomingResponse>>
where
2024-06-09 06:10:21 +00:00
T: OutgoingRequest + Debug + Send,
{
2024-07-18 06:37:47 +00:00
let client = &self.services.client.appservice;
appservice::send_request(client, registration, request).await
}
2024-04-23 16:00:39 -07:00
/// Cleanup event data
/// Used for instance after we remove an appservice registration
2024-07-07 19:03:15 +00:00
#[tracing::instrument(skip(self), level = "debug")]
2024-08-08 17:18:30 +00:00
pub async fn cleanup_events(&self, appservice_id: String) {
2024-04-23 16:00:39 -07:00
self.db
2024-08-08 17:18:30 +00:00
.delete_all_requests_for(&Destination::Appservice(appservice_id))
.await;
2024-04-23 16:00:39 -07:00
}
fn dispatch(&self, msg: Msg) -> Result<()> {
debug_assert!(!self.sender.is_full(), "channel full");
debug_assert!(!self.sender.is_closed(), "channel closed");
self.sender.send(msg).map_err(|e| err!("{e}"))
2024-04-23 16:00:39 -07:00
}
}