mirror of https://github.com/LemmyNet/lemmy
federation: parallel sending per instance (#4623)
* federation: parallel sending * federation: some comments * lint and set force_write true when a request fails * inbox_urls return vec * split inbox functions into separate file * cleanup * extract sending task code to separate file * move federation concurrent config to config file * off by one issue * improve msg * fix both permanent stopping of federation queues and multiple creation of the same federation queues * fix after merge * lint fix * Update crates/federate/src/send.rs Co-authored-by: dullbananas <dull.bananas0@gmail.com> * comment about reverse ordering * remove crashable, comment * comment * move comment * run federation tests twice * fix test run * prettier * fix config default * upgrade rust to 1.78 to fix diesel cli * fix clippy * delay * add debug to make localhost urls not valid in ap crate, add some debug logs * federation tests: ensure server stop after test and random activity id * ci fix * add test to federate 100 events * fix send 100 test * different data every time so activities are distinguishable * allow out of order receives in test * lint * comment about https://github.com/LemmyNet/lemmy/pull/4623#discussion_r1565437391 * move sender for clarity, add comment * move more things to members * update test todo comment, use same env var as worker test but default to 1 * remove else below continue * some more cleanup * handle todo about smooth exit * add federate inboxes collector tests * lint * actor max length * don't reset fail count if activity skipped * fix some comments * reuse vars * format * Update .woodpecker.yml * fix recheck time * fix inboxes tests under fast mode * format * make i32 and ugly casts * clippy --------- Co-authored-by: dullbananas <dull.bananas0@gmail.com>concurrent-sends-config-type
parent
073ff44676
commit
a08642f813
@ -0,0 +1,572 @@
|
||||
use crate::util::LEMMY_TEST_FAST_FEDERATION;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use lemmy_db_schema::{
|
||||
newtypes::{CommunityId, DbUrl, InstanceId},
|
||||
source::{activity::SentActivity, site::Site},
|
||||
utils::{ActualDbPool, DbPool},
|
||||
};
|
||||
use lemmy_db_views_actor::structs::CommunityFollowerView;
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::Url;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// interval with which new additions to community_followers are queried.
|
||||
///
|
||||
/// The first time some user on an instance follows a specific remote community (or, more precisely:
|
||||
/// the first time a (followed_community_id, follower_inbox_url) tuple appears), this delay limits
|
||||
/// the maximum time until the follow actually results in activities from that community id being
|
||||
/// sent to that inbox url. This delay currently needs to not be too small because the DB load is
|
||||
/// currently fairly high because of the current structure of storing inboxes for every person, not
|
||||
/// having a separate list of shared_inboxes, and the architecture of having every instance queue be
|
||||
/// fully separate. (see https://github.com/LemmyNet/lemmy/issues/3958)
|
||||
static FOLLOW_ADDITIONS_RECHECK_DELAY: Lazy<chrono::TimeDelta> = Lazy::new(|| {
|
||||
if *LEMMY_TEST_FAST_FEDERATION {
|
||||
chrono::TimeDelta::try_seconds(1).expect("TimeDelta out of bounds")
|
||||
} else {
|
||||
chrono::TimeDelta::try_minutes(2).expect("TimeDelta out of bounds")
|
||||
}
|
||||
});
|
||||
/// The same as FOLLOW_ADDITIONS_RECHECK_DELAY, but triggering when the last person on an instance
|
||||
/// unfollows a specific remote community. This is expected to happen pretty rarely and updating it
|
||||
/// in a timely manner is not too important.
|
||||
static FOLLOW_REMOVALS_RECHECK_DELAY: Lazy<chrono::TimeDelta> =
|
||||
Lazy::new(|| chrono::TimeDelta::try_hours(1).expect("TimeDelta out of bounds"));
|
||||
|
||||
#[async_trait]
|
||||
pub trait DataSource: Send + Sync {
|
||||
async fn read_site_from_instance_id(
|
||||
&self,
|
||||
instance_id: InstanceId,
|
||||
) -> Result<Option<Site>, diesel::result::Error>;
|
||||
async fn get_instance_followed_community_inboxes(
|
||||
&self,
|
||||
instance_id: InstanceId,
|
||||
last_fetch: DateTime<Utc>,
|
||||
) -> Result<Vec<(CommunityId, DbUrl)>, diesel::result::Error>;
|
||||
}
|
||||
pub struct DbDataSource {
|
||||
pool: ActualDbPool,
|
||||
}
|
||||
|
||||
impl DbDataSource {
|
||||
pub fn new(pool: ActualDbPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DataSource for DbDataSource {
|
||||
async fn read_site_from_instance_id(
|
||||
&self,
|
||||
instance_id: InstanceId,
|
||||
) -> Result<Option<Site>, diesel::result::Error> {
|
||||
Site::read_from_instance_id(&mut DbPool::Pool(&self.pool), instance_id).await
|
||||
}
|
||||
|
||||
async fn get_instance_followed_community_inboxes(
|
||||
&self,
|
||||
instance_id: InstanceId,
|
||||
last_fetch: DateTime<Utc>,
|
||||
) -> Result<Vec<(CommunityId, DbUrl)>, diesel::result::Error> {
|
||||
CommunityFollowerView::get_instance_followed_community_inboxes(
|
||||
&mut DbPool::Pool(&self.pool),
|
||||
instance_id,
|
||||
last_fetch,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct CommunityInboxCollector<T: DataSource> {
|
||||
// load site lazily because if an instance is first seen due to being on allowlist,
|
||||
// the corresponding row in `site` may not exist yet since that is only added once
|
||||
// `fetch_instance_actor_for_object` is called.
|
||||
// (this should be unlikely to be relevant outside of the federation tests)
|
||||
site_loaded: bool,
|
||||
site: Option<Site>,
|
||||
followed_communities: HashMap<CommunityId, HashSet<Url>>,
|
||||
last_full_communities_fetch: DateTime<Utc>,
|
||||
last_incremental_communities_fetch: DateTime<Utc>,
|
||||
instance_id: InstanceId,
|
||||
domain: String,
|
||||
pub(crate) data_source: T,
|
||||
}
|
||||
|
||||
pub type RealCommunityInboxCollector = CommunityInboxCollector<DbDataSource>;
|
||||
|
||||
impl<T: DataSource> CommunityInboxCollector<T> {
|
||||
pub fn new_real(
|
||||
pool: ActualDbPool,
|
||||
instance_id: InstanceId,
|
||||
domain: String,
|
||||
) -> RealCommunityInboxCollector {
|
||||
CommunityInboxCollector::new(DbDataSource::new(pool), instance_id, domain)
|
||||
}
|
||||
pub fn new(
|
||||
data_source: T,
|
||||
instance_id: InstanceId,
|
||||
domain: String,
|
||||
) -> CommunityInboxCollector<T> {
|
||||
CommunityInboxCollector {
|
||||
data_source,
|
||||
site_loaded: false,
|
||||
site: None,
|
||||
followed_communities: HashMap::new(),
|
||||
last_full_communities_fetch: Utc.timestamp_nanos(0),
|
||||
last_incremental_communities_fetch: Utc.timestamp_nanos(0),
|
||||
instance_id,
|
||||
domain,
|
||||
}
|
||||
}
|
||||
/// get inbox urls of sending the given activity to the given instance
|
||||
/// most often this will return 0 values (if instance doesn't care about the activity)
|
||||
/// or 1 value (the shared inbox)
|
||||
/// > 1 values only happens for non-lemmy software
|
||||
pub async fn get_inbox_urls(&mut self, activity: &SentActivity) -> Result<Vec<Url>> {
|
||||
let mut inbox_urls: HashSet<Url> = HashSet::new();
|
||||
|
||||
if activity.send_all_instances {
|
||||
if !self.site_loaded {
|
||||
self.site = self
|
||||
.data_source
|
||||
.read_site_from_instance_id(self.instance_id)
|
||||
.await?;
|
||||
self.site_loaded = true;
|
||||
}
|
||||
if let Some(site) = &self.site {
|
||||
// Nutomic: Most non-lemmy software wont have a site row. That means it cant handle these
|
||||
// activities. So handling it like this is fine.
|
||||
inbox_urls.insert(site.inbox_url.inner().clone());
|
||||
}
|
||||
}
|
||||
if let Some(t) = &activity.send_community_followers_of {
|
||||
if let Some(urls) = self.followed_communities.get(t) {
|
||||
inbox_urls.extend(urls.iter().cloned());
|
||||
}
|
||||
}
|
||||
inbox_urls.extend(
|
||||
activity
|
||||
.send_inboxes
|
||||
.iter()
|
||||
.filter_map(std::option::Option::as_ref)
|
||||
// a similar filter also happens within the activitypub-federation crate. but that filter
|
||||
// happens much later - by doing it here, we can ensure that in the happy case, this
|
||||
// function returns 0 urls which means the system doesn't have to create a tokio
|
||||
// task for sending at all (since that task has a fair amount of overhead)
|
||||
.filter(|&u| (u.domain() == Some(&self.domain)))
|
||||
.map(|u| u.inner().clone()),
|
||||
);
|
||||
tracing::trace!(
|
||||
"get_inbox_urls: {:?}, send_inboxes: {:?}",
|
||||
inbox_urls,
|
||||
activity.send_inboxes
|
||||
);
|
||||
Ok(inbox_urls.into_iter().collect())
|
||||
}
|
||||
|
||||
pub async fn update_communities(&mut self) -> Result<()> {
|
||||
if (Utc::now() - self.last_full_communities_fetch) > *FOLLOW_REMOVALS_RECHECK_DELAY {
|
||||
tracing::debug!("{}: fetching full list of communities", self.domain);
|
||||
// process removals every hour
|
||||
(self.followed_communities, self.last_full_communities_fetch) = self
|
||||
.get_communities(self.instance_id, Utc.timestamp_nanos(0))
|
||||
.await?;
|
||||
self.last_incremental_communities_fetch = self.last_full_communities_fetch;
|
||||
}
|
||||
if (Utc::now() - self.last_incremental_communities_fetch) > *FOLLOW_ADDITIONS_RECHECK_DELAY {
|
||||
// process additions every minute
|
||||
let (news, time) = self
|
||||
.get_communities(self.instance_id, self.last_incremental_communities_fetch)
|
||||
.await?;
|
||||
if !news.is_empty() {
|
||||
tracing::debug!(
|
||||
"{}: fetched {} incremental new followed communities",
|
||||
self.domain,
|
||||
news.len()
|
||||
);
|
||||
}
|
||||
self.followed_communities.extend(news);
|
||||
self.last_incremental_communities_fetch = time;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// get a list of local communities with the remote inboxes on the given instance that cares about
|
||||
/// them
|
||||
async fn get_communities(
|
||||
&mut self,
|
||||
instance_id: InstanceId,
|
||||
last_fetch: DateTime<Utc>,
|
||||
) -> Result<(HashMap<CommunityId, HashSet<Url>>, DateTime<Utc>)> {
|
||||
// update to time before fetch to ensure overlap. subtract some time to ensure overlap even if
|
||||
// published date is not exact
|
||||
let new_last_fetch = Utc::now() - *FOLLOW_ADDITIONS_RECHECK_DELAY / 2;
|
||||
|
||||
let inboxes = self
|
||||
.data_source
|
||||
.get_instance_followed_community_inboxes(instance_id, last_fetch)
|
||||
.await?;
|
||||
|
||||
let map: HashMap<CommunityId, HashSet<Url>> =
|
||||
inboxes.into_iter().fold(HashMap::new(), |mut map, (c, u)| {
|
||||
map.entry(c).or_default().insert(u.into());
|
||||
map
|
||||
});
|
||||
|
||||
Ok((map, new_last_fetch))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use lemmy_db_schema::{
|
||||
newtypes::{ActivityId, CommunityId, InstanceId, SiteId},
|
||||
source::activity::{ActorType, SentActivity},
|
||||
};
|
||||
use mockall::{mock, predicate::*};
|
||||
use serde_json::json;
|
||||
mock! {
|
||||
DataSource {}
|
||||
#[async_trait]
|
||||
impl DataSource for DataSource {
|
||||
async fn read_site_from_instance_id(&self, instance_id: InstanceId) -> Result<Option<Site>, diesel::result::Error>;
|
||||
async fn get_instance_followed_community_inboxes(
|
||||
&self,
|
||||
instance_id: InstanceId,
|
||||
last_fetch: DateTime<Utc>,
|
||||
) -> Result<Vec<(CommunityId, DbUrl)>, diesel::result::Error>;
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_collector() -> CommunityInboxCollector<MockDataSource> {
|
||||
let mock_data_source = MockDataSource::new();
|
||||
let instance_id = InstanceId(1);
|
||||
let domain = "example.com".to_string();
|
||||
CommunityInboxCollector::new(mock_data_source, instance_id, domain)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_empty() {
|
||||
let mut collector = setup_collector();
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![],
|
||||
send_community_followers_of: None,
|
||||
send_all_instances: false,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_send_all_instances() {
|
||||
let mut collector = setup_collector();
|
||||
let site_inbox = Url::parse("https://example.com/inbox").unwrap();
|
||||
let site = Site {
|
||||
id: SiteId(1),
|
||||
name: "Test Site".to_string(),
|
||||
sidebar: None,
|
||||
published: Utc::now(),
|
||||
updated: None,
|
||||
icon: None,
|
||||
banner: None,
|
||||
description: None,
|
||||
actor_id: Url::parse("https://example.com/site").unwrap().into(),
|
||||
last_refreshed_at: Utc::now(),
|
||||
inbox_url: site_inbox.clone().into(),
|
||||
private_key: None,
|
||||
public_key: "test_key".to_string(),
|
||||
instance_id: InstanceId(1),
|
||||
content_warning: None,
|
||||
};
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_read_site_from_instance_id()
|
||||
.return_once(move |_| Ok(Some(site)));
|
||||
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![],
|
||||
send_community_followers_of: None,
|
||||
send_all_instances: true,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0], site_inbox);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_community_followers() {
|
||||
let mut collector = setup_collector();
|
||||
let community_id = CommunityId(1);
|
||||
let url1 = "https://follower1.example.com/inbox";
|
||||
let url2 = "https://follower2.example.com/inbox";
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_get_instance_followed_community_inboxes()
|
||||
.return_once(move |_, _| {
|
||||
Ok(vec![
|
||||
(community_id, Url::parse(url1).unwrap().into()),
|
||||
(community_id, Url::parse(url2).unwrap().into()),
|
||||
])
|
||||
});
|
||||
|
||||
collector.update_communities().await.unwrap();
|
||||
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![],
|
||||
send_community_followers_of: Some(community_id),
|
||||
send_all_instances: false,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert_eq!(result.len(), 2);
|
||||
assert!(result.contains(&Url::parse(url1).unwrap()));
|
||||
assert!(result.contains(&Url::parse(url2).unwrap()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_send_inboxes() {
|
||||
let mut collector = setup_collector();
|
||||
collector.domain = "example.com".to_string();
|
||||
let inbox_user_1 = Url::parse("https://example.com/user1/inbox").unwrap();
|
||||
let inbox_user_2 = Url::parse("https://example.com/user2/inbox").unwrap();
|
||||
let other_domain_inbox = Url::parse("https://other-domain.com/user3/inbox").unwrap();
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![
|
||||
Some(inbox_user_1.clone().into()),
|
||||
Some(inbox_user_2.clone().into()),
|
||||
Some(other_domain_inbox.clone().into()),
|
||||
],
|
||||
send_community_followers_of: None,
|
||||
send_all_instances: false,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert_eq!(result.len(), 2);
|
||||
assert!(result.contains(&inbox_user_1));
|
||||
assert!(result.contains(&inbox_user_2));
|
||||
assert!(!result.contains(&other_domain_inbox));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_combined() {
|
||||
let mut collector = setup_collector();
|
||||
collector.domain = "example.com".to_string();
|
||||
let community_id = CommunityId(1);
|
||||
|
||||
let site_inbox = Url::parse("https://example.com/site_inbox").unwrap();
|
||||
let site = Site {
|
||||
id: SiteId(1),
|
||||
name: "Test Site".to_string(),
|
||||
sidebar: None,
|
||||
published: Utc::now(),
|
||||
updated: None,
|
||||
icon: None,
|
||||
banner: None,
|
||||
description: None,
|
||||
actor_id: Url::parse("https://example.com/site").unwrap().into(),
|
||||
last_refreshed_at: Utc::now(),
|
||||
inbox_url: site_inbox.clone().into(),
|
||||
private_key: None,
|
||||
public_key: "test_key".to_string(),
|
||||
instance_id: InstanceId(1),
|
||||
content_warning: None,
|
||||
};
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_read_site_from_instance_id()
|
||||
.return_once(move |_| Ok(Some(site)));
|
||||
|
||||
let subdomain_inbox = "https://follower.example.com/inbox";
|
||||
collector
|
||||
.data_source
|
||||
.expect_get_instance_followed_community_inboxes()
|
||||
.return_once(move |_, _| {
|
||||
Ok(vec![(
|
||||
community_id,
|
||||
Url::parse(subdomain_inbox).unwrap().into(),
|
||||
)])
|
||||
});
|
||||
|
||||
collector.update_communities().await.unwrap();
|
||||
let user1_inbox = Url::parse("https://example.com/user1/inbox").unwrap();
|
||||
let user2_inbox = Url::parse("https://other-domain.com/user2/inbox").unwrap();
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![
|
||||
Some(user1_inbox.clone().into()),
|
||||
Some(user2_inbox.clone().into()),
|
||||
],
|
||||
send_community_followers_of: Some(community_id),
|
||||
send_all_instances: true,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert_eq!(result.len(), 3);
|
||||
assert!(result.contains(&site_inbox));
|
||||
assert!(result.contains(&Url::parse(subdomain_inbox).unwrap()));
|
||||
assert!(result.contains(&user1_inbox));
|
||||
assert!(!result.contains(&user2_inbox));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_communities() {
|
||||
let mut collector = setup_collector();
|
||||
let community_id1 = CommunityId(1);
|
||||
let community_id2 = CommunityId(2);
|
||||
let community_id3 = CommunityId(3);
|
||||
|
||||
let user1_inbox_str = "https://follower1.example.com/inbox";
|
||||
let user1_inbox = Url::parse(user1_inbox_str).unwrap();
|
||||
let user2_inbox_str = "https://follower2.example.com/inbox";
|
||||
let user2_inbox = Url::parse(user2_inbox_str).unwrap();
|
||||
let user3_inbox_str = "https://follower3.example.com/inbox";
|
||||
let user3_inbox = Url::parse(user3_inbox_str).unwrap();
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_get_instance_followed_community_inboxes()
|
||||
.times(2)
|
||||
.returning(move |_, last_fetch| {
|
||||
if last_fetch == Utc.timestamp_nanos(0) {
|
||||
Ok(vec![
|
||||
(community_id1, Url::parse(user1_inbox_str).unwrap().into()),
|
||||
(community_id2, Url::parse(user2_inbox_str).unwrap().into()),
|
||||
])
|
||||
} else {
|
||||
Ok(vec![(
|
||||
community_id3,
|
||||
Url::parse(user3_inbox_str).unwrap().into(),
|
||||
)])
|
||||
}
|
||||
});
|
||||
|
||||
// First update
|
||||
collector.update_communities().await.unwrap();
|
||||
assert_eq!(collector.followed_communities.len(), 2);
|
||||
assert!(collector.followed_communities[&community_id1].contains(&user1_inbox));
|
||||
assert!(collector.followed_communities[&community_id2].contains(&user2_inbox));
|
||||
|
||||
// Simulate time passing
|
||||
collector.last_full_communities_fetch = Utc::now() - chrono::TimeDelta::try_minutes(3).unwrap();
|
||||
collector.last_incremental_communities_fetch =
|
||||
Utc::now() - chrono::TimeDelta::try_minutes(3).unwrap();
|
||||
|
||||
// Second update (incremental)
|
||||
collector.update_communities().await.unwrap();
|
||||
assert_eq!(collector.followed_communities.len(), 3);
|
||||
assert!(collector.followed_communities[&community_id1].contains(&user1_inbox));
|
||||
assert!(collector.followed_communities[&community_id3].contains(&user3_inbox));
|
||||
assert!(collector.followed_communities[&community_id2].contains(&user2_inbox));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_inbox_urls_no_duplicates() {
|
||||
let mut collector = setup_collector();
|
||||
collector.domain = "example.com".to_string();
|
||||
let community_id = CommunityId(1);
|
||||
let site_inbox = Url::parse("https://example.com/site_inbox").unwrap();
|
||||
let site_inbox_clone = site_inbox.clone();
|
||||
let site = Site {
|
||||
id: SiteId(1),
|
||||
name: "Test Site".to_string(),
|
||||
sidebar: None,
|
||||
published: Utc::now(),
|
||||
updated: None,
|
||||
icon: None,
|
||||
banner: None,
|
||||
description: None,
|
||||
actor_id: Url::parse("https://example.com/site").unwrap().into(),
|
||||
last_refreshed_at: Utc::now(),
|
||||
inbox_url: site_inbox.clone().into(),
|
||||
private_key: None,
|
||||
public_key: "test_key".to_string(),
|
||||
instance_id: InstanceId(1),
|
||||
content_warning: None,
|
||||
};
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_read_site_from_instance_id()
|
||||
.return_once(move |_| Ok(Some(site)));
|
||||
|
||||
collector
|
||||
.data_source
|
||||
.expect_get_instance_followed_community_inboxes()
|
||||
.return_once(move |_, _| Ok(vec![(community_id, site_inbox_clone.into())]));
|
||||
|
||||
collector.update_communities().await.unwrap();
|
||||
|
||||
let activity = SentActivity {
|
||||
id: ActivityId(1),
|
||||
ap_id: Url::parse("https://example.com/activities/1")
|
||||
.unwrap()
|
||||
.into(),
|
||||
data: json!({}),
|
||||
sensitive: false,
|
||||
published: Utc::now(),
|
||||
send_inboxes: vec![Some(site_inbox.into())],
|
||||
send_community_followers_of: Some(community_id),
|
||||
send_all_instances: true,
|
||||
actor_type: ActorType::Person,
|
||||
actor_apub_id: None,
|
||||
};
|
||||
|
||||
let result = collector.get_inbox_urls(&activity).await.unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert!(result.contains(&Url::parse("https://example.com/site_inbox").unwrap()));
|
||||
}
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
use crate::util::get_actor_cached;
|
||||
use activitypub_federation::{
|
||||
activity_sending::SendActivityTask,
|
||||
config::Data,
|
||||
protocol::context::WithContext,
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use chrono::{DateTime, Utc};
|
||||
use lemmy_api_common::{context::LemmyContext, federate_retry_sleep_duration};
|
||||
use lemmy_apub::{activity_lists::SharedInboxActivities, FEDERATION_CONTEXT};
|
||||
use lemmy_db_schema::{newtypes::ActivityId, source::activity::SentActivity};
|
||||
use reqwest::Url;
|
||||
use std::ops::Deref;
|
||||
use tokio::{sync::mpsc::UnboundedSender, time::sleep};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
#[derive(Debug, Eq)]
|
||||
pub(crate) struct SendSuccessInfo {
|
||||
pub activity_id: ActivityId,
|
||||
pub published: Option<DateTime<Utc>>,
|
||||
// true if the activity was skipped because the target instance is not interested in this
|
||||
// activity
|
||||
pub was_skipped: bool,
|
||||
}
|
||||
impl PartialEq for SendSuccessInfo {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.activity_id == other.activity_id
|
||||
}
|
||||
}
|
||||
/// order backwards because the binary heap is a max heap, and we need the smallest element to be on
|
||||
/// top
|
||||
impl PartialOrd for SendSuccessInfo {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
impl Ord for SendSuccessInfo {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
other.activity_id.cmp(&self.activity_id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the result of sending an activity.
|
||||
///
|
||||
/// This enum is used to communicate the outcome of a send operation from a send task
|
||||
/// to the main instance worker. It's designed to maintain a clean separation between
|
||||
/// the send task and the main thread, allowing the send.rs file to be self-contained
|
||||
/// and easier to understand.
|
||||
///
|
||||
/// The use of a channel for communication (rather than shared atomic variables) was chosen
|
||||
/// because:
|
||||
/// 1. It keeps the send task cleanly separated with no direct interaction with the main thread.
|
||||
/// 2. The failure event needs to be transferred to the main task for database updates anyway.
|
||||
/// 3. The main fail_count should only be updated under certain conditions, which are best handled
|
||||
/// in the main task.
|
||||
/// 4. It maintains consistency in how data is communicated (all via channels rather than a mix of
|
||||
/// channels and atomics).
|
||||
/// 5. It simplifies concurrency management and makes the flow of data more predictable.
|
||||
pub(crate) enum SendActivityResult {
|
||||
Success(SendSuccessInfo),
|
||||
Failure { fail_count: i32 },
|
||||
}
|
||||
/// Represents a task for retrying to send an activity.
|
||||
///
|
||||
/// This struct encapsulates all the necessary information and resources for attempting
|
||||
/// to send an activity to multiple inbox URLs, with built-in retry logic.
|
||||
pub(crate) struct SendRetryTask<'a> {
|
||||
pub activity: &'a SentActivity,
|
||||
pub object: &'a SharedInboxActivities,
|
||||
/// Must not be empty at this point
|
||||
pub inbox_urls: Vec<Url>,
|
||||
/// Channel to report results back to the main instance worker
|
||||
pub report: &'a mut UnboundedSender<SendActivityResult>,
|
||||
/// The first request will be sent immediately, but subsequent requests will be delayed
|
||||
/// according to the number of previous fails + 1
|
||||
///
|
||||
/// This is a read-only immutable variable that is passed only one way, from the main
|
||||
/// thread to each send task. It allows the task to determine how long to sleep initially
|
||||
/// if the request fails.
|
||||
pub initial_fail_count: i32,
|
||||
/// For logging purposes
|
||||
pub domain: String,
|
||||
pub context: Data<LemmyContext>,
|
||||
pub stop: CancellationToken,
|
||||
}
|
||||
|
||||
impl<'a> SendRetryTask<'a> {
|
||||
// this function will return successfully when (a) send succeeded or (b) worker cancelled
|
||||
// and will return an error if an internal error occurred (send errors cause an infinite loop)
|
||||
pub async fn send_retry_loop(self) -> Result<()> {
|
||||
let SendRetryTask {
|
||||
activity,
|
||||
object,
|
||||
inbox_urls,
|
||||
report,
|
||||
initial_fail_count,
|
||||
domain,
|
||||
context,
|
||||
stop,
|
||||
} = self;
|
||||
debug_assert!(!inbox_urls.is_empty());
|
||||
|
||||
let pool = &mut context.pool();
|
||||
let Some(actor_apub_id) = &activity.actor_apub_id else {
|
||||
return Err(anyhow::anyhow!("activity is from before lemmy 0.19"));
|
||||
};
|
||||
let actor = get_actor_cached(pool, activity.actor_type, actor_apub_id)
|
||||
.await
|
||||
.context("failed getting actor instance (was it marked deleted / removed?)")?;
|
||||
|
||||
let object = WithContext::new(object.clone(), FEDERATION_CONTEXT.deref().clone());
|
||||
let requests = SendActivityTask::prepare(&object, actor.as_ref(), inbox_urls, &context).await?;
|
||||
for task in requests {
|
||||
// usually only one due to shared inbox
|
||||
tracing::debug!("sending out {}", task);
|
||||
let mut fail_count = initial_fail_count;
|
||||
while let Err(e) = task.sign_and_send(&context).await {
|
||||
fail_count += 1;
|
||||
report.send(SendActivityResult::Failure {
|
||||
fail_count,
|
||||
// activity_id: activity.id,
|
||||
})?;
|
||||
let retry_delay = federate_retry_sleep_duration(fail_count);
|
||||
tracing::info!(
|
||||
"{}: retrying {:?} attempt {} with delay {retry_delay:.2?}. ({e})",
|
||||
domain,
|
||||
activity.id,
|
||||
fail_count
|
||||
);
|
||||
tokio::select! {
|
||||
() = sleep(retry_delay) => {},
|
||||
() = stop.cancelled() => {
|
||||
// cancel sending without reporting any result.
|
||||
// the InstanceWorker needs to be careful to not hang on receive of that
|
||||
// channel when cancelled (see handle_send_results)
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
report.send(SendActivityResult::Success(SendSuccessInfo {
|
||||
activity_id: activity.id,
|
||||
published: Some(activity.published),
|
||||
was_skipped: false,
|
||||
}))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue