dead instances in one query

This commit is contained in:
phiresky 2023-09-01 12:30:35 +00:00
parent 557576ad80
commit e945e9f308
2 changed files with 12 additions and 16 deletions

View file

@ -64,15 +64,6 @@ impl Instance {
.await
}
pub async fn dead_instances(pool: &mut DbPool<'_>) -> Result<Vec<String>, Error> {
let conn = &mut get_conn(pool).await?;
instance::table
.select(instance::domain)
.filter(coalesce(instance::updated, instance::published).lt(now() - 3.days()))
.get_results(conn)
.await
}
#[cfg(test)]
pub async fn delete_all(pool: &mut DbPool<'_>) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
@ -96,10 +87,15 @@ impl Instance {
.await
}
/// returns a list of all instances, each with a flag of whether the instance is allowed or not
/// returns a list of all instances, each with a flag of whether the instance is allowed or not and dead or not
/// ordered by id
pub async fn read_all_with_blocked(pool: &mut DbPool<'_>) -> Result<Vec<(Self, bool)>, Error> {
pub async fn read_all_with_blocked_and_dead(
pool: &mut DbPool<'_>,
) -> Result<Vec<(Self, bool, bool)>, Error> {
let conn = &mut get_conn(pool).await?;
let is_dead_expr = coalesce(instance::updated, instance::published).lt(now() - 3.days());
// this needs to be done in two steps because the meaning of the "blocked" column depends on the existence
// of any value at all in the allowlist. (so a normal join wouldn't work)
let use_allowlist = federation_allowlist::table
.select(count_star().gt(0))
.get_result::<bool>(conn)
@ -110,9 +106,10 @@ impl Instance {
.select((
Self::as_select(),
federation_allowlist::id.nullable().is_not_null(),
is_dead_expr,
))
.order_by(instance::id)
.get_results::<(Self, bool)>(conn)
.get_results::<(Self, bool, bool)>(conn)
.await
} else {
instance::table
@ -120,9 +117,10 @@ impl Instance {
.select((
Self::as_select(),
federation_blocklist::id.nullable().is_null(),
is_dead_expr,
))
.order_by(instance::id)
.get_results::<(Self, bool)>(conn)
.get_results::<(Self, bool, bool)>(conn)
.await
}
}

View file

@ -46,11 +46,10 @@ async fn start_stop_federation_workers(
let pool2 = &mut DbPool::Pool(&pool);
let process_index = opts.process_index - 1;
loop {
let dead: HashSet<String> = HashSet::from_iter(Instance::dead_instances(pool2).await?);
let mut total_count = 0;
let mut dead_count = 0;
let mut disallowed_count = 0;
for (instance, allowed) in Instance::read_all_with_blocked(pool2).await? {
for (instance, allowed, is_dead) in Instance::read_all_with_blocked_and_dead(pool2).await? {
if instance.id.inner() % opts.process_count != process_index {
continue;
}
@ -58,7 +57,6 @@ async fn start_stop_federation_workers(
if !allowed {
disallowed_count += 1;
}
let is_dead = dead.contains(&instance.domain);
if is_dead {
dead_count += 1;
}