feat: add threadpool for iterator threads, bug fixes, tracing_flame support
This commit is contained in:
parent
e0072eff63
commit
5e924227b6
26 changed files with 472 additions and 228 deletions
|
@ -1,6 +1,5 @@
|
|||
use crate::{utils, Error, Result};
|
||||
use bytes::BytesMut;
|
||||
use log::warn;
|
||||
use ruma::api::{IncomingResponse, OutgoingRequest, SendAccessToken};
|
||||
use std::{
|
||||
convert::{TryFrom, TryInto},
|
||||
|
@ -8,6 +7,7 @@ use std::{
|
|||
mem,
|
||||
time::Duration,
|
||||
};
|
||||
use tracing::warn;
|
||||
|
||||
pub async fn send_request<T: OutgoingRequest>(
|
||||
globals: &crate::database::globals::Globals,
|
||||
|
|
|
@ -6,7 +6,6 @@ use std::{
|
|||
|
||||
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
||||
use log::info;
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
|
@ -28,6 +27,7 @@ use ruma::{
|
|||
identifiers::RoomName,
|
||||
push, RoomAliasId, RoomId, RoomVersionId, UserId,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
use register::RegistrationKind;
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma};
|
||||
use log::info;
|
||||
use ruma::{
|
||||
api::{
|
||||
client::{
|
||||
|
@ -22,6 +21,7 @@ use ruma::{
|
|||
serde::Raw,
|
||||
ServerName, UInt,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post, put};
|
||||
|
|
|
@ -4,7 +4,6 @@ use crate::{
|
|||
pdu::{PduBuilder, PduEvent},
|
||||
server_server, utils, ConduitResult, Database, Error, Result, Ruma,
|
||||
};
|
||||
use log::{debug, error, warn};
|
||||
use member::{MemberEventContent, MembershipState};
|
||||
use rocket::futures;
|
||||
use ruma::{
|
||||
|
@ -34,6 +33,7 @@ use std::{
|
|||
sync::{Arc, RwLock},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
|
|
@ -2,7 +2,6 @@ use crate::{
|
|||
client_server::invite_helper, database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Error,
|
||||
Ruma,
|
||||
};
|
||||
use log::info;
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
|
@ -16,6 +15,7 @@ use ruma::{
|
|||
RoomAliasId, RoomId, RoomVersionId,
|
||||
};
|
||||
use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc};
|
||||
use tracing::info;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Error, Ruma};
|
||||
use log::info;
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
|
@ -9,6 +8,7 @@ use ruma::{
|
|||
UserId,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Claims {
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
|
||||
use log::{error, warn};
|
||||
use ruma::{
|
||||
api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
|
||||
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType},
|
||||
|
@ -13,6 +12,7 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
use tokio::sync::watch::Sender;
|
||||
use tracing::{error, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, tokio};
|
||||
|
|
|
@ -17,7 +17,6 @@ pub mod users;
|
|||
use crate::{utils, Error, Result};
|
||||
use abstraction::DatabaseEngine;
|
||||
use directories::ProjectDirs;
|
||||
use log::error;
|
||||
use lru_cache::LruCache;
|
||||
use rocket::{
|
||||
futures::{channel::mpsc, stream::FuturesUnordered, StreamExt},
|
||||
|
@ -36,6 +35,7 @@ use std::{
|
|||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
use tokio::sync::{OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use self::proxy::ProxyConfig;
|
||||
|
||||
|
@ -69,6 +69,8 @@ pub struct Config {
|
|||
allow_federation: bool,
|
||||
#[serde(default = "false_fn")]
|
||||
pub allow_jaeger: bool,
|
||||
#[serde(default = "false_fn")]
|
||||
pub tracing_flame: bool,
|
||||
#[serde(default)]
|
||||
proxy: ProxyConfig,
|
||||
jwt_secret: Option<String>,
|
||||
|
@ -91,12 +93,12 @@ impl Config {
|
|||
.keys()
|
||||
.filter(|key| DEPRECATED_KEYS.iter().any(|s| s == key))
|
||||
{
|
||||
log::warn!("Config parameter {} is deprecated", key);
|
||||
warn!("Config parameter {} is deprecated", key);
|
||||
was_deprecated = true;
|
||||
}
|
||||
|
||||
if was_deprecated {
|
||||
log::warn!("Read conduit documentation and check your configuration if any new configuration parameters should be adjusted");
|
||||
warn!("Read conduit documentation and check your configuration if any new configuration parameters should be adjusted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -193,13 +195,13 @@ impl Database {
|
|||
if sled_exists {
|
||||
if sqlite_exists {
|
||||
// most likely an in-place directory, only warn
|
||||
log::warn!("Both sled and sqlite databases are detected in database directory");
|
||||
log::warn!("Currently running from the sqlite database, but consider removing sled database files to free up space")
|
||||
warn!("Both sled and sqlite databases are detected in database directory");
|
||||
warn!("Currently running from the sqlite database, but consider removing sled database files to free up space")
|
||||
} else {
|
||||
log::error!(
|
||||
error!(
|
||||
"Sled database detected, conduit now uses sqlite for database operations"
|
||||
);
|
||||
log::error!("This database must be converted to sqlite, go to https://github.com/ShadowJonathan/conduit_toolbox#conduit_sled_to_sqlite");
|
||||
error!("This database must be converted to sqlite, go to https://github.com/ShadowJonathan/conduit_toolbox#conduit_sled_to_sqlite");
|
||||
return Err(Error::bad_config(
|
||||
"sled database detected, migrate to sqlite",
|
||||
));
|
||||
|
@ -291,7 +293,7 @@ impl Database {
|
|||
statehash_shortstatehash: builder.open_tree("statehash_shortstatehash")?,
|
||||
|
||||
eventid_outlierpdu: builder.open_tree("eventid_outlierpdu")?,
|
||||
prevevent_parent: builder.open_tree("prevevent_parent")?,
|
||||
referencedevents: builder.open_tree("referencedevents")?,
|
||||
pdu_cache: Mutex::new(LruCache::new(100_000)),
|
||||
auth_chain_cache: Mutex::new(LruCache::new(100_000)),
|
||||
},
|
||||
|
@ -444,10 +446,12 @@ impl Database {
|
|||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
pub async fn start_on_shutdown_tasks(db: Arc<TokioRwLock<Self>>, shutdown: Shutdown) {
|
||||
use tracing::info;
|
||||
|
||||
tokio::spawn(async move {
|
||||
shutdown.await;
|
||||
|
||||
log::info!(target: "shutdown-sync", "Received shutdown notification, notifying sync helpers...");
|
||||
info!(target: "shutdown-sync", "Received shutdown notification, notifying sync helpers...");
|
||||
|
||||
db.read().await.globals.rotate.fire();
|
||||
});
|
||||
|
@ -543,22 +547,25 @@ impl Database {
|
|||
futures.next().await;
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub async fn flush(&self) -> Result<()> {
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let res = self._db.flush();
|
||||
|
||||
log::debug!("flush: took {:?}", start.elapsed());
|
||||
debug!("flush: took {:?}", start.elapsed());
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn flush_wal(&self) -> Result<()> {
|
||||
self._db.flush_wal()
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[tracing::instrument(skip(engine, config))]
|
||||
pub async fn start_spillover_reap_task(engine: Arc<Engine>, config: &Config) {
|
||||
let fraction = config.sqlite_spillover_reap_fraction.clamp(0.01, 1.0);
|
||||
let interval_secs = config.sqlite_spillover_reap_interval_secs as u64;
|
||||
|
@ -585,11 +592,13 @@ impl Database {
|
|||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
#[tracing::instrument(skip(lock, config))]
|
||||
pub async fn start_wal_clean_task(lock: &Arc<TokioRwLock<Self>>, config: &Config) {
|
||||
use tokio::time::{interval, timeout};
|
||||
|
||||
#[cfg(unix)]
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
use tracing::info;
|
||||
|
||||
use std::{
|
||||
sync::Weak,
|
||||
|
@ -611,41 +620,41 @@ impl Database {
|
|||
#[cfg(unix)]
|
||||
tokio::select! {
|
||||
_ = i.tick(), if do_timer => {
|
||||
log::info!(target: "wal-trunc", "Timer ticked")
|
||||
info!(target: "wal-trunc", "Timer ticked")
|
||||
}
|
||||
_ = s.recv() => {
|
||||
log::info!(target: "wal-trunc", "Received SIGHUP")
|
||||
info!(target: "wal-trunc", "Received SIGHUP")
|
||||
}
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
if do_timer {
|
||||
i.tick().await;
|
||||
log::info!(target: "wal-trunc", "Timer ticked")
|
||||
info!(target: "wal-trunc", "Timer ticked")
|
||||
} else {
|
||||
// timer disabled, and there's no concept of signals on windows, bailing...
|
||||
return;
|
||||
}
|
||||
if let Some(arc) = Weak::upgrade(&weak) {
|
||||
log::info!(target: "wal-trunc", "Rotating sync helpers...");
|
||||
info!(target: "wal-trunc", "Rotating sync helpers...");
|
||||
// This actually creates a very small race condition between firing this and trying to acquire the subsequent write lock.
|
||||
// Though it is not a huge deal if the write lock doesn't "catch", as it'll harmlessly time out.
|
||||
arc.read().await.globals.rotate.fire();
|
||||
|
||||
log::info!(target: "wal-trunc", "Locking...");
|
||||
info!(target: "wal-trunc", "Locking...");
|
||||
let guard = {
|
||||
if let Ok(guard) = timeout(lock_timeout, arc.write()).await {
|
||||
guard
|
||||
} else {
|
||||
log::info!(target: "wal-trunc", "Lock failed in timeout, canceled.");
|
||||
info!(target: "wal-trunc", "Lock failed in timeout, canceled.");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
log::info!(target: "wal-trunc", "Locked, flushing...");
|
||||
info!(target: "wal-trunc", "Locked, flushing...");
|
||||
let start = Instant::now();
|
||||
if let Err(e) = guard.flush_wal() {
|
||||
log::error!(target: "wal-trunc", "Errored: {}", e);
|
||||
error!(target: "wal-trunc", "Errored: {}", e);
|
||||
} else {
|
||||
log::info!(target: "wal-trunc", "Flushed in {:?}", start.elapsed());
|
||||
info!(target: "wal-trunc", "Flushed in {:?}", start.elapsed());
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use super::super::Config;
|
||||
use crate::{utils, Result};
|
||||
use log::warn;
|
||||
use std::{future::Future, pin::Pin, sync::Arc};
|
||||
use tracing::warn;
|
||||
|
||||
use super::{DatabaseEngine, Tree};
|
||||
|
||||
|
|
|
@ -3,9 +3,8 @@ use crate::{database::Config, Result};
|
|||
use crossbeam::channel::{
|
||||
bounded, unbounded, Receiver as ChannelReceiver, Sender as ChannelSender, TryRecvError,
|
||||
};
|
||||
use log::debug;
|
||||
use parking_lot::{Mutex, MutexGuard, RwLock};
|
||||
use rusqlite::{params, Connection, DatabaseName::Main, OptionalExtension};
|
||||
use rusqlite::{params, Connection, DatabaseName::Main, OptionalExtension, Params};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
|
@ -13,10 +12,11 @@ use std::{
|
|||
path::{Path, PathBuf},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use threadpool::ThreadPool;
|
||||
use tokio::sync::oneshot::Sender;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
struct Pool {
|
||||
writer: Mutex<Connection>,
|
||||
|
@ -86,9 +86,9 @@ impl Deref for RecycledConn {
|
|||
impl Drop for RecycledConn {
|
||||
fn drop(&mut self) {
|
||||
if let Some(conn) = self.0.take() {
|
||||
log::debug!("Recycled connection");
|
||||
debug!("Recycled connection");
|
||||
if let Err(e) = self.1.send(conn) {
|
||||
log::warn!("Recycling a connection led to the following error: {:?}", e)
|
||||
warn!("Recycling a connection led to the following error: {:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,14 +149,14 @@ impl Pool {
|
|||
}
|
||||
}
|
||||
|
||||
log::debug!("read_lock: All permanent readers locked, obtaining spillover reader...");
|
||||
debug!("read_lock: All permanent readers locked, obtaining spillover reader...");
|
||||
|
||||
// We didn't get a connection from the permanent pool, so we'll dumpster-dive for recycled connections.
|
||||
// Either we have a connection or we dont, if we don't, we make a new one.
|
||||
let conn = match self.spills.try_take() {
|
||||
Some(conn) => conn,
|
||||
None => {
|
||||
log::debug!("read_lock: No recycled connections left, creating new one...");
|
||||
debug!("read_lock: No recycled connections left, creating new one...");
|
||||
Self::prepare_conn(&self.path, None).unwrap()
|
||||
}
|
||||
};
|
||||
|
@ -169,7 +169,7 @@ impl Pool {
|
|||
|
||||
// If the spillover readers are more than the number of total readers, there might be a problem.
|
||||
if now_count > self.readers.len() {
|
||||
log::warn!(
|
||||
warn!(
|
||||
"Database is under high load. Consider increasing sqlite_read_pool_size ({} spillover readers exist)",
|
||||
now_count
|
||||
);
|
||||
|
@ -182,6 +182,7 @@ impl Pool {
|
|||
|
||||
pub struct Engine {
|
||||
pool: Pool,
|
||||
iter_pool: Mutex<ThreadPool>,
|
||||
}
|
||||
|
||||
impl DatabaseEngine for Engine {
|
||||
|
@ -195,7 +196,10 @@ impl DatabaseEngine for Engine {
|
|||
pool.write_lock()
|
||||
.execute("CREATE TABLE IF NOT EXISTS _noop (\"key\" INT)", params![])?;
|
||||
|
||||
let arc = Arc::new(Engine { pool });
|
||||
let arc = Arc::new(Engine {
|
||||
pool,
|
||||
iter_pool: Mutex::new(ThreadPool::new(10)),
|
||||
});
|
||||
|
||||
Ok(arc)
|
||||
}
|
||||
|
@ -259,7 +263,7 @@ impl Engine {
|
|||
}
|
||||
}
|
||||
|
||||
log::debug!("Reaped {} connections", reaped);
|
||||
debug!("Reaped {} connections", reaped);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -272,6 +276,7 @@ pub struct SqliteTable {
|
|||
type TupleOfBytes = (Vec<u8>, Vec<u8>);
|
||||
|
||||
impl SqliteTable {
|
||||
#[tracing::instrument(skip(self, guard, key))]
|
||||
fn get_with_guard(&self, guard: &Connection, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
Ok(guard
|
||||
.prepare(format!("SELECT value FROM {} WHERE key = ?", self.name).as_str())?
|
||||
|
@ -279,6 +284,7 @@ impl SqliteTable {
|
|||
.optional()?)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, guard, key, value))]
|
||||
fn insert_with_guard(&self, guard: &Connection, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
guard.execute(
|
||||
format!(
|
||||
|
@ -291,41 +297,67 @@ impl SqliteTable {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn _iter_from_thread<F>(&self, f: F) -> Box<dyn Iterator<Item = TupleOfBytes> + Send>
|
||||
where
|
||||
F: (for<'a> FnOnce(&'a Connection, ChannelSender<TupleOfBytes>)) + Send + 'static,
|
||||
{
|
||||
#[tracing::instrument(skip(self, sql, param))]
|
||||
fn iter_from_thread(
|
||||
&self,
|
||||
sql: String,
|
||||
param: Option<Vec<u8>>,
|
||||
) -> Box<dyn Iterator<Item = TupleOfBytes> + Send + Sync> {
|
||||
let (s, r) = bounded::<TupleOfBytes>(5);
|
||||
|
||||
let engine = self.engine.clone();
|
||||
let engine = Arc::clone(&self.engine);
|
||||
|
||||
thread::spawn(move || {
|
||||
let _ = f(&engine.pool.read_lock(), s);
|
||||
});
|
||||
let lock = self.engine.iter_pool.lock();
|
||||
if lock.active_count() < lock.max_count() {
|
||||
lock.execute(move || {
|
||||
if let Some(param) = param {
|
||||
iter_from_thread_work(&engine.pool.read_lock(), &s, &sql, [param]);
|
||||
} else {
|
||||
iter_from_thread_work(&engine.pool.read_lock(), &s, &sql, []);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
std::thread::spawn(move || {
|
||||
if let Some(param) = param {
|
||||
iter_from_thread_work(&engine.pool.read_lock(), &s, &sql, [param]);
|
||||
} else {
|
||||
iter_from_thread_work(&engine.pool.read_lock(), &s, &sql, []);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Box::new(r.into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! iter_from_thread {
|
||||
($self:expr, $sql:expr, $param:expr) => {
|
||||
$self._iter_from_thread(move |guard, s| {
|
||||
let _ = guard
|
||||
.prepare($sql)
|
||||
.unwrap()
|
||||
.query_map($param, |row| Ok((row.get_unwrap(0), row.get_unwrap(1))))
|
||||
.unwrap()
|
||||
.map(|r| r.unwrap())
|
||||
.try_for_each(|bob| s.send(bob));
|
||||
})
|
||||
};
|
||||
fn iter_from_thread_work<P>(
|
||||
guard: &HoldingConn<'_>,
|
||||
s: &ChannelSender<(Vec<u8>, Vec<u8>)>,
|
||||
sql: &str,
|
||||
params: P,
|
||||
) where
|
||||
P: Params,
|
||||
{
|
||||
for bob in guard
|
||||
.prepare(sql)
|
||||
.unwrap()
|
||||
.query_map(params, |row| Ok((row.get_unwrap(0), row.get_unwrap(1))))
|
||||
.unwrap()
|
||||
.map(|r| r.unwrap())
|
||||
{
|
||||
if s.send(bob).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree for SqliteTable {
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
self.get_with_guard(&self.engine.pool.read_lock(), key)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key, value))]
|
||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
let guard = self.engine.pool.write_lock();
|
||||
|
||||
|
@ -365,6 +397,7 @@ impl Tree for SqliteTable {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn remove(&self, key: &[u8]) -> Result<()> {
|
||||
let guard = self.engine.pool.write_lock();
|
||||
|
||||
|
@ -385,15 +418,13 @@ impl Tree for SqliteTable {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = TupleOfBytes> + Send + 'a> {
|
||||
let name = self.name.clone();
|
||||
iter_from_thread!(
|
||||
self,
|
||||
format!("SELECT key, value FROM {}", name).as_str(),
|
||||
params![]
|
||||
)
|
||||
self.iter_from_thread(format!("SELECT key, value FROM {}", name), None)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, from, backwards))]
|
||||
fn iter_from<'a>(
|
||||
&'a self,
|
||||
from: &[u8],
|
||||
|
@ -402,28 +433,25 @@ impl Tree for SqliteTable {
|
|||
let name = self.name.clone();
|
||||
let from = from.to_vec(); // TODO change interface?
|
||||
if backwards {
|
||||
iter_from_thread!(
|
||||
self,
|
||||
self.iter_from_thread(
|
||||
format!(
|
||||
"SELECT key, value FROM {} WHERE key <= ? ORDER BY key DESC",
|
||||
name
|
||||
)
|
||||
.as_str(),
|
||||
[from]
|
||||
),
|
||||
Some(from),
|
||||
)
|
||||
} else {
|
||||
iter_from_thread!(
|
||||
self,
|
||||
self.iter_from_thread(
|
||||
format!(
|
||||
"SELECT key, value FROM {} WHERE key >= ? ORDER BY key ASC",
|
||||
name
|
||||
)
|
||||
.as_str(),
|
||||
[from]
|
||||
),
|
||||
Some(from),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
|
||||
let guard = self.engine.pool.write_lock();
|
||||
|
||||
|
@ -446,18 +474,17 @@ impl Tree for SqliteTable {
|
|||
Ok(new)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn scan_prefix<'a>(
|
||||
&'a self,
|
||||
prefix: Vec<u8>,
|
||||
) -> Box<dyn Iterator<Item = TupleOfBytes> + Send + 'a> {
|
||||
// let name = self.name.clone();
|
||||
// iter_from_thread!(
|
||||
// self,
|
||||
// self.iter_from_thread(
|
||||
// format!(
|
||||
// "SELECT key, value FROM {} WHERE key BETWEEN ?1 AND ?1 || X'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ORDER BY key ASC",
|
||||
// name
|
||||
// )
|
||||
// .as_str(),
|
||||
// [prefix]
|
||||
// )
|
||||
Box::new(
|
||||
|
@ -466,6 +493,7 @@ impl Tree for SqliteTable {
|
|||
)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, prefix))]
|
||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
|
@ -481,6 +509,7 @@ impl Tree for SqliteTable {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn clear(&self) -> Result<()> {
|
||||
debug!("clear: running");
|
||||
self.engine
|
||||
|
|
|
@ -16,6 +16,7 @@ pub struct AccountData {
|
|||
|
||||
impl AccountData {
|
||||
/// Places one event in the account data of the user and removes the previous entry.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, event_type, data, globals))]
|
||||
pub fn update<T: Serialize>(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
|
@ -60,6 +61,7 @@ impl AccountData {
|
|||
}
|
||||
|
||||
/// Searches the account data for a specific kind.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, kind))]
|
||||
pub fn get<T: DeserializeOwned>(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
|
@ -74,7 +76,7 @@ impl AccountData {
|
|||
}
|
||||
|
||||
/// Returns all changes to the account data that happened after `since`.
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, room_id, user_id, since))]
|
||||
pub fn changes_since(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
|
@ -122,6 +124,7 @@ impl AccountData {
|
|||
Ok(userdata)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, room_id, user_id, kind))]
|
||||
fn find_event(
|
||||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
|
|
|
@ -4,13 +4,13 @@ use std::{
|
|||
};
|
||||
|
||||
use crate::{pdu::PduBuilder, Database};
|
||||
use log::warn;
|
||||
use rocket::futures::{channel::mpsc, stream::StreamExt};
|
||||
use ruma::{
|
||||
events::{room::message, EventType},
|
||||
UserId,
|
||||
};
|
||||
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
||||
use tracing::warn;
|
||||
|
||||
pub enum AdminCommand {
|
||||
RegisterAppservice(serde_yaml::Value),
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::{database::Config, utils, ConduitResult, Error, Result};
|
||||
use log::{error, info};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::r0::sync::sync_events,
|
||||
|
@ -17,6 +16,7 @@ use std::{
|
|||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::sync::{broadcast, watch::Receiver, Mutex, Semaphore};
|
||||
use tracing::{error, info};
|
||||
use trust_dns_resolver::TokioAsyncResolver;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
@ -56,6 +56,7 @@ struct MatrixServerVerifier {
|
|||
}
|
||||
|
||||
impl ServerCertVerifier for MatrixServerVerifier {
|
||||
#[tracing::instrument(skip(self, roots, presented_certs, dns_name, ocsp_response))]
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
roots: &rustls::RootCertStore,
|
||||
|
@ -220,11 +221,13 @@ impl Globals {
|
|||
&self.reqwest_client
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn next_count(&self) -> Result<u64> {
|
||||
utils::u64_from_bytes(&self.globals.increment(COUNTER)?)
|
||||
.map_err(|_| Error::bad_database("Count has invalid bytes."))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn current_count(&self) -> Result<u64> {
|
||||
self.globals.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
|
||||
utils::u64_from_bytes(&bytes)
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::{Database, Error, PduEvent, Result};
|
||||
use bytes::BytesMut;
|
||||
use log::{error, info, warn};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::r0::push::{get_pushers, set_pusher, PusherKind},
|
||||
|
@ -15,6 +14,7 @@ use ruma::{
|
|||
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
|
||||
uint, UInt, UserId,
|
||||
};
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use std::{convert::TryFrom, fmt::Debug, mem, sync::Arc};
|
||||
|
||||
|
@ -26,6 +26,7 @@ pub struct PushData {
|
|||
}
|
||||
|
||||
impl PushData {
|
||||
#[tracing::instrument(skip(self, sender, pusher))]
|
||||
pub fn set_pusher(&self, sender: &UserId, pusher: set_pusher::Pusher) -> Result<()> {
|
||||
let mut key = sender.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
|
@ -48,6 +49,7 @@ impl PushData {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, senderkey))]
|
||||
pub fn get_pusher(&self, senderkey: &[u8]) -> Result<Option<get_pushers::Pusher>> {
|
||||
self.senderkey_pusher
|
||||
.get(senderkey)?
|
||||
|
@ -58,6 +60,7 @@ impl PushData {
|
|||
.transpose()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, sender))]
|
||||
pub fn get_pushers(&self, sender: &UserId) -> Result<Vec<get_pushers::Pusher>> {
|
||||
let mut prefix = sender.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
@ -71,6 +74,7 @@ impl PushData {
|
|||
.collect()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, sender))]
|
||||
pub fn get_pusher_senderkeys<'a>(
|
||||
&'a self,
|
||||
sender: &UserId,
|
||||
|
@ -82,6 +86,7 @@ impl PushData {
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(globals, destination, request))]
|
||||
pub async fn send_request<T: OutgoingRequest>(
|
||||
globals: &crate::database::globals::Globals,
|
||||
destination: &str,
|
||||
|
@ -155,6 +160,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(user, unread, pusher, ruleset, pdu, db))]
|
||||
pub async fn send_push_notice(
|
||||
user: &UserId,
|
||||
unread: UInt,
|
||||
|
@ -194,6 +200,7 @@ pub async fn send_push_notice(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(user, ruleset, pdu, db))]
|
||||
pub fn get_actions<'a>(
|
||||
user: &UserId,
|
||||
ruleset: &'a Ruleset,
|
||||
|
@ -225,6 +232,7 @@ pub fn get_actions<'a>(
|
|||
Ok(ruleset.get_actions(&pdu.to_sync_room_event(), &ctx))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(unread, pusher, tweaks, event, db))]
|
||||
async fn send_notice(
|
||||
unread: UInt,
|
||||
pusher: &get_pushers::Pusher,
|
||||
|
|
|
@ -5,7 +5,6 @@ use member::MembershipState;
|
|||
use tokio::sync::MutexGuard;
|
||||
|
||||
use crate::{pdu::PduBuilder, utils, Database, Error, PduEvent, Result};
|
||||
use log::{debug, error, warn};
|
||||
use lru_cache::LruCache;
|
||||
use regex::Regex;
|
||||
use ring::digest;
|
||||
|
@ -27,6 +26,7 @@ use std::{
|
|||
mem,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use super::{abstraction::Tree, admin::AdminCommand, pusher};
|
||||
|
||||
|
@ -82,7 +82,7 @@ pub struct Rooms {
|
|||
pub(super) eventid_outlierpdu: Arc<dyn Tree>,
|
||||
|
||||
/// RoomId + EventId -> Parent PDU EventId.
|
||||
pub(super) prevevent_parent: Arc<dyn Tree>,
|
||||
pub(super) referencedevents: Arc<dyn Tree>,
|
||||
|
||||
pub(super) pdu_cache: Mutex<LruCache<EventId, Arc<PduEvent>>>,
|
||||
pub(super) auth_chain_cache: Mutex<LruCache<EventId, HashSet<EventId>>>,
|
||||
|
@ -617,6 +617,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Returns the leaf pdus of a room.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn get_pdu_leaves(&self, room_id: &RoomId) -> Result<HashSet<EventId>> {
|
||||
let mut prefix = room_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
@ -636,6 +637,7 @@ impl Rooms {
|
|||
///
|
||||
/// The provided `event_ids` become the new leaves, this allows a room to have multiple
|
||||
/// `prev_events`.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn replace_pdu_leaves(&self, room_id: &RoomId, event_ids: &[EventId]) -> Result<()> {
|
||||
let mut prefix = room_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
@ -653,13 +655,15 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn is_event_referenced(&self, room_id: &RoomId, event_id: &EventId) -> Result<bool> {
|
||||
let mut key = room_id.as_bytes().to_vec();
|
||||
key.extend_from_slice(event_id.as_bytes());
|
||||
Ok(self.prevevent_parent.get(&key)?.is_some())
|
||||
Ok(self.referencedevents.get(&key)?.is_some())
|
||||
}
|
||||
|
||||
/// Returns the pdu from the outlier tree.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn get_pdu_outlier(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
|
||||
self.eventid_outlierpdu
|
||||
.get(event_id.as_bytes())?
|
||||
|
@ -671,6 +675,7 @@ impl Rooms {
|
|||
/// Append the PDU as an outlier.
|
||||
///
|
||||
/// Any event given to this will be processed (state-res) on another thread.
|
||||
#[tracing::instrument(skip(self, pdu))]
|
||||
pub fn add_pdu_outlier(&self, event_id: &EventId, pdu: &CanonicalJsonObject) -> Result<()> {
|
||||
self.eventid_outlierpdu.insert(
|
||||
&event_id.as_bytes(),
|
||||
|
@ -684,7 +689,7 @@ impl Rooms {
|
|||
///
|
||||
/// By this point the incoming event should be fully authenticated, no auth happens
|
||||
/// in `append_pdu`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[tracing::instrument(skip(self, pdu, pdu_json, leaves, db))]
|
||||
pub fn append_pdu(
|
||||
&self,
|
||||
pdu: &PduEvent,
|
||||
|
@ -721,11 +726,10 @@ impl Rooms {
|
|||
}
|
||||
|
||||
// We must keep track of all events that have been referenced.
|
||||
for leaf in leaves {
|
||||
for prev in &pdu.prev_events {
|
||||
let mut key = pdu.room_id().as_bytes().to_vec();
|
||||
key.extend_from_slice(leaf.as_bytes());
|
||||
self.prevevent_parent
|
||||
.insert(&key, pdu.event_id().as_bytes())?;
|
||||
key.extend_from_slice(prev.as_bytes());
|
||||
self.referencedevents.insert(&key, &[])?;
|
||||
}
|
||||
|
||||
self.replace_pdu_leaves(&pdu.room_id, leaves)?;
|
||||
|
@ -757,12 +761,11 @@ impl Rooms {
|
|||
|
||||
// See if the event matches any known pushers
|
||||
for user in db
|
||||
.users
|
||||
.iter()
|
||||
.rooms
|
||||
.room_members(&pdu.room_id)
|
||||
.filter_map(|r| r.ok())
|
||||
.filter(|user_id| user_id.server_name() == db.globals.server_name())
|
||||
.filter(|user_id| !db.users.is_deactivated(user_id).unwrap_or(false))
|
||||
.filter(|user_id| self.is_joined(&user_id, &pdu.room_id).unwrap_or(false))
|
||||
{
|
||||
// Don't notify the user of their own events
|
||||
if user == pdu.sender {
|
||||
|
@ -992,6 +995,7 @@ impl Rooms {
|
|||
Ok(pdu_id)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -1005,6 +1009,7 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -1019,6 +1024,7 @@ impl Rooms {
|
|||
.unwrap_or(Ok(0))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -1037,6 +1043,7 @@ impl Rooms {
|
|||
///
|
||||
/// This adds all current state events (not including the incoming event)
|
||||
/// to `stateid_pduid` and adds the incoming event to `eventid_statehash`.
|
||||
#[tracing::instrument(skip(self, state, globals))]
|
||||
pub fn set_event_state(
|
||||
&self,
|
||||
event_id: &EventId,
|
||||
|
@ -1121,6 +1128,7 @@ impl Rooms {
|
|||
///
|
||||
/// This adds all current state events (not including the incoming event)
|
||||
/// to `stateid_pduid` and adds the incoming event to `eventid_statehash`.
|
||||
#[tracing::instrument(skip(self, new_pdu, globals))]
|
||||
pub fn append_to_state(
|
||||
&self,
|
||||
new_pdu: &PduEvent,
|
||||
|
@ -1227,6 +1235,7 @@ impl Rooms {
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, invite_event))]
|
||||
pub fn calculate_invite_state(
|
||||
&self,
|
||||
invite_event: &PduEvent,
|
||||
|
@ -1264,6 +1273,7 @@ impl Rooms {
|
|||
Ok(state)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn set_room_state(&self, room_id: &RoomId, shortstatehash: u64) -> Result<()> {
|
||||
self.roomid_shortstatehash
|
||||
.insert(room_id.as_bytes(), &shortstatehash.to_be_bytes())?;
|
||||
|
@ -1272,6 +1282,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Creates a new persisted data unit and adds it to a room.
|
||||
#[tracing::instrument(skip(self, db, _mutex_lock))]
|
||||
pub fn build_and_append_pdu(
|
||||
&self,
|
||||
pdu_builder: PduBuilder,
|
||||
|
@ -1563,6 +1574,7 @@ impl Rooms {
|
|||
|
||||
/// Returns an iterator over all events and their tokens in a room that happened before the
|
||||
/// event with id `until` in reverse-chronological order.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn pdus_until<'a>(
|
||||
&'a self,
|
||||
user_id: &UserId,
|
||||
|
@ -1625,6 +1637,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Replace a PDU with the redacted form.
|
||||
#[tracing::instrument(skip(self, reason))]
|
||||
pub fn redact_pdu(&self, event_id: &EventId, reason: &PduEvent) -> Result<()> {
|
||||
if let Some(pdu_id) = self.get_pdu_id(event_id)? {
|
||||
let mut pdu = self
|
||||
|
@ -1642,6 +1655,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Update current membership data.
|
||||
#[tracing::instrument(skip(self, last_state, db))]
|
||||
pub fn update_membership(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
|
@ -2026,6 +2040,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Makes a user forget a room.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn forget(&self, room_id: &RoomId, user_id: &UserId) -> Result<()> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -2041,6 +2056,7 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
pub fn set_alias(
|
||||
&self,
|
||||
alias: &RoomAliasId,
|
||||
|
@ -2076,6 +2092,7 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn id_from_alias(&self, alias: &RoomAliasId) -> Result<Option<RoomId>> {
|
||||
self.alias_roomid
|
||||
.get(alias.alias().as_bytes())?
|
||||
|
@ -2089,6 +2106,7 @@ impl Rooms {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn room_aliases<'a>(
|
||||
&'a self,
|
||||
room_id: &RoomId,
|
||||
|
@ -2104,6 +2122,7 @@ impl Rooms {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn set_public(&self, room_id: &RoomId, public: bool) -> Result<()> {
|
||||
if public {
|
||||
self.publicroomids.insert(room_id.as_bytes(), &[])?;
|
||||
|
@ -2114,10 +2133,12 @@ impl Rooms {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn is_public_room(&self, room_id: &RoomId) -> Result<bool> {
|
||||
Ok(self.publicroomids.get(room_id.as_bytes())?.is_some())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn public_rooms(&self) -> impl Iterator<Item = Result<RoomId>> + '_ {
|
||||
self.publicroomids.iter().map(|(bytes, _)| {
|
||||
RoomId::try_from(
|
||||
|
@ -2219,6 +2240,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Returns an iterator of all servers participating in this room.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn room_servers<'a>(
|
||||
&'a self,
|
||||
room_id: &RoomId,
|
||||
|
@ -2242,6 +2264,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Returns an iterator of all rooms a server participates in (as far as we know).
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn server_rooms<'a>(
|
||||
&'a self,
|
||||
server: &ServerName,
|
||||
|
@ -2287,6 +2310,7 @@ impl Rooms {
|
|||
}
|
||||
|
||||
/// Returns an iterator over all User IDs who ever joined a room.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn room_useroncejoined<'a>(
|
||||
&'a self,
|
||||
room_id: &RoomId,
|
||||
|
@ -2494,6 +2518,7 @@ impl Rooms {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -2502,6 +2527,7 @@ impl Rooms {
|
|||
Ok(self.roomuseroncejoinedids.get(&userroom_id)?.is_some())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn is_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -2510,6 +2536,7 @@ impl Rooms {
|
|||
Ok(self.userroomid_joined.get(&userroom_id)?.is_some())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn is_invited(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -2518,6 +2545,7 @@ impl Rooms {
|
|||
Ok(self.userroomid_invitestate.get(&userroom_id)?.is_some())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn is_left(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||
userroom_id.push(0xff);
|
||||
|
@ -2526,6 +2554,7 @@ impl Rooms {
|
|||
Ok(self.userroomid_leftstate.get(&userroom_id)?.is_some())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn auth_chain_cache(
|
||||
&self,
|
||||
) -> std::sync::MutexGuard<'_, LruCache<EventId, HashSet<EventId>>> {
|
||||
|
|
|
@ -116,6 +116,7 @@ impl RoomEdus {
|
|||
}
|
||||
|
||||
/// Sets a private read marker at `count`.
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
pub fn private_read_set(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
|
|
|
@ -10,7 +10,6 @@ use crate::{
|
|||
appservice_server, database::pusher, server_server, utils, Database, Error, PduEvent, Result,
|
||||
};
|
||||
use federation::transactions::send_transaction_message;
|
||||
use log::{error, warn};
|
||||
use ring::digest;
|
||||
use rocket::futures::{
|
||||
channel::mpsc,
|
||||
|
@ -34,6 +33,7 @@ use tokio::{
|
|||
select,
|
||||
sync::{RwLock, Semaphore},
|
||||
};
|
||||
use tracing::{error, warn};
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
|
@ -45,6 +45,7 @@ pub enum OutgoingKind {
|
|||
}
|
||||
|
||||
impl OutgoingKind {
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn get_prefix(&self) -> Vec<u8> {
|
||||
let mut prefix = match self {
|
||||
OutgoingKind::Appservice(server) => {
|
||||
|
@ -223,6 +224,7 @@ impl Sending {
|
|||
});
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(outgoing_kind, new_events, current_transaction_status, db))]
|
||||
fn select_events(
|
||||
outgoing_kind: &OutgoingKind,
|
||||
new_events: Vec<(SendingEventType, Vec<u8>)>, // Events we want to send: event and full key
|
||||
|
@ -295,6 +297,7 @@ impl Sending {
|
|||
Ok(Some(events))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(db, server))]
|
||||
pub fn select_edus(db: &Database, server: &ServerName) -> Result<(Vec<Vec<u8>>, u64)> {
|
||||
// u64: count of last edu
|
||||
let since = db
|
||||
|
@ -371,7 +374,7 @@ impl Sending {
|
|||
Ok((events, max_edu_count))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, pdu_id, senderkey))]
|
||||
pub fn send_push_pdu(&self, pdu_id: &[u8], senderkey: Vec<u8>) -> Result<()> {
|
||||
let mut key = b"$".to_vec();
|
||||
key.extend_from_slice(&senderkey);
|
||||
|
@ -383,7 +386,7 @@ impl Sending {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, server, pdu_id))]
|
||||
pub fn send_pdu(&self, server: &ServerName, pdu_id: &[u8]) -> Result<()> {
|
||||
let mut key = server.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
|
@ -394,7 +397,7 @@ impl Sending {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, server, serialized))]
|
||||
pub fn send_reliable_edu(&self, server: &ServerName, serialized: &[u8]) -> Result<()> {
|
||||
let mut key = server.as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
|
@ -418,7 +421,7 @@ impl Sending {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
#[tracing::instrument(skip(keys))]
|
||||
fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> {
|
||||
// We only hash the pdu's event ids, not the whole pdu
|
||||
let bytes = keys.join(&0xff);
|
||||
|
@ -426,7 +429,7 @@ impl Sending {
|
|||
hash.as_ref().to_owned()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(db))]
|
||||
#[tracing::instrument(skip(db, events, kind))]
|
||||
async fn handle_events(
|
||||
kind: OutgoingKind,
|
||||
events: Vec<SendingEventType>,
|
||||
|
@ -658,6 +661,7 @@ impl Sending {
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(key))]
|
||||
fn parse_servercurrentevent(key: &[u8]) -> Result<(OutgoingKind, SendingEventType)> {
|
||||
// Appservices start with a plus
|
||||
Ok::<_, Error>(if key.starts_with(b"+") {
|
||||
|
@ -723,7 +727,7 @@ impl Sending {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
#[tracing::instrument(skip(self, globals, destination, request))]
|
||||
pub async fn send_federation_request<T: OutgoingRequest>(
|
||||
&self,
|
||||
globals: &crate::database::globals::Globals,
|
||||
|
@ -740,7 +744,7 @@ impl Sending {
|
|||
response
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, globals))]
|
||||
#[tracing::instrument(skip(self, globals, registration, request))]
|
||||
pub async fn send_appservice_request<T: OutgoingRequest>(
|
||||
&self,
|
||||
globals: &crate::database::globals::Globals,
|
||||
|
|
|
@ -8,6 +8,7 @@ use ruma::{
|
|||
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, UInt, UserId,
|
||||
};
|
||||
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc};
|
||||
use tracing::warn;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
|
@ -34,11 +35,13 @@ pub struct Users {
|
|||
|
||||
impl Users {
|
||||
/// Check if a user has an account on this homeserver.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn exists(&self, user_id: &UserId) -> Result<bool> {
|
||||
Ok(self.userid_password.get(user_id.as_bytes())?.is_some())
|
||||
}
|
||||
|
||||
/// Check if account is deactivated
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn is_deactivated(&self, user_id: &UserId) -> Result<bool> {
|
||||
Ok(self
|
||||
.userid_password
|
||||
|
@ -51,17 +54,20 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Create a new user account on this homeserver.
|
||||
#[tracing::instrument(skip(self, user_id, password))]
|
||||
pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
|
||||
self.set_password(user_id, password)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of users registered on this server.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn count(&self) -> Result<usize> {
|
||||
Ok(self.userid_password.iter().count())
|
||||
}
|
||||
|
||||
/// Find out which user an access token belongs to.
|
||||
#[tracing::instrument(skip(self, token))]
|
||||
pub fn find_from_token(&self, token: &str) -> Result<Option<(UserId, String)>> {
|
||||
self.token_userdeviceid
|
||||
.get(token.as_bytes())?
|
||||
|
@ -89,6 +95,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Returns an iterator over all users on this homeserver.
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn iter(&self) -> impl Iterator<Item = Result<UserId>> + '_ {
|
||||
self.userid_password.iter().map(|(bytes, _)| {
|
||||
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||
|
@ -99,6 +106,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Returns the password hash for the given user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_password
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -110,6 +118,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Hash and set the user's password to the Argon2 hash
|
||||
#[tracing::instrument(skip(self, user_id, password))]
|
||||
pub fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
|
||||
if let Some(password) = password {
|
||||
if let Ok(hash) = utils::calculate_hash(&password) {
|
||||
|
@ -129,6 +138,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Returns the displayname of a user on this homeserver.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn displayname(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_displayname
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -140,6 +150,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Sets a new displayname or removes it if displayname is None. You still need to nofify all rooms of this change.
|
||||
#[tracing::instrument(skip(self, user_id, displayname))]
|
||||
pub fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
|
||||
if let Some(displayname) = displayname {
|
||||
self.userid_displayname
|
||||
|
@ -152,6 +163,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Get the avatar_url of a user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<MxcUri>> {
|
||||
self.userid_avatarurl
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -164,6 +176,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Sets a new avatar_url or removes it if avatar_url is None.
|
||||
#[tracing::instrument(skip(self, user_id, avatar_url))]
|
||||
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<MxcUri>) -> Result<()> {
|
||||
if let Some(avatar_url) = avatar_url {
|
||||
self.userid_avatarurl
|
||||
|
@ -176,6 +189,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Get the blurhash of a user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> {
|
||||
self.userid_blurhash
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -189,6 +203,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Sets a new avatar_url or removes it if avatar_url is None.
|
||||
#[tracing::instrument(skip(self, user_id, blurhash))]
|
||||
pub fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
|
||||
if let Some(blurhash) = blurhash {
|
||||
self.userid_blurhash
|
||||
|
@ -201,6 +216,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Adds a new device to a user.
|
||||
#[tracing::instrument(skip(self, user_id, device_id, token, initial_device_display_name))]
|
||||
pub fn create_device(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -235,6 +251,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Removes a device from a user.
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
|
||||
let mut userdeviceid = user_id.as_bytes().to_vec();
|
||||
userdeviceid.push(0xff);
|
||||
|
@ -265,6 +282,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Returns an iterator over all device ids of this user.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn all_device_ids<'a>(
|
||||
&'a self,
|
||||
user_id: &UserId,
|
||||
|
@ -287,6 +305,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Replaces the access token of one device.
|
||||
#[tracing::instrument(skip(self, user_id, device_id, token))]
|
||||
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
|
||||
let mut userdeviceid = user_id.as_bytes().to_vec();
|
||||
userdeviceid.push(0xff);
|
||||
|
@ -310,6 +329,14 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
user_id,
|
||||
device_id,
|
||||
one_time_key_key,
|
||||
one_time_key_value,
|
||||
globals
|
||||
))]
|
||||
pub fn add_one_time_key(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -346,7 +373,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
|
||||
self.userid_lastonetimekeyupdate
|
||||
.get(&user_id.as_bytes())?
|
||||
|
@ -358,6 +385,7 @@ impl Users {
|
|||
.unwrap_or(Ok(0))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, key_algorithm, globals))]
|
||||
pub fn take_one_time_key(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -397,7 +425,7 @@ impl Users {
|
|||
.transpose()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn count_one_time_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -430,6 +458,7 @@ impl Users {
|
|||
Ok(counts)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, device_keys, rooms, globals))]
|
||||
pub fn add_device_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -452,6 +481,14 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
master_key,
|
||||
self_signing_key,
|
||||
user_signing_key,
|
||||
rooms,
|
||||
globals
|
||||
))]
|
||||
pub fn add_cross_signing_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -552,6 +589,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, target_id, key_id, signature, sender_id, rooms, globals))]
|
||||
pub fn sign_key(
|
||||
&self,
|
||||
target_id: &UserId,
|
||||
|
@ -595,7 +633,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_or_room_id, from, to))]
|
||||
pub fn keys_changed<'a>(
|
||||
&'a self,
|
||||
user_or_room_id: &str,
|
||||
|
@ -608,9 +646,24 @@ impl Users {
|
|||
let mut start = prefix.clone();
|
||||
start.extend_from_slice(&(from + 1).to_be_bytes());
|
||||
|
||||
let to = to.unwrap_or(u64::MAX);
|
||||
|
||||
self.keychangeid_userid
|
||||
.iter_from(&start, false)
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.take_while(move |(k, _)| {
|
||||
k.starts_with(&prefix)
|
||||
&& if let Some(current) = k.splitn(2, |&b| b == 0xff).nth(1) {
|
||||
if let Ok(c) = utils::u64_from_bytes(current) {
|
||||
c <= to
|
||||
} else {
|
||||
warn!("BadDatabase: Could not parse keychangeid_userid bytes");
|
||||
false
|
||||
}
|
||||
} else {
|
||||
warn!("BadDatabase: Could not parse keychangeid_userid");
|
||||
false
|
||||
}
|
||||
})
|
||||
.map(|(_, bytes)| {
|
||||
UserId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
|
||||
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.")
|
||||
|
@ -619,6 +672,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, rooms, globals))]
|
||||
fn mark_device_key_update(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -650,6 +704,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_device_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -666,6 +721,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, allowed_signatures))]
|
||||
pub fn get_master_key<F: Fn(&UserId) -> bool>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -693,6 +749,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, allowed_signatures))]
|
||||
pub fn get_self_signing_key<F: Fn(&UserId) -> bool>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -720,6 +777,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<CrossSigningKey>> {
|
||||
self.userid_usersigningkeyid
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -732,6 +790,15 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(
|
||||
self,
|
||||
sender,
|
||||
target_user_id,
|
||||
target_device_id,
|
||||
event_type,
|
||||
content,
|
||||
globals
|
||||
))]
|
||||
pub fn add_to_device_event(
|
||||
&self,
|
||||
sender: &UserId,
|
||||
|
@ -759,7 +826,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_to_device_events(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -782,7 +849,7 @@ impl Users {
|
|||
Ok(events)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
#[tracing::instrument(skip(self, user_id, device_id, until))]
|
||||
pub fn remove_to_device_events(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -817,6 +884,7 @@ impl Users {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id, device_id, device))]
|
||||
pub fn update_device_metadata(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -842,6 +910,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Get device metadata.
|
||||
#[tracing::instrument(skip(self, user_id, device_id))]
|
||||
pub fn get_device_metadata(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
@ -860,6 +929,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
|
||||
self.userid_devicelistversion
|
||||
.get(user_id.as_bytes())?
|
||||
|
@ -870,6 +940,7 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn all_devices_metadata<'a>(
|
||||
&'a self,
|
||||
user_id: &UserId,
|
||||
|
@ -886,6 +957,7 @@ impl Users {
|
|||
}
|
||||
|
||||
/// Deactivate account
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub fn deactivate_account(&self, user_id: &UserId) -> Result<()> {
|
||||
// Remove all associated devices
|
||||
for device_id in self.all_device_ids(user_id) {
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use log::warn;
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::{Error as RumaError, ErrorKind},
|
||||
|
@ -7,17 +6,18 @@ use ruma::{
|
|||
ServerName,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use tracing::warn;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::RumaResponse,
|
||||
http::StatusCode,
|
||||
log::error,
|
||||
rocket::{
|
||||
response::{self, Responder},
|
||||
Request,
|
||||
},
|
||||
ruma::api::client::r0::uiaa::UiaaResponse,
|
||||
tracing::error,
|
||||
};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
71
src/main.rs
71
src/main.rs
|
@ -17,6 +17,7 @@ use std::sync::Arc;
|
|||
use database::Config;
|
||||
pub use database::Database;
|
||||
pub use error::{Error, Result};
|
||||
use opentelemetry::trace::Tracer;
|
||||
pub use pdu::PduEvent;
|
||||
pub use rocket::State;
|
||||
use ruma::api::client::error::ErrorKind;
|
||||
|
@ -31,8 +32,7 @@ use rocket::{
|
|||
routes, Request,
|
||||
};
|
||||
use tokio::sync::RwLock;
|
||||
use tracing::span;
|
||||
use tracing_subscriber::{prelude::*, Registry};
|
||||
use tracing_subscriber::{prelude::*, EnvFilter};
|
||||
|
||||
fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<rocket::Build> {
|
||||
rocket::custom(config)
|
||||
|
@ -201,38 +201,57 @@ async fn main() {
|
|||
.extract::<Config>()
|
||||
.expect("It looks like your config is invalid. Please take a look at the error");
|
||||
|
||||
let mut _span: Option<span::Span> = None;
|
||||
let mut _enter: Option<span::Entered<'_>> = None;
|
||||
let start = async {
|
||||
config.warn_deprecated();
|
||||
|
||||
let db = Database::load_or_create(&config)
|
||||
.await
|
||||
.expect("config is valid");
|
||||
|
||||
let rocket = setup_rocket(raw_config, Arc::clone(&db))
|
||||
.ignite()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Database::start_on_shutdown_tasks(db, rocket.shutdown()).await;
|
||||
|
||||
rocket.launch().await.unwrap();
|
||||
};
|
||||
|
||||
if config.allow_jaeger {
|
||||
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
|
||||
let tracer = opentelemetry_jaeger::new_pipeline()
|
||||
.with_service_name("conduit")
|
||||
.install()
|
||||
.install_simple()
|
||||
.unwrap();
|
||||
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
|
||||
Registry::default().with(telemetry).try_init().unwrap();
|
||||
|
||||
_span = Some(span!(tracing::Level::INFO, "app_start", work_units = 2));
|
||||
_enter = Some(_span.as_ref().unwrap().enter());
|
||||
let span = tracer.start("conduit");
|
||||
start.await;
|
||||
drop(span);
|
||||
} else {
|
||||
std::env::set_var("RUST_LOG", &config.log);
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let registry = tracing_subscriber::Registry::default();
|
||||
if config.tracing_flame {
|
||||
let (flame_layer, _guard) =
|
||||
tracing_flame::FlameLayer::with_file("./tracing.folded").unwrap();
|
||||
let flame_layer = flame_layer.with_empty_samples(false);
|
||||
|
||||
let filter_layer = EnvFilter::new("trace,h2=off");
|
||||
|
||||
let subscriber = registry.with(filter_layer).with(flame_layer);
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
start.await;
|
||||
} else {
|
||||
let fmt_layer = tracing_subscriber::fmt::Layer::new();
|
||||
let filter_layer = EnvFilter::try_from_default_env()
|
||||
.or_else(|_| EnvFilter::try_new("info"))
|
||||
.unwrap();
|
||||
|
||||
let subscriber = registry.with(filter_layer).with(fmt_layer);
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
start.await;
|
||||
}
|
||||
}
|
||||
|
||||
config.warn_deprecated();
|
||||
|
||||
let db = Database::load_or_create(&config)
|
||||
.await
|
||||
.expect("config is valid");
|
||||
|
||||
let rocket = setup_rocket(raw_config, Arc::clone(&db))
|
||||
.ignite()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Database::start_on_shutdown_tasks(db, rocket.shutdown()).await;
|
||||
|
||||
rocket.launch().await.unwrap();
|
||||
}
|
||||
|
||||
#[catch(404)]
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::Error;
|
||||
use log::error;
|
||||
use ruma::{
|
||||
events::{
|
||||
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent,
|
||||
|
@ -13,6 +12,7 @@ use ruma::{
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
|
||||
use tracing::error;
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||
pub struct PduEvent {
|
||||
|
|
|
@ -10,7 +10,6 @@ use std::ops::Deref;
|
|||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::server_server,
|
||||
log::{debug, warn},
|
||||
rocket::{
|
||||
data::{self, ByteUnit, Data, FromData},
|
||||
http::Status,
|
||||
|
@ -23,6 +22,7 @@ use {
|
|||
std::collections::BTreeMap,
|
||||
std::convert::TryFrom,
|
||||
std::io::Cursor,
|
||||
tracing::{debug, warn},
|
||||
};
|
||||
|
||||
/// This struct converts rocket requests into ruma structs by converting them into http requests
|
||||
|
@ -45,6 +45,7 @@ where
|
|||
{
|
||||
type Error = ();
|
||||
|
||||
#[tracing::instrument(skip(request, data))]
|
||||
async fn from_data(
|
||||
request: &'a Request<'_>,
|
||||
data: Data<'a>,
|
||||
|
@ -256,7 +257,10 @@ where
|
|||
match ruma::signatures::verify_json(&pub_key_map, &request_map) {
|
||||
Ok(()) => (None, None, Some(origin), false),
|
||||
Err(e) => {
|
||||
warn!("Failed to verify json request from {}: {}", origin, e);
|
||||
warn!(
|
||||
"Failed to verify json request from {}: {}\n{:?}",
|
||||
origin, e, request_map
|
||||
);
|
||||
|
||||
if request.uri().to_string().contains('@') {
|
||||
warn!("Request uri contained '@' character. Make sure your reverse proxy gives Conduit the raw uri (apache: use nocanon)");
|
||||
|
|
|
@ -5,7 +5,6 @@ use crate::{
|
|||
};
|
||||
use get_profile_information::v1::ProfileField;
|
||||
use http::header::{HeaderValue, AUTHORIZATION, HOST};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use regex::Regex;
|
||||
use rocket::response::content::Json;
|
||||
use ruma::{
|
||||
|
@ -63,7 +62,8 @@ use std::{
|
|||
sync::{Arc, RwLock},
|
||||
time::{Duration, Instant, SystemTime},
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::sync::{MutexGuard, Semaphore};
|
||||
use tracing::{debug, error, info, trace, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post, put};
|
||||
|
@ -838,6 +838,7 @@ type AsyncRecursiveResult<'a, T, E> = Pin<Box<dyn Future<Output = StdResult<T, E
|
|||
/// it
|
||||
/// 14. Use state resolution to find new room state
|
||||
// We use some AsyncRecursiveResult hacks here so we can call this async funtion recursively
|
||||
#[tracing::instrument(skip(value, is_timeline_event, db, pub_key_map))]
|
||||
pub fn handle_incoming_pdu<'a>(
|
||||
origin: &'a ServerName,
|
||||
event_id: &'a EventId,
|
||||
|
@ -1156,6 +1157,18 @@ pub fn handle_incoming_pdu<'a>(
|
|||
}
|
||||
debug!("Auth check succeeded.");
|
||||
|
||||
// We start looking at current room state now, so lets lock the room
|
||||
|
||||
let mutex = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let mutex_lock = mutex.lock().await;
|
||||
|
||||
// Now we calculate the set of extremities this room has after the incoming event has been
|
||||
// applied. We start with the previous extremities (aka leaves)
|
||||
let mut extremities = db
|
||||
|
@ -1170,8 +1183,8 @@ pub fn handle_incoming_pdu<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
// Only keep those extremities we don't have in our timeline yet
|
||||
extremities.retain(|id| !matches!(db.rooms.get_non_outlier_pdu_json(id), Ok(Some(_))));
|
||||
// Only keep those extremities were not referenced yet
|
||||
extremities.retain(|id| !matches!(db.rooms.is_event_referenced(&room_id, id), Ok(true)));
|
||||
|
||||
let mut extremity_statehashes = Vec::new();
|
||||
|
||||
|
@ -1301,9 +1314,11 @@ pub fn handle_incoming_pdu<'a>(
|
|||
return Err("State resolution failed, either an event could not be found or deserialization".into());
|
||||
}
|
||||
};
|
||||
|
||||
state
|
||||
};
|
||||
|
||||
debug!("starting soft fail auth check");
|
||||
// 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it
|
||||
let soft_fail = !state_res::event_auth::auth_check(
|
||||
&room_version,
|
||||
|
@ -1322,11 +1337,11 @@ pub fn handle_incoming_pdu<'a>(
|
|||
pdu_id = Some(
|
||||
append_incoming_pdu(
|
||||
&db,
|
||||
&room_id,
|
||||
&incoming_pdu,
|
||||
val,
|
||||
extremities,
|
||||
&state_at_incoming_event,
|
||||
&mutex_lock,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| "Failed to add pdu to db.".to_owned())?,
|
||||
|
@ -1350,6 +1365,7 @@ pub fn handle_incoming_pdu<'a>(
|
|||
}
|
||||
|
||||
// Event has passed all auth/stateres checks
|
||||
drop(mutex_lock);
|
||||
Ok(pdu_id)
|
||||
})
|
||||
}
|
||||
|
@ -1626,25 +1642,15 @@ pub(crate) async fn fetch_signing_keys(
|
|||
|
||||
/// Append the incoming event setting the state snapshot to the state from the
|
||||
/// server that sent the event.
|
||||
#[tracing::instrument(skip(db))]
|
||||
#[tracing::instrument(skip(db, pdu, pdu_json, new_room_leaves, state, _mutex_lock))]
|
||||
async fn append_incoming_pdu(
|
||||
db: &Database,
|
||||
room_id: &RoomId,
|
||||
pdu: &PduEvent,
|
||||
pdu_json: CanonicalJsonObject,
|
||||
new_room_leaves: HashSet<EventId>,
|
||||
state: &StateMap<Arc<PduEvent>>,
|
||||
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room mutex
|
||||
) -> Result<Vec<u8>> {
|
||||
let mutex = Arc::clone(
|
||||
db.globals
|
||||
.roomid_mutex
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(room_id.clone())
|
||||
.or_default(),
|
||||
);
|
||||
let mutex_lock = mutex.lock().await;
|
||||
|
||||
// We append to state before appending the pdu, so we don't have a moment in time with the
|
||||
// pdu without it's state. This is okay because append_pdu can't fail.
|
||||
db.rooms
|
||||
|
@ -1657,8 +1663,6 @@ async fn append_incoming_pdu(
|
|||
&db,
|
||||
)?;
|
||||
|
||||
drop(mutex_lock);
|
||||
|
||||
for appservice in db.appservice.iter_all()?.filter_map(|r| r.ok()) {
|
||||
if let Some(namespaces) = appservice.1.get("namespaces") {
|
||||
let users = namespaces
|
||||
|
|
|
@ -9,6 +9,7 @@ use std::{
|
|||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn millis_since_unix_epoch() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
|
@ -48,16 +49,19 @@ pub fn generate_keypair() -> Vec<u8> {
|
|||
}
|
||||
|
||||
/// Parses the bytes into an u64.
|
||||
#[tracing::instrument(skip(bytes))]
|
||||
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
|
||||
let array: [u8; 8] = bytes.try_into()?;
|
||||
Ok(u64::from_be_bytes(array))
|
||||
}
|
||||
|
||||
/// Parses the bytes into a string.
|
||||
#[tracing::instrument(skip(bytes))]
|
||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
|
||||
String::from_utf8(bytes.to_vec())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(length))]
|
||||
pub fn random_string(length: usize) -> String {
|
||||
thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
|
@ -67,6 +71,7 @@ pub fn random_string(length: usize) -> String {
|
|||
}
|
||||
|
||||
/// Calculate a new hash for the given password
|
||||
#[tracing::instrument(skip(password))]
|
||||
pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
|
||||
let hashing_config = Config {
|
||||
variant: Variant::Argon2id,
|
||||
|
@ -77,6 +82,7 @@ pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
|
|||
argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(iterators, check_order))]
|
||||
pub fn common_elements(
|
||||
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>,
|
||||
check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
||||
|
@ -104,6 +110,7 @@ pub fn common_elements(
|
|||
/// Fallible conversion from any value that implements `Serialize` to a `CanonicalJsonObject`.
|
||||
///
|
||||
/// `value` must serialize to an `serde_json::Value::Object`.
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn to_canonical_object<T: serde::Serialize>(
|
||||
value: T,
|
||||
) -> Result<CanonicalJsonObject, CanonicalJsonError> {
|
||||
|
@ -117,6 +124,7 @@ pub fn to_canonical_object<T: serde::Serialize>(
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(deserializer))]
|
||||
pub fn deserialize_from_str<
|
||||
'de,
|
||||
D: serde::de::Deserializer<'de>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue