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 nostr_types::{Metadata, PublicKeyHex, Unixtime};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use tokio::task;
pub struct People {
people: HashMap<PublicKeyHex, DbPerson>,
deferred_load: HashSet<PublicKeyHex>,
deferred_follow: HashMap<PublicKeyHex, bool>,
}
impl People {
pub fn new() -> People {
People {
people: HashMap::new(),
deferred_load: HashSet::new(),
deferred_follow: HashMap::new(),
}
}
@ -168,10 +164,21 @@ impl People {
if self.people.contains_key(pubkeyhex) {
self.people.get(pubkeyhex).cloned()
} else {
// Not there. Maybe it's in the database. Defer and let syncer
// try to load
self.deferred_load.insert(pubkeyhex.to_owned());
let _ = GLOBALS.to_syncer.send("sync_people".to_owned());
// We can't get it now, but we can setup a task to do it soon
let pubkeyhex = pubkeyhex.to_owned();
tokio::spawn(async move {
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
}
}
@ -189,27 +196,6 @@ impl People {
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.
pub async fn populate_new_people() -> Result<(), Error> {
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) {
self.deferred_follow
.entry(pubkeyhex.clone())
.and_modify(|d| *d = follow)
.or_insert_with(|| follow);
let _ = GLOBALS.to_syncer.send("sync_people".to_owned());
// We can't do it now, but we spawn a task to do it soon
let pubkeyhex = pubkeyhex.to_owned();
tokio::spawn(async move {
let mut people = GLOBALS.people.write().await;
if let Err(e) = people.async_follow(&pubkeyhex, follow).await {
tracing::error!("{}", e);
}
});
}
pub async fn async_follow(

View File

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