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 <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2022-05-15 00:55:01 +02:00 committed by tazjin
parent 89f25b431d
commit e3d09c3446

View file

@ -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<HashMap<TgLink, CacheEntry>>;
type Cache = RwLock<HashMap<TgLink, TgPost>>;
fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result<String> {
fn fetch_with_cache(cache: &Cache, link: &TgLink) -> Result<TgPost> {
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(),
};
Ok(bbcode)
writer.insert(link.clone(), post.clone());
Ok(post)
}
fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result<String> {
let post = fetch_with_cache(cache, link)?;
Ok(post.bbcode)
}
fn main() {