lazy-construct presence; avoids useless db queries in sender and syncer.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
8b003e6be2
commit
33cc3d56c1
5 changed files with 16 additions and 11 deletions
|
@ -571,7 +571,7 @@ async fn process_presence_updates(
|
|||
presence_updates: &mut HashMap<OwnedUserId, PresenceEvent>, since: u64, syncing_user: &OwnedUserId,
|
||||
) -> Result<()> {
|
||||
// Take presence updates
|
||||
for (user_id, _, presence_event) in services().presence.presence_since(since) {
|
||||
for (user_id, _, presence_bytes) in services().presence.presence_since(since) {
|
||||
if !services()
|
||||
.rooms
|
||||
.state_cache
|
||||
|
@ -580,6 +580,8 @@ async fn process_presence_updates(
|
|||
continue;
|
||||
}
|
||||
|
||||
use crate::service::presence::Presence;
|
||||
let presence_event = Presence::from_json_bytes_to_event(&presence_bytes, &user_id)?;
|
||||
match presence_updates.entry(user_id) {
|
||||
Entry::Vacant(slot) => {
|
||||
slot.insert(presence_event);
|
||||
|
|
|
@ -92,16 +92,13 @@ impl service::presence::Data for KeyValueDatabase {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a> {
|
||||
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, Vec<u8>)> + 'a> {
|
||||
Box::new(
|
||||
self.presenceid_presence
|
||||
.iter()
|
||||
.flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, PresenceEvent)> {
|
||||
.flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec<u8>)> {
|
||||
let (count, user_id) = presenceid_parse(&key)?;
|
||||
let presence = Presence::from_json_bytes(&presence_bytes)?;
|
||||
let presence_event = presence.to_presence_event(&user_id)?;
|
||||
|
||||
Ok((user_id, count, presence_event))
|
||||
Ok((user_id, count, presence_bytes))
|
||||
})
|
||||
.filter(move |(_, count, _)| *count > since),
|
||||
)
|
||||
|
|
|
@ -17,5 +17,5 @@ pub trait Data: Send + Sync {
|
|||
|
||||
/// Returns the most recent presence updates that happened after the event
|
||||
/// with id `since`.
|
||||
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a>;
|
||||
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, Vec<u8>)> + 'a>;
|
||||
}
|
||||
|
|
|
@ -35,6 +35,11 @@ impl Presence {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_json_bytes_to_event(bytes: &[u8], user_id: &UserId) -> Result<PresenceEvent> {
|
||||
let presence = Self::from_json_bytes(bytes)?;
|
||||
presence.to_presence_event(user_id)
|
||||
}
|
||||
|
||||
pub fn from_json_bytes(bytes: &[u8]) -> Result<Self> {
|
||||
serde_json::from_slice(bytes).map_err(|_| Error::bad_database("Invalid presence data in database"))
|
||||
}
|
||||
|
@ -169,7 +174,7 @@ impl Service {
|
|||
|
||||
/// Returns the most recent presence updates that happened after the event
|
||||
/// with id `since`.
|
||||
pub fn presence_since(&self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)>> {
|
||||
pub fn presence_since(&self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, Vec<u8>)>> {
|
||||
self.db.presence_since(since)
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ use ruma::{
|
|||
use tokio::sync::{Mutex, Semaphore};
|
||||
use tracing::{error, warn};
|
||||
|
||||
use crate::{services, utils::calculate_hash, Config, Error, PduEvent, Result};
|
||||
use crate::{service::presence::Presence, services, utils::calculate_hash, Config, Error, PduEvent, Result};
|
||||
|
||||
pub mod appservice;
|
||||
pub mod data;
|
||||
|
@ -493,7 +493,7 @@ pub fn select_edus_presence(
|
|||
) -> Result<bool> {
|
||||
// Look for presence updates for this server
|
||||
let mut presence_updates = Vec::new();
|
||||
for (user_id, count, presence_event) in services().presence.presence_since(since) {
|
||||
for (user_id, count, presence_bytes) in services().presence.presence_since(since) {
|
||||
*max_edu_count = cmp::max(count, *max_edu_count);
|
||||
|
||||
if user_id.server_name() != services().globals.server_name() {
|
||||
|
@ -508,6 +508,7 @@ pub fn select_edus_presence(
|
|||
continue;
|
||||
}
|
||||
|
||||
let presence_event = Presence::from_json_bytes_to_event(&presence_bytes, &user_id)?;
|
||||
presence_updates.push(PresenceUpdate {
|
||||
user_id,
|
||||
presence: presence_event.content.presence,
|
||||
|
|
Loading…
Add table
Reference in a new issue