Database Refactor
combine service/users data w/ mod unit split sliding sync related out of service/users instrument database entry points remove increment crap from database interface de-wrap all database get() calls de-wrap all database insert() calls de-wrap all database remove() calls refactor database interface for async streaming add query key serializer for database implement Debug for result handle add query deserializer for database add deserialization trait for option handle start a stream utils suite de-wrap/asyncify/type-query count_one_time_keys() de-wrap/asyncify users count add admin query users command suite de-wrap/asyncify users exists de-wrap/partially asyncify user filter related asyncify/de-wrap users device/keys related asyncify/de-wrap user auth/misc related asyncify/de-wrap users blurhash asyncify/de-wrap account_data get; merge Data into Service partial asyncify/de-wrap uiaa; merge Data into Service partially asyncify/de-wrap transaction_ids get; merge Data into Service partially asyncify/de-wrap key_backups; merge Data into Service asyncify/de-wrap pusher service getters; merge Data into Service asyncify/de-wrap rooms alias getters/some iterators asyncify/de-wrap rooms directory getters/iterator partially asyncify/de-wrap rooms lazy-loading partially asyncify/de-wrap rooms metadata asyncify/dewrap rooms outlier asyncify/dewrap rooms pdu_metadata dewrap/partially asyncify rooms read receipt de-wrap rooms search service de-wrap/partially asyncify rooms user service partial de-wrap rooms state_compressor de-wrap rooms state_cache de-wrap room state et al de-wrap rooms timeline service additional users device/keys related de-wrap/asyncify sender asyncify services refactor database to TryFuture/TryStream refactor services for TryFuture/TryStream asyncify api handlers additional asyncification for admin module abstract stream related; support reverse streams additional stream conversions asyncify state-res related Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
6001014078
commit
946ca364e0
203 changed files with 12202 additions and 10709 deletions
|
@ -1,19 +1,22 @@
|
|||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use conduit::{debug, err, info, utils, warn, Error, Result};
|
||||
use database::Map;
|
||||
use conduit::{debug, info, warn, Result};
|
||||
use database::{Deserialized, Map};
|
||||
use ruma::events::room::message::RoomMessageEventContent;
|
||||
use serde::Deserialize;
|
||||
use tokio::{sync::Notify, time::interval};
|
||||
use tokio::{
|
||||
sync::Notify,
|
||||
time::{interval, MissedTickBehavior},
|
||||
};
|
||||
|
||||
use crate::{admin, client, globals, Dep};
|
||||
|
||||
pub struct Service {
|
||||
services: Services,
|
||||
db: Arc<Map>,
|
||||
interrupt: Notify,
|
||||
interval: Duration,
|
||||
interrupt: Notify,
|
||||
db: Arc<Map>,
|
||||
services: Services,
|
||||
}
|
||||
|
||||
struct Services {
|
||||
|
@ -22,12 +25,12 @@ struct Services {
|
|||
globals: Dep<globals::Service>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CheckForUpdatesResponse {
|
||||
updates: Vec<CheckForUpdatesResponseEntry>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CheckForUpdatesResponseEntry {
|
||||
id: u64,
|
||||
date: String,
|
||||
|
@ -42,33 +45,38 @@ const LAST_CHECK_FOR_UPDATES_COUNT: &[u8] = b"u";
|
|||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
interval: Duration::from_secs(CHECK_FOR_UPDATES_INTERVAL),
|
||||
interrupt: Notify::new(),
|
||||
db: args.db["global"].clone(),
|
||||
services: Services {
|
||||
globals: args.depend::<globals::Service>("globals"),
|
||||
admin: args.depend::<admin::Service>("admin"),
|
||||
client: args.depend::<client::Service>("client"),
|
||||
},
|
||||
db: args.db["global"].clone(),
|
||||
interrupt: Notify::new(),
|
||||
interval: Duration::from_secs(CHECK_FOR_UPDATES_INTERVAL),
|
||||
}))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, name = "updates", level = "trace")]
|
||||
async fn worker(self: Arc<Self>) -> Result<()> {
|
||||
if !self.services.globals.allow_check_for_updates() {
|
||||
debug!("Disabling update check");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut i = interval(self.interval);
|
||||
i.set_missed_tick_behavior(MissedTickBehavior::Delay);
|
||||
loop {
|
||||
tokio::select! {
|
||||
() = self.interrupt.notified() => return Ok(()),
|
||||
() = self.interrupt.notified() => break,
|
||||
_ = i.tick() => (),
|
||||
}
|
||||
|
||||
if let Err(e) = self.handle_updates().await {
|
||||
if let Err(e) = self.check().await {
|
||||
warn!(%e, "Failed to check for updates");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn interrupt(&self) { self.interrupt.notify_waiters(); }
|
||||
|
@ -77,52 +85,52 @@ impl crate::Service for Service {
|
|||
}
|
||||
|
||||
impl Service {
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn handle_updates(&self) -> Result<()> {
|
||||
#[tracing::instrument(skip_all, level = "trace")]
|
||||
async fn check(&self) -> Result<()> {
|
||||
let response = self
|
||||
.services
|
||||
.client
|
||||
.default
|
||||
.get(CHECK_FOR_UPDATES_URL)
|
||||
.send()
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
|
||||
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response.text().await?)
|
||||
.map_err(|e| err!("Bad check for updates response: {e}"))?;
|
||||
|
||||
let mut last_update_id = self.last_check_for_updates_id()?;
|
||||
for update in response.updates {
|
||||
last_update_id = last_update_id.max(update.id);
|
||||
if update.id > self.last_check_for_updates_id()? {
|
||||
info!("{:#}", update.message);
|
||||
self.services
|
||||
.admin
|
||||
.send_message(RoomMessageEventContent::text_markdown(format!(
|
||||
"### the following is a message from the conduwuit puppy\n\nit was sent on `{}`:\n\n@room: {}",
|
||||
update.date, update.message
|
||||
)))
|
||||
.await;
|
||||
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response)?;
|
||||
for update in &response.updates {
|
||||
if update.id > self.last_check_for_updates_id().await {
|
||||
self.handle(update).await;
|
||||
self.update_check_for_updates_id(update.id);
|
||||
}
|
||||
}
|
||||
self.update_check_for_updates_id(last_update_id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle(&self, update: &CheckForUpdatesResponseEntry) {
|
||||
info!("{} {:#}", update.date, update.message);
|
||||
self.services
|
||||
.admin
|
||||
.send_message(RoomMessageEventContent::text_markdown(format!(
|
||||
"### the following is a message from the conduwuit puppy\n\nit was sent on `{}`:\n\n@room: {}",
|
||||
update.date, update.message
|
||||
)))
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn update_check_for_updates_id(&self, id: u64) -> Result<()> {
|
||||
pub fn update_check_for_updates_id(&self, id: u64) {
|
||||
self.db
|
||||
.insert(LAST_CHECK_FOR_UPDATES_COUNT, &id.to_be_bytes())?;
|
||||
|
||||
Ok(())
|
||||
.insert(LAST_CHECK_FOR_UPDATES_COUNT, &id.to_be_bytes());
|
||||
}
|
||||
|
||||
pub fn last_check_for_updates_id(&self) -> Result<u64> {
|
||||
pub async fn last_check_for_updates_id(&self) -> u64 {
|
||||
self.db
|
||||
.get(LAST_CHECK_FOR_UPDATES_COUNT)?
|
||||
.map_or(Ok(0_u64), |bytes| {
|
||||
utils::u64_from_bytes(&bytes)
|
||||
.map_err(|_| Error::bad_database("last check for updates count has invalid bytes."))
|
||||
})
|
||||
.qry(LAST_CHECK_FOR_UPDATES_COUNT)
|
||||
.await
|
||||
.deserialized()
|
||||
.unwrap_or(0_u64)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue