From e3d09c3446b6a5ba7e7fafa66712b3420714e226 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 May 2022 00:55:01 +0200 Subject: refactor(tazjin/tgsa): factor out cache access helper factor out a function to access telegram posts from the cache, fetching them anew if required. a small behavioural change means that the program now takes a write lock when fetching a post, to avoid simultaneously fetching the same post many times (quite likely once it serves image redirects). Change-Id: If9c1429f86fb118dab90834f349cb222709c3a31 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5608 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/tgsa/src/main.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'users/tazjin/tgsa') diff --git a/users/tazjin/tgsa/src/main.rs b/users/tazjin/tgsa/src/main.rs index 28b5d4253a..911f1bad67 100644 --- a/users/tazjin/tgsa/src/main.rs +++ b/users/tazjin/tgsa/src/main.rs @@ -188,34 +188,42 @@ fn to_bbcode(link: &TgLink, msg: &TgMessage) -> String { // cache everything for one hour const CACHE_EXPIRY: Duration = Duration::from_secs(60 * 60); -struct CacheEntry { +#[derive(Clone)] +struct TgPost { bbcode: String, at: Instant, } -type Cache = RwLock>; +type Cache = RwLock>; -fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result { +fn fetch_with_cache(cache: &Cache, link: &TgLink) -> Result { if let Some(entry) = cache.read().unwrap().get(&link) { if Instant::now() - entry.at < CACHE_EXPIRY { println!("serving {}#{} from cache", link.username, link.message_id); - return Ok(entry.bbcode.to_string()); + return Ok(entry.clone()); } } + // limit concurrent fetching + // TODO(tazjin): per link? + let mut writer = cache.write().unwrap(); + let embed = fetch_embed(&link)?; let msg = parse_tgmessage(&embed)?; - let bbcode = to_bbcode(&link, &msg); - cache.write().unwrap().insert( - link.clone(), - CacheEntry { - bbcode: bbcode.clone(), - at: Instant::now(), - }, - ); + let post = TgPost { + bbcode: to_bbcode(&link, &msg), + at: Instant::now(), + }; + + writer.insert(link.clone(), post.clone()); - Ok(bbcode) + Ok(post) +} + +fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result { + let post = fetch_with_cache(cache, link)?; + Ok(post.bbcode) } fn main() { -- cgit 1.4.1