Stop using syncer with people, just spawn a task instead

This commit is contained in:
Mike Dilger 2023-01-01 12:07:55 +13:00
parent dd0ce06828
commit 26141a27d1
2 changed files with 24 additions and 40 deletions

View File

@ -3,21 +3,17 @@ use crate::error::Error;
use crate::globals::GLOBALS; use crate::globals::GLOBALS;
use nostr_types::{Metadata, PublicKeyHex, Unixtime}; use nostr_types::{Metadata, PublicKeyHex, Unixtime};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use tokio::task; use tokio::task;
pub struct People { pub struct People {
people: HashMap<PublicKeyHex, DbPerson>, people: HashMap<PublicKeyHex, DbPerson>,
deferred_load: HashSet<PublicKeyHex>,
deferred_follow: HashMap<PublicKeyHex, bool>,
} }
impl People { impl People {
pub fn new() -> People { pub fn new() -> People {
People { People {
people: HashMap::new(), people: HashMap::new(),
deferred_load: HashSet::new(),
deferred_follow: HashMap::new(),
} }
} }
@ -168,10 +164,21 @@ impl People {
if self.people.contains_key(pubkeyhex) { if self.people.contains_key(pubkeyhex) {
self.people.get(pubkeyhex).cloned() self.people.get(pubkeyhex).cloned()
} else { } else {
// Not there. Maybe it's in the database. Defer and let syncer // We can't get it now, but we can setup a task to do it soon
// try to load let pubkeyhex = pubkeyhex.to_owned();
self.deferred_load.insert(pubkeyhex.to_owned()); tokio::spawn(async move {
let _ = GLOBALS.to_syncer.send("sync_people".to_owned()); let mut people = GLOBALS.people.write().await;
#[allow(clippy::map_entry)]
if !people.people.contains_key(&pubkeyhex) {
match People::fetch_one(&pubkeyhex).await {
Ok(Some(person)) => {
let _ = people.people.insert(pubkeyhex, person);
}
Err(e) => tracing::error!("{}", e),
_ => {}
}
}
});
None None
} }
} }
@ -189,27 +196,6 @@ impl People {
v v
} }
pub async fn sync(&mut self) -> Result<(), Error> {
// handle deferred load
for pubkeyhex in self.deferred_load.iter() {
if !self.people.contains_key(pubkeyhex) {
if let Some(person) = Self::fetch_one(pubkeyhex).await? {
let _ = self.people.insert(pubkeyhex.to_owned(), person);
}
}
}
self.deferred_load.clear();
// handle deferred follow
let df = self.deferred_follow.clone();
for (pubkeyhex, follow) in df {
self.async_follow(&pubkeyhex, follow).await?;
}
self.deferred_follow.clear();
Ok(())
}
/// This is a 'just in case' the main code isn't keeping them in sync. /// This is a 'just in case' the main code isn't keeping them in sync.
pub async fn populate_new_people() -> Result<(), Error> { pub async fn populate_new_people() -> Result<(), Error> {
let sql = "INSERT or IGNORE INTO person (pubkey) SELECT DISTINCT pubkey FROM EVENT"; let sql = "INSERT or IGNORE INTO person (pubkey) SELECT DISTINCT pubkey FROM EVENT";
@ -226,11 +212,14 @@ impl People {
} }
pub fn follow(&mut self, pubkeyhex: &PublicKeyHex, follow: bool) { pub fn follow(&mut self, pubkeyhex: &PublicKeyHex, follow: bool) {
self.deferred_follow // We can't do it now, but we spawn a task to do it soon
.entry(pubkeyhex.clone()) let pubkeyhex = pubkeyhex.to_owned();
.and_modify(|d| *d = follow) tokio::spawn(async move {
.or_insert_with(|| follow); let mut people = GLOBALS.people.write().await;
let _ = GLOBALS.to_syncer.send("sync_people".to_owned()); if let Err(e) = people.async_follow(&pubkeyhex, follow).await {
tracing::error!("{}", e);
}
});
} }
pub async fn async_follow( pub async fn async_follow(

View File

@ -21,11 +21,6 @@ impl Syncer {
let message = message.unwrap(); let message = message.unwrap();
match &*message { match &*message {
"sync_people" => {
if let Err(e) = GLOBALS.people.write().await.sync().await {
tracing::error!("Problem syncing people: {}", e);
}
}
"sync_fetcher" => { "sync_fetcher" => {
if let Err(e) = GLOBALS.fetcher.sync().await { if let Err(e) = GLOBALS.fetcher.sync().await {
tracing::error!("Problem fetching from web: {}", e); tracing::error!("Problem fetching from web: {}", e);